我正在使用背景減法並希望顯示內容。不知何故,由於內存異常,代碼似乎總是打破。該錯誤似乎是在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;
}
你能粘貼錯誤嗎? – 2012-01-04 11:29:18
爲什麼你將兩個視頻捕獲操作到同一個設備?我從來沒有測試過,但我真的懷疑cap總是返回一個NULL指針,因爲cap1正在「竊取」設備0.您能調試嗎? – 2012-01-04 15:08:51