visual-c++ - cvFindContours 总是返回 0 - OpenCV

标签 visual-c++ opencv

我在我创建的一个单独的线程中调用 cvFindContours 函数来处理所有 OpenCV 工作,而另一个线程则保留用于 OpenGL 的东西。

我注意到当这段代码在一个单独的线程中执行时,我的 cvFindContours 函数总是返回 0。在主线程本身中执行时,它之前工作正常。我使用断点和 Watches 来评估值的变化。除 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 视觉 C++ 2008

编辑

痕迹

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

opencv_imgproc230d.dll!cvPointSeqFromMat(int seq_kind=20480, const void * arr=0x00000000, CvContour * contour_header=0x01a6f514, CvSeqBlock * block=0x01a6f4f4) Line 47 + 0xbd bytes 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 bytes C++

Project.exe!startOCV(void * vpParam=0x00000000) Line 267 + 0x24 bytes C++

所有这些东西都归结为 cvPointSeqFromMat 中的函数 CV_Assert( arr != 0 && contour_header != 0 && block != 0 ) 并且由于 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/

相关文章:

c++ - #include 在查找预编译头文件时跳过 -- 查找预编译头文件时文件意外结束

winforms - 在Windows Form应用程序中处理WAV文件

visual-studio-2010 - 使用opencv进行阈值化

python - 消除 OpenCV 图像大小限制

c++ - 如何在没有额外分配的情况下从 OpenCV 矩阵获取图像数据?

c++ - 使用第三方源代码时的预编译 header

c++ - 使用 operator[] 插入时不同的 std::map 大小(vc++ vs g++)

c# - 当 c# 访问 c++ dll 时尝试读取或写入 protected 内存错误

python - 填充轮廓 OpenCV 的外部

java - OpenCV,安卓 : Draw Line on bitmap