Ask Your Question
0

OpenCV: Background subtracting issue when one `VideoCapture` instance is used

asked 2014-07-12 07:37:53 -0600

yohanrw gravatar image

Please have a look at the below code

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

using namespace cv;
using namespace std;

int main()

{
    cv::VideoCapture cam1,cam2;
    cam1.open(0);
    //cam2.open(0);

    Mat im,im2;

    cam1>>im;
    cam1>>im2;

    while(true)
    {
        cam1>>im;

        for(int i=0;i<15000;i++)
        {


        }
        cam1>>im2;

        Mat im3 = im2-im;

        imshow("video",im3);

        if(waitKey(30)>=0)
        {
            break;
        }

    }
    waitKey(0); 
}

I am trying to identify the difference (in other terms, motion) by subtracting the images. However what I get is a 100% blank screen. If I use 2 VideoCapture instances capture frames and load them to im and im2, then it works. But I must not use 2 VideoCapture instances, I must only use 1. what have I done wrong here?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-07-12 09:32:10 -0600

The pointer returns by the VideoCapture object can be used directly, you need to copy it, otherwise im and im2 refer to the same memory zone (that's why the result is black). Make a copy, like this:

cam1 >> im;
im.copyTo( im2 );
cam1 >> im;
im.copyTo( im3 ); // not really needed in that case
Mat im4 = im2 - im3;
edit flag offensive delete link more

Comments

Thank you. I appreciate your help.

yohanrw gravatar imageyohanrw ( 2014-07-13 02:39:04 -0600 )edit

Question Tools

Stats

Asked: 2014-07-12 07:37:53 -0600

Seen: 150 times

Last updated: Jul 12 '14