Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

extract point sequence of edge (a not enclosed contour)

Hello,

i am trying to extract the point sequence of a single (curvy) edge, which i obtained with the CannyEdge-Filter. When I use findContours(...EXTERNAL...) i receive a enclosed contour, say for example p1->p2->p3->p4->p3->p2->p1 - but what i really want is just a "one-way-sequence" like p1->p2->p3->p4. I currently use an ugly workaround, where I detect the fraction of the extracted contours which has no reduntant points in it, but this is kind of ineffective and i wonder if there is an easier way to get what I need.

click to hide/show revision 2
edited to post the results of an offered solution

extract point sequence of edge (a not enclosed contour)

Hello,

i am trying to extract the point sequence of a single (curvy) edge, which i obtained with the CannyEdge-Filter. When I use findContours(...EXTERNAL...) i receive a enclosed contour, say for example p1->p2->p3->p4->p3->p2->p1 - but what i really want is just a "one-way-sequence" like p1->p2->p3->p4. I currently use an ugly workaround, where I detect the fraction of the extracted contours which has no reduntant points in it, but this is kind of ineffective and i wonder if there is an easier way to get what I need.

@Haris: i have tried your suggestion and got the following output:

input image:
input

contour 0:
x: 5, y: 20
x: 14, y: 20
x: 15, y: 21
x: 32, y: 21
x: 33, y: 22
x: 50, y: 22
x: 51, y: 23
x: 68, y: 23
x: 69, y: 24
x: 86, y: 24
x: 87, y: 25
x: 95, y: 25
x: 87, y: 25
x: 86, y: 24
x: 69, y: 24
x: 68, y: 23
x: 51, y: 23
x: 50, y: 22
x: 33, y: 22
x: 32, y: 21
x: 15, y: 21
x: 14, y: 20

using this code:

    Mat input(50,100,CV_8UC1);
    input.setTo(0);
    line(input, Point(5,20), Point(95,25), Scalar(255), 1);

    namedWindow( "input", CV_WINDOW_AUTOSIZE );
    imshow( "input", input );

    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( input, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

    for (vector<vector<Point>>::iterator outter = contours.begin(); outter < contours.end(); outter++)
    {
        printf("contour %d:\n", outter - contours.begin());
        for (vector<Point>::iterator inner = (*outter).begin(); inner < (*outter).end(); inner++)
        {
            Point cur = (*inner);
            printf("x: %d, y: %d\n", cur.x, cur.y);
        }
    }

See what I mean?