Ask Your Question
2

Editing/Understanging FindContours Points

asked 2017-02-18 19:39:00 -0600

jpistorino gravatar image

updated 2017-08-24 15:13:50 -0600

I am using OpenCV 3.1.1 with VS2012 C++ on a Win10 64 bit machine.

I am attempting to implement Xingang, et al., "Bag of Contour Fragments for Robust Shape Classification" (2014). http://dl.acm.org/citation.cfm?id=258... That approach involves editing the points of a contour to eliminate non-essential points.

I used findContours with CHAIN_APPROX_SIMPLE. That outputs an array of CV::Points. What I am hoping to understand is if, or how, the points are ordered. That is, given a list of points, how do you know which points are next to each other on the contour? Seems like once you have more than four points, a variety of connections between the points is possible.

Related to that, how does drawContours interpret the points array to decide which points to connect with lines?

If there is any documentation which describes any of this, a reference to it would be great.

Thanks

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-02-19 02:54:59 -0600

LBerger gravatar image

updated 2017-02-19 13:39:41 -0600

Yes there is a documentation. About findContours I think it is clear :

compresses horizontal, vertical, and diagonal segments and leaves only their end points. For example, an up-right rectangular contour is encoded with 4 points.

you can test using a small program :

    Mat img(300,300,CV_8UC1,Scalar::all(0)),imgColor;

    rectangle(img, Rect(100, 100, 100, 100), Scalar(255), -1);
    rectangle(img, Rect(150, 50, 100, 100), Scalar(255), -1);
    vector<vector<Point>> ctr;
    Mat hierarchy;
    findContours(img,ctr,hierarchy, RETR_LIST, CHAIN_APPROX_SIMPLE);
    cvtColor(img,imgColor,CV_GRAY2BGR);
    cout<< "#contour : "<<ctr.size()<<"\n";
    for (int i=0;i<ctr.size();i++)
    {
        cout<< "Ctr "<<i<<"\n";
        for (int j=0;j<ctr[i].size();j++)
        {
            cout <<ctr[i][j]<<"\t";
            putText(imgColor,format("%d",j), ctr[i][j], FONT_HERSHEY_SIMPLEX,1,Scalar(255,0,255));
        }
        cout<<"\n";
    }
    imshow("test", imgColor);
    waitKey();

result is :

image description

edit flag offensive delete link more

Comments

Yes, I know of those. However, unless I am missing it, there is nothing that explains how points are connected. For example, if there are five points, they can be connected in multiple ways. The resulting shape depends on the order in which the points are connected. What I am trying to figure out is if there is anything indicating what order the points should be connected, either generally or as part of drawContours.

Also, I fixed the ACM link.

jpistorino gravatar imagejpistorino ( 2017-02-19 10:53:34 -0600 )edit

They are in order. 0 connects to 1, 1 connect to 2, 2 connects to 3, n-1 connects to n, n connects to 0.

Tetragramm gravatar imageTetragramm ( 2017-02-19 15:44:27 -0600 )edit

OK. That is what I was looking for. The result is that you have to maintain the original findContours point order if you want to properly process the shape.

jpistorino gravatar imagejpistorino ( 2017-02-19 18:05:52 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2017-02-18 19:39:00 -0600

Seen: 2,472 times

Last updated: Feb 19 '17