5

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:

  • draw b/w image with all points (how? point-by-point?)
  • use findContours for find contours on new image
  • 1
    It's been 2 years, so you might already have the solution to your question. I'm running into the same problem. Thus I think the documentation of OpenCV that describes MSER to return the "contours" is not correct: "//runs the extractor on the specified image; returns the MSERs, // each encoded as a contour (vector, see findContours) // the optional mask marks the area where MSERs are searched for (...)" – Jim Raynor Apr 16 '14 at 20:58

3 Answers3

1

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

Rethunk
  • 3,976
  • 18
  • 32
  • 1
    "create an image and draw the points you've listed, then findContours() can do the rest of the work" Yes, I think, it's simplest way (I wrote it in bottom of post). But I don't know fast way of drawing sequence of points on image. Can you help me? – Alexander Kholodovitch May 07 '12 at 19:31
1

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"

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
0

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.

karlphillip
  • 92,053
  • 36
  • 243
  • 426
  • 1
    I think that is not good answer. What if he has different shape which needs more corners to make contour? Simple logic become not simple to write. – krzych Apr 26 '12 at 08:56
  • Perhaps karlphillip probably meant to say convex hull? – rwong Mar 04 '14 at 13:17