Sagar Gautam
Sagar Gautam

Reputation: 9359

How to detect polygon in binary image

I want to detect existence of continuous or closed polygon in binary image. The image may contain polygon of variable number of edges and also there may be no polygon at all.

Here is the positive case:

enter image description here

And,

this one is negative case:

enter image description here

Is there any method in image processing or some algorithm to detect continuous polygon ?

Any kind of help is appreciated.

Upvotes: 3

Views: 3440

Answers (4)

sop
sop

Reputation: 3625

If you need to detect both concave and convex polygons, then I can suggest you another way to do it, one has already have been told by @mdh

  1. first, you need to find the contour (cv2.findContours)
  2. then compute the area of the contour (cv2.contourArea)
  3. compute the area of the black area in the binary image (compute the number of black pixels in the binary image, openCv has a countNonZero function, that does it for you)
  4. compare the 2 values, if they are close (e.g. their ratio is close to 1, or greater than a value, you should chose it by testing on more images), then the contour you found is closed, so it may be a polygon

Of course there may be some errors in self-intersecting polygons, and/or if there are more than one contour in an image, but this may be fixed by a segmentation picking just the bounding box of the contour and apply the algo on each one.

Upvotes: 2

mdh
mdh

Reputation: 5563

You may wanna look into shapely.geometry.Polygon, in particular the is_valid method.

Upvotes: 2

Jeru Luke
Jeru Luke

Reputation: 21203

I hope you know how to find contours.

Using cv2.isContourConvex(contour) you can identify whether the detected contour is convex or not. By convex, I mean whether the convex is closed or not. You will be able to separate closed and open contours using this function, which return a boolean value True or False.

After separating out the contours you go on to perform further analysis deciding whether the contour has edges or not.

Upvotes: 2

Ibrahim
Ibrahim

Reputation: 305

I had a tool to detect region in image and some properties belonging to that region. https://github.com/mribrahim/Blob-Detection

Think that, if it is a polygon , it will be a closed region (positive case).

If it is not closed,it will be a line (negative case).

You can find regions properties with the blob detection tool, I mentioned. And you can use some properties like eccentricity, solidity to distinguish positive and negative cases.

For example; negative case eccentiricity will be closed to 0, because of it is a line

Upvotes: 1

Related Questions