Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The contours in drawcontours is closed or not?

Hi all, this is my test picture:

image description

As i know, if want to calculate the area or others , the contours should be closed. But I test findcontours and drawcontours , i cann't verify. The following code is true or not. Thanks.

  Mat temp,blob;
    blob=Mat::zeros(imgIn_height,imgIn_width,CV_8UC1);
    temp=Mat::zeros(imgIn_height,imgIn_width,CV_8UC1);
    cv::findContours(hole.clone(),contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    for(int idx=0; idx<contours.size(); ++idx)    
    {       
            drawContours(blob, contours, idx, Scalar::all(255), CV_FILLED, 8, hierarchy);       
            imwrite("c:\\result.bmp",blob);

            for(int i=0;i<contours[0].size();i++)
            {
                cv::Point p=contours[idx][i];
                temp.at<uchar>(p.y,p.x)=255;
            }

    A=cv::contourArea(contours[idx]);
    }
    imwrite("c:\\temp.bmp",temp);

Finally I get:

image description---->result.bmp

image description--->Contours(temp.bmp) is not closed.

The contours in drawcontours is closed or not?

Hi all, this is my test picture:

image description

As i know, if want to calculate the area or others , the contours should be closed. But I test findcontours and drawcontours , i cann't verify. The following code is true or not. Thanks.

  Mat temp,blob;
    blob=Mat::zeros(imgIn_height,imgIn_width,CV_8UC1);
    temp=Mat::zeros(imgIn_height,imgIn_width,CV_8UC1);
    cv::findContours(hole.clone(),contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    for(int idx=0; idx<contours.size(); ++idx)    
    {       
            drawContours(blob, contours, idx, Scalar::all(255), CV_FILLED, 8, hierarchy);       
            imwrite("c:\\result.bmp",blob);

            for(int i=0;i<contours[0].size();i++)
i=0;i<contours[idx].size();i++)
            {
                cv::Point p=contours[idx][i];
                temp.at<uchar>(p.y,p.x)=255;
            }

    A=cv::contourArea(contours[idx]);
    }
    imwrite("c:\\temp.bmp",temp);

Finally I get:

image description---->result.bmp

image description--->Contours(temp.bmp) is not closed.