Ask Your Question
0

Subsequent Webcam Frames Are Identical

asked 2013-08-11 20:31:57 -0500

Ell gravatar image

updated 2013-08-11 20:33:05 -0500

I'm coming across the problem where subtracting two subsequent webcam frames gives a completely black image - I would have thought that it would leave a non blank image when there is movement in the video stream, but I always get a blank image. I think the problem could be that the two frames are identical, but I think this is highly unlikely because the camera is (at a guess, v4l2 doesn't support FPS property) at 20 fps tops. Here is the code I'm using:

#include <iostream>
#include <opencv2/opencv.hpp>

int main() {
    cv::VideoCapture input(0);
    cv::namedWindow("last_frame", cv::WINDOW_AUTOSIZE);
    cv::namedWindow("current_frame", cv::WINDOW_AUTOSIZE);
    cv::namedWindow("difference", cv::WINDOW_AUTOSIZE);
    for(;;) {
        cv::Mat last_frame;
        input >> last_frame;
        cv::Mat current_frame;
        input >> current_frame;
        cv::Mat difference; 
        cv::subtract(current_frame, last_frame, difference);
        cv::imshow("last_frame", last_frame);
        cv::imshow("current_frame", current_frame);
        cv::imshow("difference", difference);
        if(cv::waitKey(10) >= 0) break;
    }
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-08-11 21:58:41 -0500

You have to clone the returned image (see this note on the doc). 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 );
    ...
}
edit flag offensive delete link more

Comments

Thanks! I can't believe I missed that!

Ell gravatar imageEll ( 2013-08-12 04:39:33 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-08-11 20:31:57 -0500

Seen: 247 times

Last updated: Aug 11 '13