I'm looking to fill holes inside the largest component of an binary image.
Input binary image:
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.
I also tried different techniques from different packages (found on other questions). The result is not the one I'm looking for:
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 ?