0

I am new to opencv (python) and don't really know how to tackle my new task.

I have several images (binarized) and masks for them. I want to extract all Connected Components of the original image that are masked and see their shapes (bounding boxes). I'm mainly interested in their length to height ratio. I'd also like to get a mean (or better: median?) for those, because I'd like to analyse them.

I played around with cv2.connectedComponentsWithStats(), but I can't seem to get the information I want with it. The documentation sadly also didn't help me.

So: Is there a way to get all desired CCs in (i.e.) an array, where they have their location and shape listed? That would be tremendously helpful!

(Also I have quite a few of those images and would like to get a good average of all of them. Is there a way to do this for a whole folder full of images?)

Lukas W.
  • 43
  • 7
  • It seems like you actually want contours, not connected components (contours are just a list of points that outline each component, like a polygon). Check out `findContours()`! – alkasm Sep 17 '19 at 22:57
  • I'm unclear on what stats you're looking for. What do you want returned as the shape of the connected component? Are you looking for a pixel list, a mask, whether it's a square or a triangle? Location is given by both centroid and bounding box, or you can look at the labeled image. As far as mean, the mean of a binary component (with no holes) is 1. – beaker Sep 18 '19 at 16:16
  • @beaker I'd like the bounding boxes, yes. Maybe I was unclear: I'd like to know the ratio of length to height and would like a mean of this ratio over all selected components. So I want something like: "Component A is 75 (rows) by 450 (columns)." – Lukas W. Sep 18 '19 at 23:05
  • The [`stats`](https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#gac7099124c0390051c6970a987e7dc5c5) return variable for [`cv.connectedComponentsWithStats`](https://docs.opencv.org/3.4/d3/dc0/group__imgproc__shape.html#gae57b028a2b2ca327227c2399a9d53241) includes `cv.CC_STAT_LEFT`, `cv.CC_STAT_TOP`, `cv.CC_STAT_WIDTH` and `cv.CC_STAT_HEIGHT`. Is that what you're looking for? – beaker Sep 19 '19 at 20:02

0 Answers0