0

I'm using Skimage regionprops to find the center of objects, and then opencv to write text in the middle of the object. However, some of the objects are irregular in shape and the centroid coordinates are outside of the object. How can I get the "center" of the object when it is irregular such that the center is inside the object?

Michael S.
  • 3,050
  • 4
  • 19
  • 34
Adam Boyher
  • 36
  • 1
  • 4
  • If the centroid is inside the object (as you said), then just use the centroid. You can test if the point is inside a polygon with cv2.pointPolygonTest(). Get the contour of the object and then use this test. – fmw42 Oct 01 '21 at 16:45
  • Thanks for cv2.pointPolygonTest(), that'll be super useful. But what do I do if the centroid isn't in the polygon? How can find a point that is inside the polygon? – Adam Boyher Oct 01 '21 at 17:05
  • Compute the distance from the point to the closest point on the polygon and move the point to the closest point and then a little more. Test again and iterate if needed. – fmw42 Oct 01 '21 at 17:13
  • Intesting, thanks! – Adam Boyher Oct 01 '21 at 17:25

1 Answers1

1

What you are after is called the medoid and currently doesn't exist in scikit-image, though we are interested in adding an implementation.

Note that as of v0.18, regionprops lets you add extra properties, so if you find a good implementation you can start using it straight away.

Juan
  • 5,433
  • 21
  • 23