I have black and white images including unfilled contours within contours. These contours represent the edges of solids. I'd like to detect the solids (islands) and holes than fill the solids using Matlab.
I tried some methods. I can detect solids and holes. But I can not detect the solids and holes if they are inside of another solid/hole.
F.e: Lastly I tried the the method mentioned at Amro's answer in following link: Fill area between two connected components in MATLAB
But it couldn't detect the solid islands inside the holes.
In addition: All the examples I could find works fine for detecting solids on the background and detecting holes inside of those solids. Or if there are more than one object (solid) on background or more than one hole in an object like the link mentioned: Distinguish a simply connected figures?
But I'm trying to work on unfilled contours which have a hierarchical order.
An example image:
The output I want to have is:
Editing my questions and adding my previous works to clarify
What I simply want to do is to detect which areas of the image I must fill as solids. The image have many contours. And these contours are the edges of the solids and holes. Unlike many other examples my images have contours within contours...
Let's say there is a contour on the background. That means it is a solid and we should fill it. But if there is a second contour inside of the first one, that means it s a hole and it must stay unfilled. And if there is a third one inside of the second, that is again a solid and it must be filled. This goes like that...
Up to now I've tried a method nearly similar like @Huá dé ní 華得尼 wrote (His code is simpler and better than mine, so I don't write mine to talk on his code).
In this method I scan the pixels with raster scan. Each time a find a contour pixel I change the state between 0 and 1. And if the state is 0 I fill the pixels with 0 and if it's 1 i fill them with 1.
But there is a problem of this method. There stays some broken lines on the upper and lower horizontal edges of each area.
Here is my output image using the same method. It nearly the the same of @Huá dé ní 華得尼 .