Contents

Labeling - Lab 4

% Read the an image and display. Note the image is type logical ie binary

BW = imread('text.png');
figure(1)
imshow(BW);

Label the image.

Each indiviual object is given a unique value

figure(2)
lab=bwlabel(BW);
imshow(lab)
colorbar;

% We can also show this as a false color map

figure(3)
imshow(label2rgb(lab));
title('False Colour Map');

Another way

The labelled image is not that useful in practice so lets delete it

clear lab

% Instead use this command

CC = bwconncomp(BW);

% Each object is stored seperatly is a structure CC

CC
CC = 

    Connectivity: 8
       ImageSize: [256 256]
      NumObjects: 88
    PixelIdxList: {1x88 cell}

Construct the labelled image

% load the target image

BW=imread('target.png');

% threshold to convert to a binary image, and perform labelling

BW=im2bw(BW,0.1);
CC = bwconncomp(BW);

% create an array filled with zeros of the same size

BW2=zeros(CC.ImageSize);

%cycle through each object, adding it into array BW2

for p=1:CC.NumObjects  %loop through each image
    BW2(CC.PixelIdxList{p}) = p; %set the image
    figure(4)
    imshow(BW2,[0 CC.NumObjects]) %display with fixed intestiy range
    pause(.5)  %pause for 0.5 seconds
end

Find the cirles

 s  = regionprops(BW, 'Eccentricity');
 BW3=zeros(CC.ImageSize);
for p=1:CC.NumObjects  %loop through each image
    if s(p).Eccentricity < 0.3
        BW3(CC.PixelIdxList{p}) = 1; %set the image
    end

end
figure(5)
imshow(BW3,[0 1]) %display with fixed intestiy range

Now for some morphological operations

 % Create a structure array

 se=strel('disk',11)
 se

 figure(6)
 imshow(imerode(BW,se))
 title('Erode')

 figure(7)
 imshow(imdilate(BW,se))
 title('Dilate')

 % Lets try a different structure array

 se=strel('square',11)
 se

 figure(8)
 imshow(imerode(BW,se))
 title('Erode 2')

 figure(9)
 imshow(imdilate(BW,se))
 title('Dilate 2')
 
se =
 
Flat STREL object containing 357 neighbors.
Decomposition: 4 STREL objects containing a total of 32 neighbors

Neighborhood:
  Columns 1 through 13

     0     0     0     0     0     0     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     1     1     1     1     1     1     1

  Columns 14 through 21

     1     1     0     0     0     0     0     0
     1     1     1     0     0     0     0     0
     1     1     1     1     0     0     0     0
     1     1     1     1     1     0     0     0
     1     1     1     1     1     1     0     0
     1     1     1     1     1     1     1     0
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     0
     1     1     1     1     1     1     0     0
     1     1     1     1     1     0     0     0
     1     1     1     1     0     0     0     0
     1     1     1     0     0     0     0     0
     1     1     0     0     0     0     0     0

 
 
se =
 
Flat STREL object containing 357 neighbors.
Decomposition: 4 STREL objects containing a total of 32 neighbors

Neighborhood:
  Columns 1 through 13

     0     0     0     0     0     0     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1     1     1
     0     1     1     1     1     1     1     1     1     1     1     1     1
     0     0     1     1     1     1     1     1     1     1     1     1     1
     0     0     0     1     1     1     1     1     1     1     1     1     1
     0     0     0     0     1     1     1     1     1     1     1     1     1
     0     0     0     0     0     1     1     1     1     1     1     1     1
     0     0     0     0     0     0     1     1     1     1     1     1     1

  Columns 14 through 21

     1     1     0     0     0     0     0     0
     1     1     1     0     0     0     0     0
     1     1     1     1     0     0     0     0
     1     1     1     1     1     0     0     0
     1     1     1     1     1     1     0     0
     1     1     1     1     1     1     1     0
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     0
     1     1     1     1     1     1     0     0
     1     1     1     1     1     0     0     0
     1     1     1     1     0     0     0     0
     1     1     1     0     0     0     0     0
     1     1     0     0     0     0     0     0

 
 
se =
 
Flat STREL object containing 121 neighbors.
Decomposition: 2 STREL objects containing a total of 22 neighbors

Neighborhood:
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1

 
 
se =
 
Flat STREL object containing 121 neighbors.
Decomposition: 2 STREL objects containing a total of 22 neighbors

Neighborhood:
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1
     1     1     1     1     1     1     1     1     1     1     1

 

Combining the two methods

 tmp=imdilate(BW,se);
 figure(10)
 imshow(imerode(tmp,se))
 title('Dilate then Erode')

 % this is close to the oringal. By combining these we can do things like
 % fill holes in our mask, remove small objects and noise. Erode is useful
 % for seperating objects that are close together.