Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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?