OPENCV奇怪的错误

时间:2012-01-03 18:56:35

标签: image-processing opencv webcam out-of-memory background-subtraction

我正在使用背景减法并想要显示内容。不知何故,由于内存异常,代码似乎一直处于中断状态。错误似乎在cvCopy函数中。无法理解。

#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;
}

1 个答案:

答案 0 :(得分:1)

我建议您尽可能坚持使用C ++ API,而不是混合使用C和C ++ API。如果您只想复制矩阵,只需使用Mat::clone()Mat::copyTo()即可。由于您要使用掩码,请使用copyTo成员函数,如下所示:

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

希望有所帮助!