2011-11-10 122 views
2

我在一個單獨的線程中調用了cvFindContours函數,我創建該線程來處理所有的OpenCV工作,而另一個線程保留用於OpenGL的東西。cvFindContours總是返回0 - OpenCV

我注意到,當代碼在單獨的線程內執行時,我的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; 
} 

NOTE1:這些線必須被複制,由於在How to avoid "Video Source -> Capture source" selection in OpenCV 2.3.0 - Visual C++ 2008

環境中提到的錯誤: 的OpenCV 2.3.0 的Visual C++ 2008

EDIT

痕跡

opencv_core230d.dll!CV ::錯誤(常量CV ::例外& EXC = {...})431線C++

opencv_imgproc230d.dll! cvPointSeqFromMat(INT seq_kind = 20480,常量無效* ARR = 00000000,CvContour * contour_header = 0x01a6f514,CvSeqBlock *塊= 0x01a6f4f4)47線+ 0xbd字節C++

opencv_imgproc230d.dll! cvApproxPoly(常量無效*陣列= 00000000,整數header_size = 88,CvMemStorage *存儲= 0x017e7b40,INT方法= 0,雙參數= 3.0000000000000000,INT參數2 = 1)線703 + 0×28字節C++

Project.exe !startOCV(void *的vpParam = 00000000)線267 + 0X24字節C++

所有這些東西歸結爲cvPointSeqFromMat功能CV_Assert(arr != 0 && contour_header != 0 && block != 0)和失敗,因爲arr它需要是空的。

回答

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來計算檢測到的輪廓數量。

希望有幫助!

+0

請看看我上面添加的痕跡 – coder9

+0

不幸的是評論塊已經背叛了你。請閱讀源代碼「contours.cpp」。 '返回計數;' – rwong