簡體   English   中英

OpenCV輪廓錯誤

[英]Opencv Contour Error

我想找到圖像的輪廓,然后使用openCV繪制輪廓。 我使用的是VS 2012和OpenCV 2.4.5,我編寫了有關查找輪廓和繪制輪廓的示例代碼。 卜我堆積了那個可怕的錯誤:)對於任何幫助,我將不勝感激

void MyClass::findContoursAndDraw(cv::Mat image,int b,int g,int r)
{
findContours(image,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
for(int i=0;i<contours.size();i++)
{
    int size=cv::contourArea(contours[i]);
    if(size>500)
    {
        printf("%i \n",size);
        drawContours(originalTemp,contours,i,cv::Scalar(b,g,r),2,8);        
    }

}

}

void MyClass::findContoursAndDrawFilled(cv::Mat image,int b,int g,int r)
{
findContours(image,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
for(int i=0;i<contours.size();i++)
{
    int size=cv::contourArea(contours[i]);
    if(size>3000)
    {
        printf("%i \n",size);
        drawContours(originalImg,contours,i,cv::Scalar(b,g,r));     
    }

}
}

我的閾值和其他必要的功能效果很好。 但是我的程序堆積在尋找輪廓和drawcontour函數上。 說:

 Unhandled exception at 0x00B3A52A (opencv_imgproc245d.dll) in OpencvTest.exe: 
 0xC0000005: Access violation reading location 0xCDCDCDCD

我有一個類似的問題。 但是有兩種隱含情況。

第一個是繪圖問題,我復制了官方文檔包含的解決方法:

    findContours( src, contours, hierarchy, CV_RETR_CCOMP, 
                 CV_CHAIN_APPROX_SIMPLE );
    // iterate through all the top-level contours,
    // draw each connected component with its own random color
    int idx = 0;
    for( ; idx >= 0; idx = hierarchy[idx][0] )
    {
      Scalar color( rand()&255, rand()&255, rand()&255 );
      drawContours( dst, contours, idx, color, CV_FILLED, 8, hierarchy );
    }

這為我為每個輪廓繪制不同的顏色而工作。


編輯:此函數“ drawContours”可以為輪廓和所有子項繪制顏色。 為了更好地理解,閱讀


第二個是輪廓上的迭代導航。 由於某些未知原因,“ findContours(...)”函數的輸出“ contours”帶來了大小為0或非常大的輪廓(這就像一個內存重擊,一個很大的數字)。 我使用條件解決了使用輪廓的方式:

    for(int i=0;i<contours.size();i++)
    {
    if(contours[i].size() < 10000 && contours[i].size() > 0)
       {
       int size=cv::contourArea(contours[i]);
       if(size>3000)
          {
        printf("%i \n",size);
        drawContours(originalImg,contours,i,cv::Scalar(b,g,r));     
          }
       }
    }

我使用條件“ if(contours [i] .size()<10000 && Silhouettes [i] .size()> 0)”)是因為在任何情況下,當我們操縱“ contours [i]”時,其中“ contours [ i] .size()“為0或那個大數字,程序將崩潰。 (“ 10000”是任意的,並且在我的情況下有效)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM