我在另一个线程中调用cvFindContours
函数,该线程是我创建的,用于处理所有OpenCV工作,而另一个线程则保留用于OpenGL。
我注意到当在单独的线程中执行此代码时,我的cvFindContours
函数始终返回0。在主线程本身中执行之前,它运行良好。我使用断点和监视来评估值更改。除contourCount
(值:0)外,其他所有(变量)都得到值。
有什么线索吗?
// header includes goes here
CvCapture* capture = NULL;
IplImage* frame = NULL;
IplImage* image;
IplImage* gray;
IplImage* grayContour;
CvMemStorage *storage;
CvSeq *firstcontour=NULL;
CvSeq *polycontour=NULL;
int contourCount = 0;
DWORD WINAPI startOCV(LPVOID vpParam){
capture = cvCaptureFromCAM(0); // NOTE 1
capture = cvCaptureFromCAM(0);
frame = cvQueryFrame(capture);
image = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U,3);
gray = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U,1);
grayContour = cvCreateImage(cvGetSize(image), IPL_DEPTH_8U,1);
storage = cvCreateMemStorage (0);
firstcontour=NULL;
while(1){
frame = cvQueryFrame(capture);
cvCopy(frame,image);
cvCvtColor(image,gray,CV_BGR2GRAY);
cvSmooth(gray,gray,CV_GAUSSIAN,3);
cvThreshold (gray, gray, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
cvNot(gray,gray);
cvCopy(gray,grayContour);
contourCount=cvFindContours (grayContour, storage, &firstcontour, sizeof (CvContour),
CV_RETR_CCOMP);
polycontour=cvApproxPoly(firstcontour,sizeof(CvContour),storagepoly,CV_POLY_APPROX_DP,3,1); // Error starts here (Pls refer to stack trace)
}
// goes on...
}
int main(int argc, char** argv){
DWORD qThreadID;
HANDLE ocvThread = CreateThread(0,0,startOCV, NULL,0, &qThreadID);
initGL(argc, argv); //some GL intitialization functions
glutMainLoop(); // draw some 3D objects
CloseHandle(ocvThread);
return 0;
}
注意1:由于How to avoid "Video Source -> Capture source" selection in OpenCV 2.3.0 - Visual C++ 2008中提到的错误,这些行必须重复环境:
OpenCV 2.3.0
Visual C++ 2008
编辑
跟踪
所有这些东西都归结为
CV_Assert( arr != 0 && contour_header != 0 && block != 0 )
中的cvPointSeqFromMat
函数,并且由于所需的arr
为空而失败。 最佳答案
您的变量contourCount
没有按照您认为的去做。从contours.cpp
源文件:
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: cvFindContours
// Purpose:
// Finds all the contours on the bi-level image.
// Context:
// Parameters:
// img - source image.
// Non-zero pixels are considered as 1-pixels
// and zero pixels as 0-pixels.
// step - full width of source image in bytes.
// size - width and height of the image in pixels
// storage - pointer to storage where will the output contours be placed.
// header_size - header size of resulting contours
// mode - mode of contour retrieval.
// method - method of approximation that is applied to contours
// first_contour - pointer to first contour pointer
// Returns:
// CV_OK or error code
// Notes:
//F*/
您正在获得
CV_OK == 0
,这意味着它已成功运行。 cvFindContours
不返回找到的轮廓数量。它只是让您知道它是否失败。您应该使用CvSeq* first_contour
找出检测到的轮廓数量。希望有帮助!
关于visual-c++ - cvFindContours始终返回0-OpenCV,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8084461/