Alexander Kholodovitch
Alexander Kholodovitch

Reputation: 363

Find contour of the set of points in OpenCV

I try to find objects on image by MSER-detection from OpenCV. But function cvExtractMSER return not contours, but set of points (CvSeq), that create figure:

(1, 4), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4), ...

Area created by set of points

But I needs only points of contour:

(1, 4), (8, 4), (8, 1), (4, 1)

Highlight needed contour points

How I can find this contour?

I think, that simplest (but not fastest) way is:

Upvotes: 5

Views: 6470

Answers (3)

Dipanshu Nagar
Dipanshu Nagar

Reputation: 13

The most generalized way to get those points for any shape would be to use a convex hull on the contours. (Hull Tutorial)

But, in case you are only looking for the 4 extreme points in each direction, you can do that simply treating the contours like a NumPy array (which they are):

if c is a contour:

extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])

More on this can be found here: pyimagesearch.com "Finding Extreme Points in Contours with OpenCV"

Upvotes: 1

Rethunk
Rethunk

Reputation: 4093

One of the options in findContours() is to pass a parameter that will remove all points except end points on a straight horizontal, vertical, or diagonal line. If you create an image and draw the points you've listed, then findContours() can do the rest of the work for you.

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

http://opencv.itseez.com/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html?highlight=findcontours#findcontours

Upvotes: 1

karlphillip
karlphillip

Reputation: 93410

If I understood it correctly, you are looking for the corners of the detected object.

You can iterate through your list of countours and write a simple logic to detect the 4 corners by doing simple coordinates comparisons.

Upvotes: 0

Related Questions