# label two objects that share part of the same boundary separately

1 Ansicht (letzte 30 Tage)
Joy am 3 Aug. 2022
Kommentiert: Joy am 3 Aug. 2022
I have a matrix like this:
m = [0,1,1,1,0;1,0,0,0,1;0,1,1,1,0;0,1,0,1,0;0,0,1,0,0];
And I want to label the interior separately, which results in a matrix like this:
m1 = [0,1,1,1,0;1,2,2,2,1;0,1,1,1,0;0,1,3,1,0;0,0,1,0,0];
But I'm not sure how to do that. Any suggestion will be appreciated! Thank you!
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Steve Eddins am 3 Aug. 2022
m = [0,1,1,1,0;1,0,0,0,1;0,1,1,1,0;0,1,0,1,0;0,0,1,0,0]
m = 5×5
0 1 1 1 0 1 0 0 0 1 0 1 1 1 0 0 1 0 1 0 0 0 1 0 0
Fill the holes. From your example, it looks like the background connectivity should be 4.
filled_holes = imfill(m,4,'holes')
filled_holes = 5×5
0 1 1 1 0 1 1 1 1 1 0 1 1 1 0 0 1 1 1 0 0 0 1 0 0
The pixels that were added are the holes.
hole_pixels = filled_holes & ~m
hole_pixels = 5×5 logical array
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
Label the hole pixels.
L = bwlabel(hole_pixels,4)
L = 5×5
0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0
Add the labels (incremented by one) to the original matrix.
m_out = m;
m_out(hole_pixels) = L(hole_pixels) + 1
m_out = 5×5
0 1 1 1 0 1 2 2 2 1 0 1 1 1 0 0 1 3 1 0 0 0 1 0 0
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Joy am 3 Aug. 2022

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Ground Truth Labeling finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by