Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV webcam capture problem

Tags:

c++

c

opencv

webcam

I've installed OpenCV 2.2 and now I can't get webcam capture to work. It worked ok in 2.1. OpenCV detects a webcam, doesn't report any errors or warnings, but each frame is a gray image. I even tried a code sample from OpenCV wiki:

VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

Mat edges;
namedWindow("edges",1);
for(;;)
{
    Mat frame;
    cap >> frame; // get a new frame from camera
    cvtColor(frame, edges, CV_BGR2GRAY);
    //GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
    //Canny(edges, edges, 0, 30, 3);
    imshow("edges", edges);
    if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;

Did anyone run into this issue? I'm using 64bit Win7 and Visual Studio 2010.

like image 326
Figaro Avatar asked Dec 13 '10 15:12

Figaro


4 Answers

I found the solution after a very long search.

The problem is that if doesn't have a delay between showing the frames happen this problem.

The solution is put cvWaitKey(20); in loop.

like image 200
Victor Avatar answered Nov 13 '22 09:11

Victor


The issue was with the camera I used, MSFT LifeCam. I tried Logitech C210 and 120 and they both work fine.

like image 25
Figaro Avatar answered Nov 13 '22 10:11

Figaro


Here is the Solution.

Every image frame captured is being converted into grayscale image in the first line below. Commenting and running the code alone will show an error since since you are not capturing any processed image into the edges frame, which is being displayed in imshow.

Hence comment the cvtColor line and change the second parameter in imshow to frame. This will allow you to display the colour video from the webcam.

cvtColor(frame, edges, CV_BGR2GRAY);

imshow("edges", frame);
like image 24
user1555123 Avatar answered Nov 13 '22 08:11

user1555123


I really don't know anything about OpenCV, but Isn't the problem on the following line ?

cvtColor(frame, edges, CV_BGR2GRAY);

Seems like you are intentionally converting a B-G-R color space into a Grayscale space.

Shouldn't it be something like:

cvtColor(frame, edges, CV_BGR2RGB);
like image 1
Machado Avatar answered Nov 13 '22 08:11

Machado