Ask Your Question
2

ApproxPolygon - Close contour

asked 2015-11-29 11:43:28 -0600

diegomoreira gravatar image

updated 2017-08-09 07:23:28 -0600

I need close the contour, but i don't understand how to use the approxpolydp function.

This image below is my open contour.

image description

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2015-11-29 14:07:29 -0600

you can't do what you want by approxPolyDP.

try the code below:

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main( int, char** argv )
{
    Mat src, srcGray;

    src = imread( argv[1] );
    cvtColor(src, srcGray, COLOR_BGR2GRAY);

    srcGray = srcGray  > 127;

    vector<Point> contours;
    findNonZero( srcGray, contours );

    polylines( src, contours, false, Scalar(255,255,255) );

    imshow("Result", src);
    waitKey();
    return 0;
}

Result :

image description

edit flag offensive delete link more

Comments

Hello guy,

Thanks by the help.

Your code helped me a lot.

diegomoreira gravatar imagediegomoreira ( 2015-11-29 14:31:18 -0600 )edit

@diegomoreira keep in mind it works only if contour shape is like ∩ or U. it fails for example C shape.

sturkmen gravatar imagesturkmen ( 2015-11-29 14:45:49 -0600 )edit

How can we do it for a C Style Shape ?

PujanPaudel gravatar imagePujanPaudel ( 2017-09-20 10:20:14 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-11-29 11:43:28 -0600

Seen: 1,459 times

Last updated: Nov 29 '15