Ask Your Question
1

The contours in drawcontours is closed or not?

asked 2013-06-18 08:14:55 -0600

wuling gravatar image

updated 2013-06-18 08:17:06 -0600

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[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.

edit retag flag offensive close merge delete

Comments

1

you should use CV_CHAIN_APPROX_NONE flag

cv::findContours(hole.clone(),contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
sturkmen gravatar imagesturkmen ( 2016-09-02 09:46:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-06-18 10:15:54 -0600

berak gravatar image

drawContours() actually only draws the points, not line segments.

if you want that, try:

std::vector<cv::Point> contour_line;
cv::approxPolyDP(cv::Mat(contours[i]), contour_line, 5, true); // last arg is "closed line" 
int nc = contour_line.size();
const cv::Point * pc = &(contour_line[0]);
cv::polylines( img, &pc, &nc, 1, 0, cv::Scalar(255) );
edit flag offensive delete link more

Comments

Hi berak,in fact i find out the output of the contourarea is not extract correct. I use cvblob & Commercial Software ,the answer all is 975 ,but the contourarea is 925. so I check the countours.But i find it 's ok. so...............finally, i use cvblob to calcuate blob's area, and hole's area.

wuling gravatar imagewuling ( 2013-06-18 22:11:12 -0600 )edit

Question Tools

Stats

Asked: 2013-06-18 08:14:55 -0600

Seen: 2,757 times

Last updated: Jun 18 '13