You are on page 1of 15

Experiment-6

Aim: to implement the geometric operations on images using MATLAB.

Matlab Code:

clc
close all

% Read a colour Image and Resize it to 256 x 256


% Convert the image to grey image and display it

f = imread('pot.jpg');
g1=imresize(f,[256,256],'nearest');
g = rgb2gray(g1);
subplot(2, 2, 1);
imshow(g); title('Original Grey Image');

% Create a translation matrix for x direction - 50 units and y direction


% in 100 units. Use the command maketform to create the affine matrix
% for translation and imtransform to translate the image

t = [ 1 0 0; 0 1 0; 50 100 1];
tform = maketform('affine',t);
translateimg = imtransform(g,tform);
subplot(2, 2, 2);
imshow(translateimg),title('Translated Image');

% Create a rotation matrix for pi/6


% Use the command imtransform to rotate the image using the created
% rotation matrix

subplot(2, 2, 3);
t = pi/6;
r = [cos(t) sin(t) 0; -sin(t) cos(t) 0; 0 0 1];
tform = maketform('affine',r);
rotimg = imtransform(g,tform);
imshow(rotimg),title('Rotated image');

% Use the imrotate command to rotate the image in clockwise direction


% 45 degrees - Use negative values for angle if the rotation required is
% clockwise direction as the default rotation is in counter clockwise
% direction. Additional parameter 'crop' is given to ensure that the
% output image is same as the input image
subplot(2, 2, 4);
h = imrotate(g,-45,'crop');
imshow(h);title('Rotated by 45 degree in clockwise direction');

Results:

Conclusion: Thus we have concluded from this experiment that it can be observed that affine
matrix is created first and then imtransform is used to translate the image. This logic can be
extended for performing other affine transforms also and a negative angle is give to perform the
rotation in clock wise direction and the additional parameter is used to ensure the output image is
same as the input image.
Experiment-7

Aim: to understand and implement the following task in MATLAB


 Spatial filtering
 Spatial Convolution

Matlab Code:

clc;
close all;
clear all;

% Read the test image and display it


myimage = imread('grayleaf.jpg');
subplot(3,3,1);
imshow(myimage); title('Original Image');

% The command fspecial() is used to create mask


% The command imfilter() is used to apply the gaussian filter mask to the image
% Create a Gaussian low pass filter of size 3

gaussmask = fspecial('gaussian',3);
filtimg = imfilter(myimage,gaussmask);
subplot(3,3,2);
imshow(filtimg,[]),title('Output of Gaussian filter 3 X 3');

% Generate a lowpass filter of size 7 X 7


% The command conv2 is used the apply the filter
% This is another way of using the filter

avgfilt = [ 1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1;
1 1 1 1 1 1 1];

avgfiltmask = avgfilt/sum(avgfilt);
convimage= conv2(double(myimage),double(avgfiltmask));

subplot(3,3,3);
imshow(convimage,[]);
title('Average filter with conv2()');

% Add noise to an image


% Display the noisy Image

subplot(3,3,4);
myimage = imread('grayleaf.jpg');
noisyimg = imnoise(myimage,'Salt & Pepper', 0.5);
imshow(noisyimg,[]);
title('Noisy Image');

% generate Median filter of size 3


% The command medianfilt2() is used to filter the image

mymed3img = medfilt2(noisyimg,[3 3]);


subplot(3,3,5);
imshow(mymed3img,[]), title('Output of 3 x 3 Median filter');

% generate Median filter of size 7


% The command medianfilt2() is used to filter the image

mymed7img = medfilt2(noisyimg,[7 7]);


subplot(3,3,6);
imshow(mymed7img,[]), title('Output of 7 x 7 Median filter');

% Generate a high pass filter mask


% The command conv2 is used the apply the filter mask

h = [ 1 -2 -1; -1 5 -1; 1 -2 1];


hpt3 = conv2(double(myimage),double(h));
subplot(3,3,7);
imshow(hpt3/100),title('Output of High pass filter');

% Generate a User defined mask for sharpening


% The command conv2 is used the apply the filter mask

h = [ -1 -1 -1; -1 9 -1; -1 -1 -1];


hpt3 = conv2(double(myimage),double(h));
subplot(3,3,8);
imshow(hpt3/100),title('Sharpening - User defined mask');

% Generate a unsharp filter mask with alpha = 0.3


% The command conv2 is used the apply the filter mask

h = fspecial('unsharp',0.3);
hpt3 = imfilter(myimage,h);
subplot(3,3,9);
imshow(hpt3,[]),title('Output of Unsharp mask filter');

Results:

Conclusion: Thus we have concluded from this experiment that image smoothing and
sharpening can be done by a spatial filter.
Experiment-8

Aim: to understand and implement Edge detection operators using MATLAB

Matlab Code:

clc;
close all;
clear all;

% Read Colour Image and convert it to a grey level Image


% Display the original Image

mycolourimage = imread('grayflower256.jpg');
myimage = rgb2gray(mycolourimage);
subplot(3,3,1);
imshow(myimage); title('Original Image');

% Apply Sobel Operator


% Display only the horizontal Edges

sobelhz = edge(myimage,'sobel','horizontal');
subplot(3,3,2);
imshow(sobelhz,[]); title('Sobel - Horizontal Edges');

% Apply Sobel Operator


% Display only the vertical Edges

sobelvrt = edge(myimage,'sobel','vertical');
subplot(3,3,3);
imshow(sobelhz,[]); title('Sobel - Vertical Edges');

% Apply Sobel Operator


% Display both horizontal and vertical Edges
sobelvrthz = edge(myimage,'sobel','both');
subplot(3,3,4);
imshow(sobelvrthz,[]); title('Sobel - All edges');

% Apply Roberts Operator


% Display both horizontal and vertical Edges

robertsedg = edge(myimage,'roberts');
subplot(3,3,5);
imshow(robertsedg,[]); title('Roberts - Edges');
% Apply Prewitt Operator
% Display both horizontal and vertical Edges

robertsedg = edge(myimage,'prewitt');
subplot(3,3,6);
imshow(robertsedg,[]); title('Prewitt - Edges');

% Apply Laplacian Filter

f=fspecial('laplacian');
lapedg = imfilter(myimage,f,'symmetric');
subplot(3,3,7);
imshow(lapedg,[]); title('Laplacian Filter');

% Apply LOG edge detection


% The sigma used is 3

f=fspecial('log',[15,15],3.0);
logedg1 = edge(myimage,'zerocross',[],f);
subplot(3,3,8);
imshow(logedg1); title('Log with sigma 3');

% Apply Canny edge detection


cannyedg = edge(myimage,'canny');
subplot(3,3,9);
imshow(cannyedg,[]); title('Canny Edge');
Results:

Conclusion: Thus we have concluded from this experiment that edges can be implemented and
extracted for synthetic images.
Experiment-9

Aim: to understand and implement Image segmentation in MATLAB using the following
threshold types
 Simple thresholding
 Multiple thresholding
 Adaptive thresholding
 Optimal thresholding

Matlab Code:

clc;
close all;
clear all;

a = imread('grayflower256.jpg');
a = rgb2gray(a);
subplot(3,3,1);
imshow(a); title('Original Image');

% Simple thresholding at 0.3


% This is equivalent to saying threshoding at pixel value
% 0.3 x 255 = 76.5 , approximatesly 77
% imlabel() is a Matlab command that can threshold any matrix

level = 0.3;
%% Display the threshold image
subplot(3,3,2);
segimage1 = im2bw(a,level);
imshow(segimage1); title('Simple Thresholding at 0.3');

% Simple thresholding at 0.6


% So threshold value is 0.6 x 255 = 153
% Single Thresholding can be done like this also

%% Display the threshold image


subplot(3,3,3);
imshow(a > 153); title('Simple Thresholding at 0.6');

% Multiple thresholding Algorithm


% Let us assume that the output should be zero if pixel value is
% if <= 0.1 x 255 = 25.5 = 26, pixel output 204 if pixel value is <= 0.9 x
% 255 = 230 and 0 if pixel value is above 230.

%% Create a temporary matrix g

tmp = a;

[m n]= find(a<26);
for j = 1: length(m)
tmp(m(j),n(j))=0;
end

[m n]= find(a>26 & a <= 230);


for j = 1: length(m)
tmp(m(j),n(j))=0.8;
end

[m n]= find(a>230);
for j = 1: length(m)
tmp(m(j),n(j))=0;
end
subplot(3,3,4);
segimage2 = im2bw(tmp,0);
imshow(segimage2); title('Multiple threshoding(Between 27-230)');

%% Find the threshold Value using Otsu Algorithm

level = graythresh(a);

%% Display the threshold image

subplot(3,3,5);
segimage = im2bw(a,level);
imshow(segimage); title('Otsu - Optimal Segmented Image');

%% Display Blured Image

b = imread('bluredtxt.jpg');
subplot(3,3,6);
imshow(b); title('Badly illuminated Image');

level = graythresh(b);
subplot(3,3,7);
segimage = im2bw(b,level);
imshow(segimage); title('Otsu - Segmentation for bad illuminated Image');
b = imread('bluredtxt.jpg');
b = rgb2gray(b);

%Create an average Image

avgfilt = ones(13,13);
adaptfiltmask = avgfilt/sum(avgfilt);
im = imfilter(b,adaptfiltmask,'replicate');

%Create an median image


im1 = medfilt2(b,[20 20]);

%Adaptive threshold algorithm use


% threshold = mean + constant (Here 18)

thresh = im+18;
adaptthreshimg = b - thresh;
subplot(3,3,8);
imshow(adaptthreshimg > 0);

%Adaptive threshold algorithm used threshold = mean + constant (Here 2)


thresh1 = im1 + 2;
adaptthreshimg = b - thresh1;
subplot(3,3,9);
imshow(adaptthreshimg > 0);
Results:

Conclusion: Thus we have concluded from this experiment that segments of an image can be
extracted by using thresholding.
Experiment-10

Aim: to understand and implement morphological operators and their applications in


MATLAB.

Matlab Code:

clc;
close all;
clear all;

% Read the test Image


% Convert the image to binary image

myorigimg = imread('test.jpg');
myorigimg = im2bw(rgb2gray(myorigimg));
subplot(3, 3, 1);
imshow(myorigimg);title('Originalimage');

% Create Structuring Element


se = strel('disk', 9);

% Perform dilation operation using imdilate command


% Display the dilated image

mydilatedimg = imdilate(myorigimg, se);


subplot(3, 3, 2);
imshow(mydilatedimg);title('Dilated image');

% Perform Erosion operation using imerode command


% Display the Eroded image

myerodedimg = imerode(myorigimg, se);


subplot(3, 3, 3);
imshow(myerodedimg);title('Eroded image');

% Find Internal Boundary


% Internal Boundary = Dilated Image AND Not of Eroded Image
% Display Internal Boundary

internalboundimg = mydilatedimg & ~ myerodedimg;


subplot(3, 3, 4);
imshow(internalboundimg,[]);title('Internal Boundary');
% Find External Boundary
% External Boundary = Dilated Image AND Not of Eroded Image
% Display External Boundary

externalboundimg = mydilatedimg & ~myorigimg;


subplot(3, 3, 5);
imshow(externalboundimg,[]);title('External Boundary');

% Find Morphological Gradient


% Morphological Gradient = Dilated Image AND Not of Eroded Image
% Display External Boundary

mymorphgradimg = imsubtract(myorigimg,myerodedimg);
subplot(3, 3, 6);
imshow(mymorphgradimg,[]);title('Morphological Gradient');

% Perform Thinning operation using bwmorph() command


% Display the dilated image

thinf = bwmorph(myorigimg,'thin');
subplot(3,3,7);
imshow(thinf);title('Thinning of the Image');

% Perform Thickening operation using bwmorph()command


% Display the dilated image

thickf = bwmorph(myorigimg,'thicken');
subplot(3,3,8);
imshow(thickf);title('Thickening of the Image');

% Perform Skeletonozation operation using bwmorph()command


% with 8 iterations and display the dilated image

skelf100 = bwmorph(myorigimg,'skel',9);
subplot(3,3,9);
imshow(skelf100);title('Skeletonization - 9 iterations');
Results:

Conclusion: Thus we have concluded from this experiment that it can be observed that external
boundaries are larger than the internal boundaries and morphological gradient is thicker.

You might also like