0
votes

Actually, I want five external bounding boxes for the "white" pixels on the following binary image. Desired zones are highlighted with red color.

To get 5th bounding box I'd dilate or blur it. However, dilation will merge zone 3 with zones 1 and 2, so I'll get a bounding box which covers almost entire image. (If I don't dilate or blur it, then cv::findContours + cv::boundingRect will produce a big number of small rectangles.)

In other words, I want only "big enough" bounding boxes.

Binary image

It's just a sample pattern. Positions of the zones may vary. Is there a way to solve the problem in a general way?

1
If you knew where bounding box 5 was, it is simple to do do the dilation on just that rect. But to find it you first need to do the dilation. Sorry, I can't see past this chicken and egg problem. Perhaps if there are lots of small contours close to each other you could merge them somehow. - Bull

1 Answers

1
votes

Dilation is done at a per-pixel basis, without regard for the size of the component to which the pixel belongs.

If you want to apply dilation only to small blobs, then you need to remove big blobs before applying the dilation.

So, extract all contours with findContours, then store all contours that are 'big enough' in a list, and paint them black in your source image. Then dilate the modified source and extract the remaining contours.

Note that to get the correct size of the boundingBox, what you probably want is morphological closing (dilation followed by the same amount of erosion), instead of dilation only.