Professional Documents
Culture Documents
Experiment-6 Aim: To Implement The Geometric Operations On Images Using MATLAB. Matlab Code
Experiment-6 Aim: To Implement The Geometric Operations On Images Using MATLAB. Matlab Code
Matlab Code:
clc
close all
f = imread('pot.jpg');
g1=imresize(f,[256,256],'nearest');
g = rgb2gray(g1);
subplot(2, 2, 1);
imshow(g); title('Original Grey 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');
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');
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
Matlab Code:
clc;
close all;
clear all;
gaussmask = fspecial('gaussian',3);
filtimg = imfilter(myimage,gaussmask);
subplot(3,3,2);
imshow(filtimg,[]),title('Output of Gaussian filter 3 X 3');
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()');
subplot(3,3,4);
myimage = imread('grayleaf.jpg');
noisyimg = imnoise(myimage,'Salt & Pepper', 0.5);
imshow(noisyimg,[]);
title('Noisy Image');
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
Matlab Code:
clc;
close all;
clear all;
mycolourimage = imread('grayflower256.jpg');
myimage = rgb2gray(mycolourimage);
subplot(3,3,1);
imshow(myimage); title('Original Image');
sobelhz = edge(myimage,'sobel','horizontal');
subplot(3,3,2);
imshow(sobelhz,[]); title('Sobel - Horizontal Edges');
sobelvrt = edge(myimage,'sobel','vertical');
subplot(3,3,3);
imshow(sobelhz,[]); title('Sobel - 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');
f=fspecial('laplacian');
lapedg = imfilter(myimage,f,'symmetric');
subplot(3,3,7);
imshow(lapedg,[]); title('Laplacian Filter');
f=fspecial('log',[15,15],3.0);
logedg1 = edge(myimage,'zerocross',[],f);
subplot(3,3,8);
imshow(logedg1); title('Log with sigma 3');
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');
level = 0.3;
%% Display the threshold image
subplot(3,3,2);
segimage1 = im2bw(a,level);
imshow(segimage1); title('Simple Thresholding at 0.3');
tmp = a;
[m n]= find(a<26);
for j = 1: length(m)
tmp(m(j),n(j))=0;
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)');
level = graythresh(a);
subplot(3,3,5);
segimage = im2bw(a,level);
imshow(segimage); title('Otsu - Optimal Segmented 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);
avgfilt = ones(13,13);
adaptfiltmask = avgfilt/sum(avgfilt);
im = imfilter(b,adaptfiltmask,'replicate');
thresh = im+18;
adaptthreshimg = b - thresh;
subplot(3,3,8);
imshow(adaptthreshimg > 0);
Conclusion: Thus we have concluded from this experiment that segments of an image can be
extracted by using thresholding.
Experiment-10
Matlab Code:
clc;
close all;
clear all;
myorigimg = imread('test.jpg');
myorigimg = im2bw(rgb2gray(myorigimg));
subplot(3, 3, 1);
imshow(myorigimg);title('Originalimage');
mymorphgradimg = imsubtract(myorigimg,myerodedimg);
subplot(3, 3, 6);
imshow(mymorphgradimg,[]);title('Morphological Gradient');
thinf = bwmorph(myorigimg,'thin');
subplot(3,3,7);
imshow(thinf);title('Thinning of the Image');
thickf = bwmorph(myorigimg,'thicken');
subplot(3,3,8);
imshow(thickf);title('Thickening of the 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.