9

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
Figaro
  • 141
  • 1
  • 1
  • 8
  • 2
    What do you mean by "a gray image"? Solid gray, with no structure? Or, just a grayscale image? You are calling cvtColor() to grayscale the image, and the imshow() call is showing the grayscale version... – jtdubs Dec 13 '10 at 15:27
  • Sometimes this ib is a little bit slow. Add a `sleep(1)` or `cv::waitKey(100)` before Mat edge and add the same inside the for loop. This should work. And don't forget to chekc if `frame` is a prper image. – nutario Dec 13 '10 at 21:19
  • I have the same problem. My code worked just fine with OpenCV 2.1, then I upgraded to OpenCV2.2, and suddenly all camera input is displayed as an all gray picture. Downgrading to OpenCV2.1 solved the problem, but that cannot be the solution. OpenCV2.2 issue? – Tom Mar 24 '11 at 13:03

8 Answers8

9

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.

Nifle
  • 11,745
  • 10
  • 75
  • 100
Victor
  • 91
  • 1
3

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);
nalply
  • 26,770
  • 15
  • 78
  • 101
user1555123
  • 377
  • 3
  • 4
3

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

Figaro
  • 141
  • 1
  • 1
  • 8
1

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);
Machado
  • 13
  • 2
  • most edge detect algorithms only work on grayscale images - the op is displaying the grayscale image instead of the original color one – Martin Beckett Dec 13 '10 at 16:54
  • That's the problem by giving my opinion on a matter that I don't know... :) I thought the issue was the grayscale image, not a gray image itself... Silly me. :) – Machado Dec 15 '10 at 18:29
0

You might try a look at this post as well.

To put it simple, changing

from

import cv

to

import cv2.cv as cv

worked for me. See also the post here.

Community
  • 1
  • 1
David
  • 15,894
  • 22
  • 55
  • 66
0

I had the same problem. I figured that it might be the settings on my camera, because I was supposed to have an input of 640x480px (which I could not adjust) and I could not adjust the permissions for the feed. So I installed a virtual webcam, which pretty much resolved both issues. I got an input dialogue, chose the virtual camera and it worked. I managed to set it to 640x480 in the app too.

My virtual camera was called ManyCam, but it looks like it is made for teenage girls, wanted me to install 3 other apps and I'm still not sure weather it doesn't come with 'complementary' trojans.

However, it lets you adjust colors, hue, contrast etc. and other stuff you might need for testing.

0

I am using cvtColor and found that

cvtColor(image,image,CV_BGR2RGB); didn't work.

But the good news is that this change work !!

cvtColor(image,image,**COLOR_BGR2RGB**);

Also include:

include opencv2/imgproc/imgproc.hpp

and in the .pro file the library:

-lopencv_imgproc 
Gerard de Visser
  • 7,590
  • 9
  • 50
  • 58
0

Try this:

VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
{
  cout<<"Cam not captured\n";
}

Mat frame;
namedWindow("frame");
for(;;)
{
    cap >> frame; // get a new frame from camera
    imshow("frame", frame);

    if(waitKey(10) >= 0) break;
}

return 0;
Saransh Kejriwal
  • 2,467
  • 5
  • 15
  • 39