tags:

views:

7319

answers:

12

I am new to OpenCV, and trying to capture an image, and then save it to a file. I am posting the code for your reference, below.

The jpg file is being saved, but it is black.

// Capture the Image from the webcam
CvCapture *pCapturedImage = cvCreateCameraCapture(0);

// Get the frame
IplImage *pSaveImg = cvQueryFrame(pCapturedImage);

// Save the frame into a file
cvSaveImage("test.jpg". ,pSaveImg); // A JPG FILE IS BEING SAVED
                                    // OF 6KB , BUT IT IS BLACK

All of the functions are succesful. I have tried the above code in both XP and Vista - the result is a black image on both. Please let me know what I am missing out.

A: 

Sometimes the first call to cvQueryFrame() returns an empty image. Try:

IplImage *pSaveImg = cvQueryFrame(pCapturedImage);
pSaveImg = cvQueryFrame(pCapturedImage);

If that does not work, try to select capture device automatically:

CvCapture *pCapturedImage = cvCreateCameraCapture(-1);

Or you may try to select other capture devices where n=1,2,3...

CvCapture *pCapturedImage = cvCreateCameraCapture(n);

PS: Also I believe there is a misunderstanding about captured image looking at your variable name. The variable pCapturedImage is not an Image it is a Capture. You can always 'read' an image from capture.

nimcap
I am still getting a black image.I even tried this out in a while loop ; which shall break when 'q' is pressed. I also use cvVideoWriter , so as to save the captured frames into a video file (avi) , I still get a file which has black contents .Do I need to install any codecs, for OpenCV to run properly .Thanks,SujayI get a black jpg, blac
Sujay Ghosh
I am not sure I will be able to help you further.
nimcap
A: 

I use the following code to capture images:

CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
if(!capture) error((char*)"No Capture");
IplImage *img=cvQueryFrame(capture);

I know this works for sure

Yogi
A: 

sorry if this is too obvious. Are you sure the webcam is properly seen and detected by OpenCV in other words, do you get an image when you redirect the captured frame to a "highGui" window? For instance like so:

 frame = cvQueryFrame( capture );
 cvNamedWindow( "myWindow", CV_WINDOW_AUTOSIZE );
 cvShowImage( "myWindow", frame );
david h
This code is also giving me a black image. Do I need to install codecs, or are there any dependenciesMy web cam is startng , but there is a black image
Sujay Ghosh
A: 

From my experiences the first few frames that are captured when using:

frame = cvQueryFrame( capture );

Tend to be blank. You may want to wait a short while(about 3 seconds) and then try to capture the image.

Nexus
+1  A: 

I suggest you run OpenCV sanity check

Its a serie of small executables located in the bin directory of opencv.

It will check if your camera is ok

Eric
Thanks for the info. I shall check it.But I think my camera is okay, becuase using RoboRealm, a robotics tool, I can see my image and picute. I also use the web cam for video conferenceing, and people can see me.
Sujay Ghosh
I tried running opencv on my netbook, the camera usually works fine but for some reason, it doesn't pass the sanity checks. I can't have it to work either
Eric
A: 

i have the same damn problem in vista, in my xp it works perfectly.

Spraken
It does not for me on XP either. Any thoughts what is going wrong
Sujay Ghosh
A: 

I know the problem! You just put a dot after "test.jpg"!

cvSaveImage("test.jpg". ,pSaveImg);

I may be wrong but I think its not good!

lf
A: 

i think, simply camera not initialize in first frame. Try to save image after 10 frames.

chronos
A: 

Make sure to check if capture is null using the second line of Yogi's code.

Mike Chelen
A: 

I had the same problem with vista, what I did was, add this code before cvQueryFrame

cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 720);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);

Bye

jmp4281
A: 

You might need to change your drivers for the cam. That could only be the reason. At times these drivers dont work on Vista but do work on XP. If its PNP webcam, remove the drivers and extra software that you might have installed with it. Then try again.

Wajih
A: 

You might need to change your drivers for the cam. That could only be the reason. At times these drivers dont work on Vista but do work on XP. If its PNP webcam, remove the drivers and extra software that you might have installed with it. Then try again.

Wajih