Professional Documents
Culture Documents
Dip-Lab#14: Image Segmentation: Objective
Dip-Lab#14: Image Segmentation: Objective
Dip-Lab#14: Image Segmentation: Objective
OBJECTIVE:
The objective of this lab is to implement image segmentation in MATLAB.
INTRODUCTION:
Segmentation subdivides an image into its components. It distinguishes objects of interest
from background, e.g. Optical Character Recognition (OCR) systems first segment character
shapes from an image before they start to recognize them. The segmentation operation only
subdivides an image; it does not attempt to recognize the segmented image parts.
THRESHOLDING:
Amplitude thresholding (i.e. in the brightness domain) is the basis approach to image
segmentation. A threshold T is selected that would separate the two modes, i.e. any image
point for which f(x, y)>T is considered as an object; otherwise, the point is called a
background point.
TASK#01:
Perform Region Growing Segmentation In MATLAB.
MATLAB CODE:
clc
clear all
close all
a=[7 6 5 4 3;0 1 0 1 2;1 0 0 1 5;0 0 1 7 7;2 2 1 6 3;1 0 1 3 0];
threshold=3
s=6
for i=1:6
for j=1:5
a2(i,j)=s-a(i,j)
if a2(i,j)<=threshold
z(i,j)=1
else
z(i,j)=0
end
end
end
OUTPUT:
a2 =
-1 0 1 2 3
6 5 6 5 4
5 6 6 5 1
6 6 5 -1 -1
4 4 5 0 3
5 6 5 3 6
z=
1 1 1 1 1
0 0 0 0 0
0 0 0 0 1
0 0 0 1 1
0 0 0 1 1
0 0 0 1 0
TASK#02:
Perform Region Splitting and Merging in MATLAB.
MATLAB CODE:
% Region splitting and merging
clc
close all
clear all
M=[3 6 7 7 6 5 4 3; 1 2 3 4 1 0 1 4;1 6 6 1 0 0 1 5; 4 5 7 0 0 1 7 7;...
3 1 1 2 2 1 6 3; 3 2 2 1 0 1 3 0; 4 4 3 1 5 4 3 1; 3 1 1 2 1 0 2 1];
[r,c]=size(M);
p1=M(1:r/2,1:c/2);
p2=M(1:r/2,c/2+1:c);
p3=M(r/2+1:r,1:c/2);
p4=M(r/2+1:r,c/2+1:c);
[r1,c1]=size(p1);
for i=1:4
for j=1:4
% for 1st quadrant of the matrix
df1= max(max(p1))-min(min(p1));
if df1<=3
M2(i,j)=p1(i,j);
else
m1=p1(1:r1/2,1:c1/2); m2=p1(1:r1/2,c1-1:c1); m3=p1(r1-1:r1,1:c1/2); m4=p1(r1-
1:r1,c1-1:c1);
[r2,c2]=size(m1);
% for 1st sub-quadrant of the 1st quadrant
for k=1:r2
for l=1:c2
df10= max(max(m1))-min(min(m1));
if df10<=3
a1(k,l)=1;
else
a1(k,l)=m1(k,l);
end
M =3 6 7 7 6 5 4 3
1 2 3 4 1 0 1 4
1 6 6 1 0 0 1 5
4 5 7 0 0 1 7 7
3 1 1 2 2 1 6 3
3 2 2 1 0 1 3 0
4 4 3 1 5 4 3 1
3 1 1 2 1 0 2 1
Z=3 6 1 1 6 5 1 1
1 2 1 1 1 0 1 1
1 6 6 1 1 1 1 5
4 5 7 0 1 1 7 7
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
4 4 3 1 5 4 3 1
3 1 1 2 1 0 2 1
TASK#03:
Compute Watershed Segmentation in MATLAB.
MATLAB CODE:
clc
clear all
close all
% Watershed Transform
rgb = imread('pears.png');
I = rgb2gray(rgb);
imshow(I)
title('Original Image')
text(732,501,'Image courtesy of Corel(R)',...
'FontSize',7,'HorizontalAlignment','right')
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I), hy, 'replicate');
Ix = imfilter(double(I), hx, 'replicate');
gradmag = sqrt(Ix.^2 + Iy.^2);
figure
imshow(gradmag,[]), title('Gradient magnitude (gradmag)')
L = watershed(gradmag);
Lrgb = label2rgb(L);
figure, imshow(Lrgb), title('Watershed transform of gradient magnitude (Lrgb)')
se = strel('disk', 20);
Io = imopen(I, se);
figure
imshow(Io), title('Opening (Io)')
Ie = imerode(I, se);
Iobr = imreconstruct(Ie, I);
figure
imshow(Iobr), title('Opening-by-reconstruction (Iobr)')
Ioc = imclose(Io, se);
figure
imshow(Ioc), title('Opening-closing (Ioc)')
Iobrd = imdilate(Iobr, se);
Iobrcbr = imreconstruct(imcomplement(Iobrd), imcomplement(Iobr));
Iobrcbr = imcomplement(Iobrcbr);
figure
imshow(Iobrcbr), title('Opening-closing by reconstruction (Iobrcbr)')
fgm = imregionalmax(Iobrcbr);
figure
imshow(fgm), title('Regional maxima of opening-closing by reconstruction (fgm)')
I2 = I;
I2(fgm) = 255;
figure
imshow(I2), title('Regional maxima superimposed on original image (I2)')
se2 = strel(ones(5,5));
fgm2 = imclose(fgm, se2);
fgm3 = imerode(fgm2, se2);
fgm4 = bwareaopen(fgm3, 20);
I3 = I;
I3(fgm4) = 255;
figure
imshow(I3)
title('Modified regional maxima superimposed on original image (fgm4)')
imshow(I2), title('Regional maxima superimposed on original image (I2)')
bw = imbinarize(Iobrcbr);
figure
imshow(bw), title('Thresholded opening-closing by reconstruction (bw)')
D = bwdist(bw);
DL = watershed(D);
bgm = DL == 0;
figure
imshow(bgm), title('Watershed ridge lines (bgm)')
I4 = I;
I4(imdilate(L == 0, ones(3, 3)) | bgm | fgm4) = 255;
figure
imshow(I4)
title('Markers and object boundaries superimposed on original image (I4)')
Lrgb = label2rgb(L, 'jet', 'w', 'shuffle');
figure
imshow(Lrgb)
title('Colored watershed label matrix (Lrgb)')
figure
imshow(I)
hold on
himage = imshow(Lrgb);
himage.AlphaData = 0.3;
title('Lrgb superimposed transparently on original image')