burakim
burakim

Reputation: 482

Opencv Contour Error

I want to find contours of my image then I want to draw contour using openCV. I'm using VS 2012 and OpenCV 2.4.5 I wrote sample code about finding contours and drawing contours. Bu I stacked that horrible error :) For any help I will be appreciated

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));     
    }

}
}

My threshold and other necessary function works very well. But My program stacked at finding contour and drawcontour function. Says that:

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

Upvotes: 1

Views: 1577

Answers (1)

b_froz
b_froz

Reputation: 451

I had a similiar problem. But there are two implicit situations.

The first one is a Drawing issue, that I copied the way the official documentation contains to solve:

    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 );
    }

This worked for me for drawing different colors for each contour.


EDIT: This function "drawContours" can draw a color for a contours and all children. To understand this better, read this.


The second one is the iterative navigation on contours. For some unknow reason, the output "contours" from the "findContours(...)" function brings contours with 0 size or a very HIGH size (it's like a memory thrash, a really big number). I solved the way I use the contour using a condition:

    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));     
          }
       }
    }

I used the condition "if(contours[i].size() < 10000 && contours[i].size() > 0)" because when, in any case, we manipulate "contours[i]", where "contours[i].size()" is 0 or that big number, the program CRASHES. (that "10000" is arbitrary, and worked in my case).

Upvotes: 1

Related Questions