DigitalImageProcessing BorderDetectionAndLabeling
DigitalImageProcessing BorderDetectionAndLabeling
TEL 432 E
DIGITAL IMAGE
PROCESSING
-Homework 1-
Batuhan Osmanoglu
040010250
Border Detection And Labeling using 4-Neighborhood
4-Neighborhood:
In this concept every cell can be connected with its 4 neighbors. These neighbors
might be located on the left, right hand sides and above or below the pixel itself.
To employ this concept and give unique labels to different group of neighbor pixels a
MatLab code might be used as given below:
%Simple Detect
clc;
colormap(bone);
%Create Image Array 8 X 13
Im=[ 0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,1,0,0,0,0,0,0,1,0,1,0;
0,0,0,0,0,0,0,0,0,0,0,0,0;
0,1,1,1,0,1,1,1,0,1,0,1,0;
0,0,1,0,0,0,1,0,0,1,0,1,0;
0,0,1,0,0,0,1,0,0,1,0,1,0;
0,1,1,1,0,0,1,0,0,1,1,1,0;
0,0,0,0,0,0,0,0,0,0,0,0,0;
];
%Check Connectivitiy, If an unconnected pixel is found, create a new label!
%lc=2; %label color, Actually no need to set this up. Just to be on the safe side.
lcmax=2;
[nr,nc]=size(Im); %number of rows and columns
im_out=Im;
for r=1:nr-1
for c=1:nc-1
if im_out(r,c)==1
lcmax=lcmax+1;
%set a lcmax value to achieve label uniqueness...lcmax=maximum label value ever used
lc=lcmax;
im_out(r,c)=lc;
elseif im_out(r,c) > 1
lc = im_out(r,c);
elseif im_out(r,c)==0
continue;
else
break;
end
if im_out(r,c+1)==1 %right neighbor
im_out(r,c+1)=lc;
end
if im_out(r+1,c)==1 %bottom neighbor
im_out(r+1,c)=lc;
end
end
end
% Do some correction. In other words, PHASE 2 :)
for r=nr:-1:2 %This time go in the reverse direction.
for c=nc:-1:2
if im_out(r,c)== 0
continue;
end
if (im_out(r,c)~= im_out(r,c-1)) *(im_out(r,c-1)>0) % incase there is a mislabeled
neighbor re-label it. (Left neighbor)
im_out(r,c-1) = im_out(r,c);
end
if (im_out(r,c)~= im_out(r-1,c))* (im_out(r-1,c)>0) % incase there is a mislabeled
neighbor relabel it. (Upper neighbor)
im_out(r-1,c) = im_out(r,c);
end
end
end
%Output results
im_out
figure(1);
imagesc(im_out);
Algorithm:
Once it is created algorithm needs to scan it for two times, to obtain unique group
labels. The first pass labels each cell trying to maintain uniqueness, however, because of
its one-way operation it does not go back and correct mislabeled pixels. Therefore, a
second pass over the output image is needed to correct mislabeled pixels and decrease the
number of groups to optimum level.
Below one can find values for original image, output of first phase and second
phase.
Original Image:
Im =
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 1 1 1 0 1 0 1 0
0 0 1 0 0 0 1 0 0 1 0 1 0
0 0 1 0 0 0 1 0 0 1 0 1 0
0 1 1 1 0 0 1 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Output of Phase 1:
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 4 0 5 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 6 6 0 7 7 7 0 8 0 9 0
0 0 6 0 0 0 7 0 0 8 0 9 0
0 0 6 0 0 0 7 0 0 8 0 9 0
0 10 6 6 0 0 7 0 0 8 8 9 0
0 0 0 0 0 0 0 0 0 0 0 0 0
Output of Phase 2:
im_out =
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 4 0 5 0
0 0 0 0 0 0 0 0 0 0 0 0 0
0 6 6 6 0 7 7 7 0 9 0 9 0
0 0 6 0 0 0 7 0 0 9 0 9 0
0 0 6 0 0 0 7 0 0 9 0 9 0
0 6 6 6 0 0 7 0 0 9 9 9 0
0 0 0 0 0 0 0 0 0 0 0 0 0
It is easily seen that there are no pixels labeled 8 or 10 in the last output.