Dip-Lab#14: Image Segmentation: Objective

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

DIP-LAB#14: IMAGE SEGMENTATION

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.

MICROSCOPE IMAGE OF CELLS:

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

% for 2nd sub-quadrant of the 1st quadrant


df11= max(max(m2))-min(min(m2));
if df11<=4
a2(k,l)=1;
else
a2(k,l)=m2(k,l);
end

% for 3rd sub-quadrant of the 1st quadrant


df12= max(max(m3))-min(min(m3));
if df12<=3
a3(k,l)=1;
else
a3(k,l)=m3(k,l);
end

% for 4th sub-quadrant of the 1st quadrant


df13= max(max(m4))-min(min(m4));
if df13<=4
a4(k,l)=1;
else
a4(k,l)=m4(k,l);
end
end
end
end
end
end
A=[a1 a2; a3 a4];

% for 2nd quadrant of the matrix


df2= max(max(p2))-min(min(p2));
if df2<=3
M3(i,j)=p2(i,j);
else
n1=p2(1:r1/2,1:c1/2); n2=p2(1:r1/2,c1-1:c1); n3=p2(r1-1:r1,1:c1/2); n4=p2(r1-1:r1,c1-
1:c1);
[r2,c2]=size(n1);
end
%for 1st sub-quadrant of the 2nd quadrant
for k=1:r2
for l=1:c2
df20= max(max(n1))-min(min(n1));
if df20<=2
b1(k,l)=1;
else
b1(k,l)=n1(k,l);
end
end
end

%for 2nd sub-quadrant of the 2nd quadrant


for k=1:r2
for l=1:c2
df21= max(max(n2))-min(min(n2));
if df21<=3
b2(k,l)=1;
else
b2(k,l)=n2(k,l);
end
end
end

%for 3rd sub-quadrant of the 2nd quadrant


for k=1:r2
for l=1:c2
df22= max(max(n3))-min(min(n3));
if df22<=1
b3(k,l)=1;
else
b3(k,l)=n3(k,l);
end
end
end

%for 4th sub-quadrant of the 2nd quadrant


for k=1:r2
for l=1:c2
df23= max(max(n4))-min(min(n4));
if df23<=1
b4(k,l)=1;
else
b4(k,l)=n4(k,l);
end
end
end
B=[b1 b2;b3 b4];

% for 3rd quadrant of the matrix


df3= max(max(p3))-min(min(p3));
if df3<=2
M4(i,j)=p3(i,j);
else
x1=p3(1:r1/2,1:c1/2); x2=p3(1:r1/2,c1-1:c1); x3=p3(r1-1:r1,1:c1/2); x4=p3(r1-1:r1,c1-
1:c1);
[r2,c2]=size(x1);
end
%for 1st sub-quadrant of the 3rd quadrant
for k=1:r2
for l=1:c2
df30= max(max(x1))-min(min(x1));
if df30<=2
d1(k,l)=1;
else
d1(k,l)=x1(k,l);
end
end
end

%for 2nd sub-quadrant of the 3rd quadrant


for k=1:r2
for l=1:c2
df31= max(max(x2))-min(min(x2));
if df31<=3
d2(k,l)=1;
else
d2(k,l)=x2(k,l);
end
end
end

%for 3rd sub-quadrant of the 3rd quadrant


for k=1:r2
for l=1:c2
df32= max(max(x3))-min(min(x3));
if df32<=1
d3(k,l)=1;
else
d3(k,l)=x3(k,l);
end
end
end

%for 4th sub-quadrant of the 3rd quadrant


for k=1:r2
for l=1:c2
df33= max(max(x4))-min(min(x4));
if df33<=1
d4(k,l)=1;
else
d4(k,l)=x4(k,l);
end
end
end

D=[d1 d2; d3 d4];

% for 4th quadrant of the matrix


df4= max(max(p4))-min(min(p4));
if df4<=2
M4(i,j)=p3(i,j);
else
y1=p4(1:r1/2,1:c1/2); y2=p4(1:r1/2,c1-1:c1); y3=p4(r1-1:r1,1:c1/2); y4=p4(r1-1:r1,c1-
1:c1);
[r2,c2]=size(y1);
end
%for 1st sub-quadrant of the 4th quadrant
for k=1:r2
for l=1:c2
df40= max(max(y1))-min(min(y1));
if df30<=2
e1(k,l)=1;
else
e1(k,l)=y1(k,l);
end
end
end

%for 2nd sub-quadrant of the 4th quadrant


for k=1:r2
for l=1:c2
df41= max(max(y2))-min(min(y2));
if df21<=3
e2(k,l)=1;
else
e2(k,l)=y2(k,l);
end
end
end

%for 3rd sub-quadrant of the 2nd quadrant


for k=1:r2
for l=1:c2
df42= max(max(y3))-min(min(y3));
if df42<=1
e3(k,l)=1;
else
e3(k,l)=y3(k,l);
end
end
end

%for 4th sub-quadrant of the 4th quadrant


for k=1:r2
for l=1:c2
df43= max(max(y4))-min(min(y4));
if df43<=1
e4(k,l)=1;
else
e4(k,l)=y4(k,l);
end
end
end
E=[e1 e2; e3 e4];
Z=[A,B;D,E]
OUTPUT:

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')

STEP#01: READ IN THE COLOR IMAGE AND CONVERT IT TO GRAY-SCALE


STEP#02: USE THE GRADIENT MAGNITUDE AS THE SEGMENTATION
FUNCTION:

STEP#03: MARK THE FOREGROUND OBJECTS:


STEP#04: COMPUTE BACKGROUND MARKERS:

STEP#05: VISUALIZE THE RESULT:

You might also like