0

我正在使用背景減法並希望顯示內容。不知何故,由於內存異常,代碼似乎總是打破。該錯誤似乎是在cvCopy函數中。無法弄清楚。OPENCV怪異錯誤

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

你能粘貼錯誤嗎? – 2012-01-04 11:29:18

+0

爲什麼你將兩個視頻捕獲操作到同一個設備?我從來沒有測試過,但我真的懷疑cap總是返回一個NULL指針,因爲cap1正在「竊取」設備0.您能調試嗎? – 2012-01-04 15:08:51

回答

1

不是混合使用C和C++ API,我建議您儘量使用C++ API。如果您只想複製矩陣,只需使用Mat::clone()Mat::copyTo()即可。由於要使用口罩,使用copyTo成員函數是這樣的:

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

希望幫助!

+0

cvCpy在這裏不是問題,但值得注意,因爲「堅持使用C++ api」 – 2012-01-04 15:13:38

+0

@IMeMedeiros打開雙重捕獲的好處。我沒有注意到(說實話我只是看着註釋掉的「症狀」部分:)我會想象這是非常多的問題。 – mevatron 2012-01-04 15:17:07

+0

真的很感謝你的建議。我的目標是當我選擇背景爲白色表面時分離人臉(背景扣除會導致它) 應用掩碼確實使用frame.copyTo()沒有實現。相反,我會在複製的數組最後進行追蹤。 – user1026134 2012-01-04 18:06:08