0

I am coding an app in C for windows using openCV. I want to capture video from the webcam and show it in a window. The app is almost finished but it doesn't work properly. I think it's because of the cvQueryFrame() that alwasy returns NULL and I don't know why. I tried capturing some frames before going into the while but didn't fix the problem.

The compiler doesn't show me any error. It's not a compiling problem but an execution one. I debugged it step by step and in the line

if(!originalImg) break;

it allways jumps out of the while. That's why the app doesn't remain in execution. It opens and closes very fast. Here's the code:

void main()
{
cvNamedWindow("Original Image", CV_WINDOW_AUTOSIZE);

while (1)
    {
    originalImg = cvQueryFrame(capture);

    if(!originalImg) break;

    cvShowImage("Original Image", originalImg);

    c = cvWaitKey(10);
    if( c == 27 ) break;
}

cvReleaseCapture(&capture);

cvDestroyWindow("Original Image");

}

Let's see if someone have some idea and can help me with this, thanks!

Antoni
  • 2,071
  • 2
  • 24
  • 31

2 Answers2

1

It seems you have not opened capture. Add in the beginning of main:

CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);
Andrey Smorodov
  • 10,649
  • 2
  • 35
  • 42
  • Oh sorry, I only showed the main of my app. I have in my code all the variables declarated. Any other suggestion? – Antoni Aug 15 '13 at 13:38
  • Check in the debugger if value of capture is NULL. If it so, then opencv can't find your camera. – Andrey Smorodov Aug 15 '13 at 13:54
  • I have just cheked it and you're right. Te value of capture is NULL. Do you know how can I solve this problem? I'm working in a HP laptop which has a WebCAM incorporated. – Antoni Aug 15 '13 at 14:02
  • Try recompile opencv (or just HighGui module) with flags HAVE_VIDEOINPUT HAVE_DSHOW. In many cases it helps. – Andrey Smorodov Aug 15 '13 at 14:05
  • What do you mean with "recompile opencv"?? I have no advanced knowledge in opencv and I don't understand what you mean... – Antoni Aug 15 '13 at 14:15
  • You can get sources here: https://github.com/Itseez/opencv and there are a lot of videos about building creating and configuring opencv with CMAKE. For example http://www.youtube.com/watch?v=XeBhwbRoKvk It's not hard, and very userful. – Andrey Smorodov Aug 15 '13 at 14:37
  • Thanks man! I'll check it. Right now I'm completely sure that the problem is not in my code but in the compatibility between opencv-SO-webCAM. I tried cvCaptureFromFile reading an .avi file and it worked properly. I will try with an old Logitech usb WebCAM and see if it works. – Antoni Aug 15 '13 at 14:49
1

Assuming the compilation was ok (included all relevant libraries), it may be that the camera has not been installed properly. Can you check if you are able to use the webcam otherwise (using some other software)

If the compilation is actually the issue, please refer to the following related question: https://stackoverflow.com/a/5313594/1218748

Quick summary:

Recompile opencv_highgui changing the "Preprocesser Definitions" in the C/C++ panel of the properties page to include: HAVE_VIDEOINPUT HAVE_DSHOW

There are other good answers that raise some relvant good points, but my gut feeling is that the above solution would work :-)

Community
  • 1
  • 1
mehfoos yacoob
  • 408
  • 3
  • 9
  • Thanks for your answer. I have changed the Preprocessor Definitions of my project adding what you said (HAVE_VIDEOINPUT and HAVE_DSHOW). But what do you mean with "recompile opencv_highgui"? Do I have to do something after adding HAVE_VIDEOINPUT and HAVE_DSHOW? – Antoni Aug 15 '13 at 18:51
  • Hi @JeanClaude93. If you make changes such as the one I mentioned; in order to actually implement the changes in the library that you are using (highgui, in this case), you will have to build the library again in order to modify the dll & lib files. I do not know which IDE and compiler you are using, but here is the instruction page http://opencv.willowgarage.com/wiki/InstallGuide#Compile_using_Visual_Studio (Note: I made it point to MS VS, because that is usually where people start off, with opencv) – mehfoos yacoob Aug 15 '13 at 20:26
  • Oh, and if you never did this process before, then you probably used the pre-built binary for MSVS. Also note that you only need to build highgui; not the entire OpenCV set of libraries. – mehfoos yacoob Aug 15 '13 at 20:29