Ask Your Question
0

Inconsistencies regarding >>operator on video feed

asked 2014-02-09 11:32:56 -0600

Steve7 gravatar image

Hi I am fairly new to OpenCV and recently I encountered one really weird situation when I tested out cv::absdiff on a video feed (I want to compare the first frame of the video with each following frame).

VideoCapture capture("768x576.avi");

Mat first_frame;
Mat current_frame;
//Mat diff;

capture>>first_frame;

while( (char)keyboard != 'q' && (char)keyboard != 27 ){

    capture >> current_frame;
    //cv::absdiff(current_frame, first_frame, diff);

    imshow("first_frame", first_frame);
    imshow("current_frame", current_frame);

    keyboard = waitKey( 30 );
}

The weird thing I encountered is, you can see I only modified Mat first_frame once (putting in the first frame which isn't a black frame) but when I imshow both first_frame and current_frame on the while loop, they both "play" like a normal video which is weird since I do not modified first_frame in the while loop. Can anyone please explain why is this happening?

P/S: I solved it by cloning the first_frame into another Mat by still, why is this happening?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-09 20:06:32 -0600

updated 2014-02-10 02:22:42 -0600

New answer

After reading your comment the "inspiration" came to me... I think that your problem is that first_frame and current_frame are pointing to the same data (for better performance the opperation capture>>first_frame does not copy the data, only returns a pointer. And the data pointed by that pointer will be overwritten with the brand new frame instead of allocating new memory)

what you need to do is to copy first_frame to a local variable. Like this, for example:

VideoCapture capture("768x576.avi");

/* Initialize the windows */
namedWindow("first_frame");
namedwindow("current_frame");

Mat tmp;
Mat first_frame;
Mat current_frame;
//Mat diff;

capture>>tmp;
first_frame = tmp.clone();

while( (char)keyboard != 'q' && (char)keyboard != 27 ){

    capture >> current_frame;
    //cv::absdiff(current_frame, first_frame, diff);

    imshow("first_frame", first_frame);
    imshow("current_frame", current_frame);

    keyboard = waitKey( 30 );
}

Old answer

Might be not the source of your problem and you might be already doing this, but you need to initialize the windows where you want to display the images in advance:

VideoCapture capture("768x576.avi");

/* Initialize the windows */
namedWindow("first_frame");
namedwindow("current_frame");

Mat first_frame;
Mat current_frame;
//Mat diff;

capture>>first_frame;

while( (char)keyboard != 'q' && (char)keyboard != 27 ){

    capture >> current_frame;
    //cv::absdiff(current_frame, first_frame, diff);

    imshow("first_frame", first_frame);
    imshow("current_frame", current_frame);

    keyboard = waitKey( 30 );
}
edit flag offensive delete link more

Comments

Yeah I did include the initialization but I simplified the code when I posted it here. The problem persists even with the initialization though.

Steve7 gravatar imageSteve7 ( 2014-02-10 00:39:23 -0600 )edit

I have updated my answer, please let me know if the new suggestion solves your problem

Martin Peris gravatar imageMartin Peris ( 2014-02-10 02:23:46 -0600 )edit

Yeah I was suspecting this was the case as I have mentioned cloning solves the issue. However, thanks for confirming my suspicion, at first I don't know why it was designed this way, I guess you have a point on the performance issue.

Steve7 gravatar imageSteve7 ( 2014-02-15 10:26:02 -0600 )edit

Question Tools

Stats

Asked: 2014-02-09 11:32:56 -0600

Seen: 295 times

Last updated: Feb 10 '14