Ask Your Question

Revision history [back]

You have to clone the returned image (see this note. The operator>> returns a reference, therefore, last_frame and current_frame reference the same image. I know it says it's for C version, but it also valid for C++. We have discuss this on this forum, but I can't find the post anymore. By the way, I've added the check of image, to avoid exception.

cv::Mat frame;
cv::Mat last_frame;
cv::Mat current_frame;
cv::Mat difference;
for(;;) {
    input >> frame;
    if( !frame.data )
        break;
    frame.copyTo( last_frame );
    input >> frame;
    if( !frame.data )
        break;
    frame.copyTo( current_frame );
    cv::subtract( last_frame, current_frame, difference );
    ...
}