简体   繁体   中英

video capturing with opencv

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!

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

CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);

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 :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM