Opencv imshow showing gray image.
#include "stdafx.h"
#include <opencv2\opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap;
Mat frame;
cap.open(0);
while (true)
{
cap >> frame;
imshow("test", frame);
waitKey(30);
}
return 0;
}
I recently upgraded to windows insider preview 14316 and it stopped working after that i think.
I checked if the camera work in skype, and it did. I also tried the code on another computer and it worked.
Any ideas?
Related
I have problem with RTSP stream from my BOSH cammera. While Im getting stream to my program with this code:
VideoCapture cap("rtsp://name:password#IPaddress:554");
i get some stream, but after few images i get this error:
enter image description here
and then, my stream looks like this:
enter image description here
I saw that there are more solutions for my problem, like using VLC libraries to resolve my problem, or GStreamer. But this is no solution for my problem, because I need to use OPenCV libraries, save every image to MATRIX, and Im then using my special image processing..
Does anybody have solution ?
Is it possible to create some buffer ? If answer yes, can anybody write HOW ?
Here is my code:
#include <opencv\cv.h>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
int main()
{
Mat frame;
namedWindow("video", 1);
VideoCapture cap("rtsp://name:password#IPaddress:554");
if(!cap.isOpened())
{
cout<<"Camera not found"<<endl;
return -1;
}
while(1)
{
cap >> frame;
if(frame.empty()) break;
resize(frame, frame, Size(1200, 800), 0, 0, INTER_CUBIC);
imshow("video", frame);
if(waitKey(1000) >= 0) break;
}
return 0;
}
Every time i compile this code the webcam streaming is mirrored like I lift up my right hand and it appears like if it was my left hand on the screen instead and after a couple of re-compiling an error message appeared and the code never worked again.
The error:
Unhandled exception at 0x00007FFB3C6DA1C8 in Camera.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000D18AD5F610.
And no other option left except to break the process.
The code:
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
imshow("window", image);
waitKey(33);
}
}
I don't know if something wrong with my code I've just started learning opencv !
#include <opencv2/highgui/highgui.hpp>
#include <opencv\cv.h>
using namespace cv;
int main(){
Mat image;
VideoCapture cap;
cap.open(1);
namedWindow("Window", 1);
while (1){
cap >> image;
flip(image,image,1)
imshow("window", image);
waitKey(33);
}
}
just Flip the image horizontally that will do Find More Here
There is nothing wrong when your image is mirrored on the vertical (/x) axis (I guess in your example you are using a built-in (laptop) webcam).
A (very) simple code for capturing and showing your image could be the following:
// imgcap in opencv3
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
int main() {
cv::VideoCapture cap(0); // camera 0
cv::Mat img;
while(true) {
cap >> img;
cv::flip(img,img,1);
cv::imshow("live view", img);
cv::waitKey(1);
}
return 0;
}
When using OpenCV 3 you should include headers like:
#include <opencv2/highgui.hpp>
I'm having an issue that I've never had with OpenCV before. When I use cv::imshow() the image is shown perfectly, but there are weird ascii characters along the top bar instead of the window name and every few seconds a new window appears with a different name and the old freezes on whatever frame it was currently on. I can exit out of the windows that are frozen but more appear instantly after with different names. I have used OpenCV before and imshow() has always worked for me.
Here's my code:
#include "stdafx.h"
#include "opencv2/opencv.hpp"
using namespace cv;
int main()
{
VideoCapture cap(0);
Mat frame;
namedWindow("Sample Program", CV_WINDOW_AUTOSIZE);
while (true)
{
cap >> frame;
imshow("Sample Program", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
If I replace imshow() with
cvShowImage("Sample Program", cvCloneImage(&(IplImage)frame));
It works perfectly.
I just want to know if theres a library issue or something that is causing this issue
I'm using OpenCV 2.4.9 in Visual Studio Ultimate 2012
using namespace std;
using namespace cv;
With that cv it works for me.
I am using console linux and I have a camera capture application. I need to capture an image without GUI(The camera should start and capture some images, save it to disk and close). The following code works well on my laptop but doesn't start on console. Any suggestions?
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
Mat frame;
namedWindow("feed",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("feed", frame);
imwrite("/home/zaif/output.png", frame);
if(waitKey(1) >= 0) break;
}
return 0;
}
After the release of OpenCV 2.4.6 there were bug fixes for video capture on Linux. Go straight to 2.4.6.2 and you should get the fixes. Specifically, this revision is probably the relevant fix for you, although there were a number of other revisions pertaining to video capture on android that might effect Linux compilation too.
I need to get the frame feed of the video from OpenCV. My code runs well but I need to get the frames it is processing at each ms.
I am using cmake on Linux.
My code:
#include "cv.h"
#include "highgui.h"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
Mat frame;
namedWindow("feed",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("feed", frame);
if(waitKey(1) >= 0) break;
}
return 0;
}
I'm assuming you want to store the frames. I would recommend std::vector (as GPPK recommends). std::vector allows you to dynamically create an array. The push_back(Mat()) function adds an empty Mat object to the end of the vector and the back() function returns the last element in the array (which allows cap to write to it).
The code would look like this:
#include "cv.h"
#include "highgui.h"
using namespace cv;
#include <vector>
using namespace std; //Usually not recommended, but you're doing this with cv anyway
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
vector<Mat> frame;
namedWindow("feed",1);
for(;;)
{
frame.push_back(Mat());
cap >> frame.back(); // get a new frame from camera
imshow("feed", frame);
// Usually recommended to wait for 30ms
if(waitKey(30) >= 0) break;
}
return 0;
}
Note that you can fill your RAM very quickly like this. For example, if you're grabbing 640x480 RGB frames every 30ms, you will hit 2GB of RAM in around 70s.
std::vector is a very useful container to know, and I would recommend checking out a tutorial on it if it is unfamiliar to you.