0

I am using Background Subtraction and want to display the contents. Somehow the code seems to break all the time due to a memory exception. The error seems to be in cvCopy function. Can't figure it out.

#include "cv.h"
#include "highgui.h"
#include "opencv2\core\operations.hpp"
#include "opencv2\core\core.hpp"
#include "opencv2\core\types_c.h"
#include "opencv\cxcore.h"
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int, char**)
{
    bool flag=0;
    VideoCapture cap(0); // open the default camera
    VideoCapture cap1(0);
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat gray,bg,result,frame,result1,final,frame1;
    //CvMemStorage*     contours = NULL;

    cap>>frame;
    cvtColor(frame,bg,CV_BGR2GRAY);

    namedWindow("GRAY",1);

    for(;;)
    {
        //final = Mat::zeros(mGreenScale.rows, mGreenScale.cols, CV_8UC3);
        cap >> frame; // get a new frame from camera
        cap1 >> frame1;
        cvtColor(frame, gray, CV_BGR2GRAY);
        absdiff(gray,bg,result);
        threshold(result,result1,50,255,THRESH_BINARY);
        //cvCopy(const CvArr* src, CvArr* dst, const CvArr* mask=NULL)¶
        //cvCopy(&frame1, &final, &result1);
        //findContours(result1,contours, ;CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
        //drawContours(final,contours,-1,CV_RGB(0,255,0));
        //imshow("GRAY",result1);
        //imshow("GRAY", result);
        imshow("GRAY1",final);

        if(flag)
        {
            imshow("BG",bg);
        }
        //if(waitKey(0)==27) break;
        if(waitKey(1)==32) 
        {
            cvtColor(frame,bg,CV_BGR2GRAY);
            flag=!flag;
        }
        if(waitKey(1)==27) 
        {
            break;
        }
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
user1026134
  • 241
  • 2
  • 4
  • 8
  • can you paste the error? – Jav_Rock Jan 04 '12 at 11:29
  • Why are you oppening two video captures to the same device? I've never tested it, but I really suspect that cap is always returning a NULL pointer because cap1 is "stealing" device 0. Can you debug it please? – Ian Medeiros Jan 04 '12 at 15:08

1 Answers1

1

Instead of mixing the C and C++ APIs I would recommend you stick to the C++ API where possible. If you merely want to copy a matrix, just use either Mat::clone() or Mat::copyTo(). Since you want to use a mask, use the copyTo member function like this:

Mat final;
frame1.copyTo(final, result1);

Hope that helps!

mevatron
  • 13,911
  • 4
  • 55
  • 72
  • cvCpy is not the problem here, but deserves an up because of the "stick with the c++ api" – Ian Medeiros Jan 04 '12 at 15:13
  • @IanMedeiros Good point about the double capture opens. I didn't notice that (to be honest I was just looking at the commented out "symptom" sections :) I would imagine that's very much the problem. – mevatron Jan 04 '12 at 15:17
  • Really appreciate the advice. My aim is to isolate people's faces when I choose the background to be a white surface (background subtraction will lead to it) Applying the mask does using frame.copyTo() does not achieve that. Instead, I get trailing in the copied array final. – user1026134 Jan 04 '12 at 18:06
  • Your last comment makes no sense to me at all. – Ian Medeiros Jan 05 '12 at 15:13