6
votes

I am trying to capture an image frame from a USB camera using opencv. However I am always getting a black frame. I have read many posts with this issue and tried all the suggestions but nothing worked for me.

I started using the code discussed here: http://opencv-users.1802565.n2.nabble.com/Using-USB-Camera-td6786148.html

I have tried including the method cvWaitKey(1000) after many 'critical' sentences. As you can see the waiting value is very high (1000).

I have also tried to save the image frame and, equally, it is a black image.

I am using the following system:

  • OpenCV 2.2.0
  • Windows 7, 32 bits
  • Visual Studio 2010 (C++)
  • a board usb camera (which I do not know the manufacturer)

The usb camera works well with AMCAP.EXE 1.00.

Could it be because of the camera drivers being used by Windows? Could I change to other drivers that work better for OpenCV 2.2.0?

Thanks

4
Can i see your code tat grabs the frame part? So that I can help. Cheers (:rockinfresh
Or wat do your output window says?rockinfresh
I tried with a Logitech cam and it all works well... so I guess it is not a code problem... I am still thinking that the drivers play a key role in this...jaiserpe
Cuz I also did snap a black image with my usb camera before, but mangaed to solve it. You are right when you said driver play a role. This is actually if and only if the camera is not compatible with OpenCV. But I have yet to come across such webcam. Some other method could be to use a format that the USB driver is using, (declare it and set the height and width). The codes, are you using "CvCapture* capture = cvCaptureFromCAM(0);" and "IplImage* img=cvRetrieveFrame(capture);" ?rockinfresh
@rockinfresh, can you write some code to exemplify your suggestion about the use of a format that the USB driver is using?jaiserpe

4 Answers

6
votes

Ok. As I promised to your request in the comments, and sorry to keep you waiting, really been busy. Barely had time to post this answer too. But here it is:

This is me simulating that opencv is capturing black image. On the output window, which I had asked you in the comments about what it says, shows that there is an error.

enter image description here

After investigating, I realised that it is due to the camera's available format: enter image description here

Of cuz, this is a lousier camera. If you have a better camera like the logitech one, you can see that the format available is so much more. enter image description here

There are lots of methods, you can try some thing like

capture.set(CV_CAP_PROP_FRAME_WIDTH , 640); 
capture.set(CV_CAP_PROP_FRAME_HEIGHT , 480); 
capture.set (CV_CAP_PROP_FOURCC, CV_FOURCC('B', 'G', 'R', '3'));//diff from mine, using as example

then the webcam will be able to snap. This webcam is bit faulty, hence the image snapped is not that beautiful.enter image description here

Hope this is your problem. but it may not be the case too. I like debugging problems, but I can't put down all the possible causes that this happen for you as I am really busy, as you asked for an example, this is one of them. Cheers. If you could tell me what you output window error says, I probably can help more.

EDIT(to answer more in your comments):

Ok, I want you to try a few things:

1)First, instead of using cvQueryFrame, or similar capturing methods, I want you to try use that webcam to capture a video instead. Wait up to maybe say 10 secs to see if it's successful. Reason being, some cameras(lower quality ones) take quite a while to warm up and the first few frames they capture may be an empty one.

2) If the step one doesn't work, try typing

cout << cv::getBuildInformation() << endl;

and paste the results for media I/O and Video I/O? I want to see the results. I would suspect your library dependencies too, but since you said it works with a logitech camera, I doubt that's the case. Of course, there's always a chance it's due to that the camera is not compatible to OpenCV. Does the camera have any brands by the way?

3) Alternatively, just search for usb drivers online and install it, I had a friend who did this for a similar problem, but not sure the process of that.

1
votes

The first thing, I would suggest is, visit this link and check your camera is working or not

http://www.youronlinemirror.com/

if yes, then go through below link to get started with things, you ll also find a good opencv c++ code out there, the code which you are using is of opencv1 api's c code, I would rather encourage you to go for c++ than the old version of opencv.

http://opencv-srf.blogspot.in/2011/09/capturing-images-videos.html

if you want an answer for your code, then, its simple, as you are saying its giving a black screen, which happened in my case when i started things out with opencv, it isnt able to take the data from device, so, try this, it might work, as it did for me.

add

cvQueryFrame( capture ); 

before

IplImage* frame = cvQueryFrame( capture ); 
1
votes

I went through the same problem as yours. Then I just changed the version from 3.1.0 to 2.4.13, then my webcam works! No more black images. I guess the 3 version is not compatible with the vs15. Thinking you may have already solved the problem long ago. But just post to let others know if they happen to have the same issue.

0
votes

Even I faced the same black screen problem while running OpenCV-related programs. So instead of using USB cam I used mobile camera which worked perfectly fine. Use Google apps such as DroidCam (install DroidCam on mobile as well as on laptop/PC) to connect your mobile camera to laptop through Wi-Fi.