2

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:

enter image description here

The output I want to have is:

enter image description here

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í 華得尼 .

enter image description here

Community
  • 1
  • 1
furkan
  • 21
  • 4
  • @rayryeng , the example in the link you mentioned is more about to find the solid or solids and their connection type. But I can not even detect if there is another solid inside of a hole, or hierarchically more... – furkan Nov 17 '15 at 17:37
  • 2
    I see. OK then, you didn't really make yourself clear. I'll reopen. It will also help to show us what you've tried, rather than showing us what you've read. In addition, what you want to fill in and what you want to not fill in is rather ambiguous. I still don't understand how to go from the original image to the filled in one. If I'm having trouble, then any automated algorithm will most likely have trouble too. – rayryeng Nov 17 '15 at 18:54
  • 1
    @rayryeng <>, this is what most people don't understand. – Autonomous Nov 17 '15 at 21:01
  • @ParagS.Chandakkar You couldn't have said it better my friend. I can't upvote on your comment because I've run out of votes :S... but I will tomorrow! – rayryeng Nov 17 '15 at 21:03

1 Answers1

0

What about a simple solution like traversing through each row. See the following code. It is not perfect, need to handle continuing white lines. I will update if I can solve it.

clc; clear all;

img = imread('g4K3N.png');
imgb = im2bw(img);
imgb2 = imgb;

for r = 1:size(img,1)
    cc = 0;
    p = 0;

    for c = 2:size(img,2)
        if (imgb(r,c)==imgb(r,c-1))
                imgb2(r,c) = p;
                continue;
        else
            cc = cc+1;
            if (cc~=0 && mod(cc,2)==0)
                p = ~p;
                imgb2(r,c) = p;
            end
        end
    end
end

imshow(imgb2);

Output:

enter image description here

Huá dé ní 華得尼
  • 1,248
  • 1
  • 18
  • 33
  • Thanks for the answer.I've done something like yours. But I had that broken lines like your picture. I've edited my question for details. – furkan Nov 18 '15 at 15:13
  • What about trying some image processing techniques to remove the lines then, like erode and dilate. Yes, as @rayryeng told before, this seems difficult to achieve. – Huá dé ní 華得尼 Nov 19 '15 at 01:56