0

I'm looking to fill holes inside the largest component of an binary image.

Input binary image:

enter image description here

So what I'm looking for is to fill "lakes" inside the purple component, but not altering any other pixels.

I know there is several answers on stackoverflow using connectedComponentsWithStats function from opencv or with scikit-image but I cannot make it work the way I'm looking to. So please don't tag this as duplicate.

Here is what I've made using other questions' answers:

nb_components, output, stats, centroids = cv.connectedComponentsWithStats(image, connectivity=4)
sizes = stats[:, -1]

max_label = 1
max_size = sizes[1]
for i in range(2, nb_components):
    if sizes[i] > max_size:
        max_label = i
        max_size = sizes[i]

res = np.zeros(output.shape)
res[output == max_label] = res

It always modifies the rest of the pixels and is not filling all the holes.

enter image description here

I also tried different techniques from different packages (found on other questions). The result is not the one I'm looking for:

enter image description here

I'm looking to only modify the pixel of the holes and not touching anything else.

What should I change in my script to do that ?

Bryce Siedschlaw
  • 4,136
  • 1
  • 24
  • 36
Marie
  • 81
  • 7
  • How does the contour filling approach in the duplicate target not work for you? It seems like a reasonable place to start, and if you can help us understand why that's not the correct solution in this case, perhaps we can help you with a more relevant answer. – beaker Mar 14 '22 at 18:15
  • I made two examples (black and white images) using different solutions (including the duplicated target) and as you can see, a lot of pixels are modified. There is more than holes filling using this answer. I'm looking for a solution without that and my question keeps getting downvoted/duplicated/deleted. This is not friendly. I'm still learning. People don't understand that what is clear for you isn't automatically clear for the newbies. – Marie Mar 15 '22 at 13:01
  • Please include your original image and the code you use to get each of your results. I don't think people are being unfriendly, they are just trying to point you to an answer they think will help you. If that answer does not help you and you clearly explain why, then people can vote to reopen the question. I also don't see any downvotes on this question. – beaker Mar 15 '22 at 14:09
  • Hi beaker, I edited my question. Thank you for taking time with me. Sorry about saying unfriendly, I was kinda pissed off that people was shuting me down for no reason. I was doing my best to explain my problem. I hope it's better now. – Marie Mar 16 '22 at 10:48
  • It looks like your second image fills all the internal holes, so are you looking for something that will fill the concavities around the edges of the large component? You could try something like [`convexHull`](https://docs.opencv.org/3.4/d7/d1d/tutorial_hull.html), but I'm afraid it would change more pixels than you want. – beaker Mar 16 '22 at 22:28

0 Answers0