Ask Your Question

Revision history [back]

how to find the REAL width and height of contours

there is a image of 3 shapes image description

if i use the code blew ,i get the result

int main( int argc, char** argv )
    //read the image
    Mat img = imread("e:/sandbox/leaf.jpg");
    Mat bw;
    bool dRet;

    cvtColor(img, bw, COLOR_BGR2GRAY);
    //morphology operation  
    threshold(bw, bw, 150, 255, CV_THRESH_BINARY);
    //find and draw contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(bw, contours, hierarchy, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    for (int i = 0;i<contours.size();i++)
        RotatedRect minRect = minAreaRect( Mat(contours[i]) );
        Point2f rect_points[4];
        minRect.points( rect_points ); 
        for( int j = 0; j < 4; j++ )
            line( img, rect_points[j], rect_points[(j+1)%4],Scalar(255,255,0),2);
    return 0;

image description

but ,in fact ,the contour 1 and contour 2 which i fingure out in RED do not get the right widht and height.

what i want should be this

image description

i did't find the appropriate function or any Doc from Opencv to do this work It's been bothering me for days any help will be appreciate!