Display webcam video in OpenCV

I installed VS2008 and can run the "camshiftdemo and lkdemo" demo codes, which are part of the opencv library. With this done, now I'm trying to run some simple codes from the Internet to get to know OpenCV. I am just trying to display video from a webcam and I am getting the following error.

The error I am getting is:

Unhandled exception in 0x5e7e3d10 (highgui200.dll) in opencv.exe file: 0xC0000005: reading access violation location 0x719b3856.

The code I'm trying to run is:

#include <cv.h>
#include <highgui.h>
void main(int argc,char *argv[])
{
    int c;
    IplImage* color_img;
    CvCapture* cv_cap = cvCaptureFromCAM(-1); // -1 = only one cam or doesn't matter
    cvNamedWindow("Video",1); // create window
    for(;;) {
        color_img = cvQueryFrame(cv_cap); // get frame
        if(color_img != 0)
            cvShowImage("Video", color_img); // show frame
        c = cvWaitKey(10); // wait 10 ms or for key stroke
        if(c == 27)
            break; // if ESC, break and quit
    }
    /* clean up */
    cvReleaseCapture( &cv_cap );
    cvDestroyWindow("Video");
}

Any help on this would be greatly appreciated.

+3
source share
2 answers

The following code compiles and works for me in VS2008 using OpenCV 2.1

#include <cv.h>
#include <highgui.h>
void main(int argc,char *argv[])
{
    int c;
    IplImage* color_img;
    CvCapture* cv_cap = cvCaptureFromCAM(0);
    cvNamedWindow("Video",0); // create window
    for(;;) {
        color_img = cvQueryFrame(cv_cap); // get frame
        if(color_img != 0)
            cvShowImage("Video", color_img); // show frame
        c = cvWaitKey(10); // wait 10 ms or for key stroke
        if(c == 27)
            break; // if ESC, break and quit
    }
    /* clean up */
    cvReleaseCapture( &cv_cap );
    cvDestroyWindow("Video");
}
+3

, "cvReleaseCapture (& cv_cap);" . . . , ESC, . .

+1

Source: https://habr.com/ru/post/1741359/


All Articles