Ask Your Question

How to remove small line segments from contour?

asked 2014-10-10 09:18:52 -0500

HasanGhaforian gravatar image

updated 2015-10-19 15:16:44 -0500

Is there a way to remove small line segments from a contour?

For example,in this image,largest contour is specified by green color and it's approximation is specified by blue color:

image description

As contour is a set of Points,I guess we can do somethings to remove segments of contours that are in red circles.For example by detecting and removing small lines or small sub contours or another way.But I do not know how I can do it.Please remember I want to remove them after finding contour and not before that.Do you know how I can remove them?Or any idea?

edit retag flag offensive close merge delete


@HasanGhaforian. Can you post original image?

supra56 gravatar imagesupra56 ( 2020-01-20 07:06:44 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2014-10-11 03:48:16 -0500

The easiest approach to test would be a simple erode/dilate. Just draw your green contour as a (white) filled pylygon on a black background and call erode for some iterations. This will shrink your goal rectangle but also remove the additional lines. If you then call dilate for the same number of iterations, the rectangle will be (approximately) the same size as before, but without your additional lines.

edit flag offensive delete link more


at first , fill closed loop, then use erode/dilate

wuling gravatar imagewuling ( 2014-10-12 10:43:00 -0500 )edit

answered 2014-10-11 12:59:41 -0500

gfx gravatar image

on opencv4android not sure but I think there is myvector.push_back (myvectorpoint); function ..... I suggest to use minarearect function for find 4point ... if you need store it in a vector ..... than draw the line around your poligon image (4 line poligon)..... using function line(IMG, point[0], point[1], scalar(XXX), 3,8,1)....for every poligon side.

edit flag offensive delete link more

answered 2014-10-10 12:40:43 -0500

aliavci gravatar image

updated 2014-10-19 02:56:39 -0500

Make sure your parameters aren't giving you excess lines by calling OpenCV's convexHull function found here:

After this to find corners, there's a whole complex manual method of running all the lines through many while loops and calculating their slopes to detect whether they are horizontal or vertical. check your vertical lines separately for highest and lowest y values. Make sure you do this separately for vertical lines on specific x ranges to differentiate between right line and left line

edit flag offensive delete link more


@aliavci Can you add more details?In opencv4android,how I can achieve corners?

HasanGhaforian gravatar imageHasanGhaforian ( 2014-10-10 14:11:34 -0500 )edit

Question Tools


Asked: 2014-10-10 09:18:52 -0500

Seen: 3,806 times

Last updated: Oct 19 '14