Why is this while loop breaking while reading an avi file in opencv?

0
int main(int argc, char* argv[]) {
ofstream file;
file.open("Motion.dat");
int frame_number = 0;
CvCapture* capture = cvCreateFileCapture("Cricketc1.avi");
CvCapture* capture1 = cvCreateFileCapture("Cricketc1.avi");

IplImage* imgsize  = NULL;
IplImage *img1     = NULL;
IplImage *img2     = NULL;
IplImage *vidFrame = NULL;
IplImage *imggray1 = NULL;
IplImage *imggray2 = NULL;
IplImage *imggray3 = NULL;

cvNamedWindow("Video", 0);
cvNamedWindow("Video1", 0);
imgsize = cvQueryFrame(capture1);
assert(imgsize);
CvSize sz = cvGetSize(imgsize);
cvReleaseCapture(&capture1);
imggray1 = cvCreateImage(sz, IPL_DEPTH_8U, 1);
imggray2 = cvCreateImage(sz, IPL_DEPTH_8U, 1);
imggray3 = cvCreateImage(sz, IPL_DEPTH_8U, 1);

while (true) {
    frame_number++;
    img1 = cvQueryFrame(capture);
    if(img1->imageData == NULL)
        break;
    cvCvtColor(img1, imggray1, CV_RGB2GRAY);

    img2 = cvQueryFrame(capture);
    if(img2->imageData == NULL)
        break;
    cvCvtColor(img1, imggray1, CV_RGB2GRAY);

            cvAbsDiff( imggray1, imggray2, imggray3 );
            CvScalar sumDiff = cvSum (imggray3);
    cout << sunDiff.val[0] << sunDiff.val[1] << sunDiff.val[2] << endl;
    cvWaitKey(40);
}
cvReleaseCapture(&capture);
cvDestroyAllWindows();
file.close();
system("Pause");
return 0;
}

There are a total of 1251 frames in the video @ 25 fps.

But the loop breaks at frame_number equal to 625, at line if(img2->imageData == NULL).

This error mainly comes up if I do any computation in between the while loop. A simple cvShowImage() will work just fine, but any other manipulation around it causes this error to show up.

This is the error that comes up after that : Unhandled exception at 0x00221de7 in getMotion2.exe: 0xC0000005: Access violation reading location 0x00000044.

What is the problem ?

visual-studio-2010
opencv
asked on Stack Overflow Dec 26, 2012 by Animesh Pandey • edited Dec 27, 2012 by Animesh Pandey

1 Answer

0

You are reading image twice from the same cvCapture* in a loop: img1 = cvQueryFrame(capture); and img2 = cvQueryFrame(capture);. If you change the second line to img2 = cvQueryFrame(capture2); it should work fine.

answered on Stack Overflow Dec 26, 2012 by cyriel

User contributions licensed under CC BY-SA 3.0