2

I know how to make a closed component labeling for small structures with the help of bwlabel. However, I have now the following image:

enter image description here

And using bwlabel on this image results only in two classes, the edge - and the everything around it.

I was wondering if there is an easy solutin in matlab to get the inner part of the "circle" as one class and the outer one as another class? The border could be a third class.

The code I have so far is onyl for testing bwlabel

i = imread('apple.jpg')
labels = bwlabel(i)
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
Kev1n91
  • 3,553
  • 8
  • 46
  • 96

2 Answers2

4

@Shai's answer should be faster and easier


Easy: just make labels twice, once filling.

% load
I=rgb2gray(imread('https://i.stack.imgur.com/nnJUn.png'));
I=I(:,1:end-2); %some artifacts in the corners of the SO image

labels = bwlabel(I);
filled=imfill(I,'holes'); % fill
labels2= bwlabel(filled);
labels=labels+labels2;

enter image description here

Shai
  • 111,146
  • 38
  • 238
  • 371
Ander Biguri
  • 35,140
  • 11
  • 74
  • 120
4

Matlab's bwlabel uses 8-connect connectivity to connect neighboring pixels. Since your boundary is very thin, the diagonal connections connect inner and outer pixel resulting with a single label.
However, if you use 4-connect connectivity

 labels = bwlabel(~i, 4);

You should get you desired output.


BTW,
It is best not to use i as a variable name in Matlab.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • 2
    This would label each of the 4-connected strokes in the circle as separate regions, but the inner part of the circle and the area outside it would still be labeled as background, forming a single component. If you invert the image you'd get the correct effect: `bwlabel(~i,4)`. – Cris Luengo Mar 27 '18 at 16:11