简体   繁体   中英

Error while finding differences in frames from camera

int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    Mat current_frame;
    Mat previous_frame;
    Mat result; 
    Mat frame;

    //cap.open(-1);
    if (!cap.isOpened()) {
        //cerr << "can not open camera or video file" << endl;
        return -1;
    }

    while(1)
    {
        cap >> current_frame;
        if (current_frame.empty())
            break;

        if (! previous_frame.empty())  {
            // subtract frames
            subtract(current_frame, previous_frame, result);
        }


        imshow("Window", result);
        waitKey(10);

        frame.copyTo(previous_frame); 
    }
}

When i run this program to subtract current frame from the previous frame and then show the resultant frame , it show me this error while start executing

Unhandled exception at 0x755d812f in WK01.exe: Microsoft C++ exception: cv::Exception at memory location 0x001fe848..

And i want to apply the same thing on recorded video

I think the problem is with previos_frame . You assign value to previous_frame only at the and of the loop. I think it might be empty at the start of the while loop, so the

if (! previous_frame.empty())  {
        // subtract frames
        subtract(current_frame, previous_frame, result);
    }

block will not executed.

previous_frame also must be the same size as current_frame when subtracting.

This code(the subtract method) should determine the size of result , what You'd like to show at the following line.

in the 1st frame, result is empty !

imshow("Window", result); // this will crash

also, you're copying the empty frame Mat to previous_frame , that should be current_frame instead, no ?

try like:

   if (! previous_frame.empty())  {
       // subtract frames
       subtract(current_frame, previous_frame, result);
       imshow("Window", result); 
   }
   waitKey(10);
   current_frame.copyTo(previous_frame); 
}

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