Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 236
Sagar Gautam Avatar asked Mar 05 '26 12:03

Sagar Gautam


1 Answers

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.

like image 108
sop Avatar answered Mar 08 '26 00:03

sop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!