cvFindContours总是返回0 - OpenCV

时间:2011-11-10 18:28:43

标签: visual-c++ opencv

我在一个单独的线程中调用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

修改

痕量

  

opencv_core230d.dll!cv :: error(const cv :: Exception& exc = {...})第431行C ++

     

opencv_imgproc230d.dll! cvPointSeqFromMat (int seq_kind = 20480,const void * arr = 0x00000000,CvContour * contour_header = 0x01a6f514,CvSeqBlock * block = 0x01a6f4f4)第47行+ 0xbd字节C ++

     

opencv_imgproc230d.dll! cvApproxPoly (const void * array = 0x00000000,int header_size = 88,CvMemStorage * storage = 0x017e7b40,int method = 0,double parameter = 3.0000000000000000,int parameter2 = 1)Line 703 + 0x28字节C ++

     

Project.exe!startOCV(void * vpParam = 0x00000000)第267行+ 0x24字节C ++

所有这些内容归结为CV_Assert( arr != 0 && contour_header != 0 && block != 0 )中的cvPointSeqFromMat函数,但由于它所需的arr为空,因此失败。

1 个答案:

答案 0 :(得分:0)

您的变量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来确定检测到的轮廓数量。

希望有所帮助!