You are on page 1of 86

LAB MANUAL

OF

FUNDAMENTALS OF
IMAGE PROCESSING
Using MATLAB
DEPARTMENT OF EC
GOVERNMENT ENGINEERING COLLEGE
DAHOD -389151

Prepared By :
Prof. Alpesh H. Dafda
Asst. Prof. (E.C.)

1
CERTIFICATE
This is to certify that
__________________________________ Enrollement
number ____________________ has successfully
completed his/her term work and practical work in the
subject FUNDAMENTALS OF IMAGE PROCESSING
(181102) for the term ending in __________________
at Government Engineering College, Dahod, for partial
fulfillment of B.E. degree to be awarded by Gujarat
Technological University. This work has been carried
out under my supervision and is to my satisfaction.

Date:

Place:

Subject Teacher Head of Department

2
INDEX
NO SUBJECT DATE PAGE SIGN REMARKS
1 To write a program to read an
image and display it in double
precision.
2 To write a program to display a
magic square as an image.
3 To write a program to display an
image and its histogram.
4 To write a program to learn basic
functions of DIP in MATLAB.
5 To write a program for mirror
image generation.
6 To write a program to find negative
of an image.
7 To write a program to show
shrinking of an image.
8 To write a program to show
zooming of an image.
9 To write a program to show image
cropping.
10 To write a program for the display
of Image in Grayscale, Red, Green
and Blue.
11 To write a program to learn Image
Segmentation Processing.
12 To write a program to show Image
Enhancement.
13 To write a program to learn Line
Algorithms based Image Scanning.
14 To write a program to show
histogram equalization.
15 To write a program to illustrate
Gray level(Intensity) slicing.
16 To write a program to illustrate bit
plane slicing.
17 To write a program to show edge
detection.
18 To write a program to convert
image into high boost image.

3
19 To write a program to filter image
using a high pass filter.
20 To write a program to blur and
deblur an image.
21 To write a program to blur a part of
an image.
22 To write a program to apply liner
and log transformation on image.
23 To write a program to illustrate the
effect of Laplacian Derivative on an
image.
24 To write a program to illustrate the
effect of Unsharp Mask and
Highboost filtering.
25 To write a program to illustrate the
difference between arithmetic
mean filter and Geometric mean
filter in removing Gaussian noise.
26 To write a program to illustrate the
effect of Square Averaging filter of
different masks on an image.
27 To write a program to observe the
effect of order-statistics filter like
Median Filter of size 3x3 on an
image corrupted by salt & pepper
noise.

28 To write a MATLAB program to


observe the result of applying the
following steps on the given image.
1) Multiplying the given
image by (-1)^x+y,
2) Computing the DFT,
3) Taking the complex
conjugate of the transform,
4) Computing the inverse
DFT,and
5) Multiplying the real part
of the result by (-1)^(x+y)

4
Practical -1

AIM : To write a program to read an image and display it in double


precision.

THEORY :

5
MATLAB PROGRAM :

clc;
A = imread('India.jpg');
A= im2double(A);
imshow(A)

OUTPUT :

CONCLUSIONS :

6
Practical - 2

AIM : To write a program to display a magic square as an image.

THEORY :

7
MATLAB PROGRAM :

clc;
A = magic(100)
I = im2double(A);
imshow(I)

OUTPUT :

CONCLUSIONS :

8
Practical -3

AIM : To write a program to display an image and its histogram.


THEORY :

9
MATLAB PROGRAM :

clc;
A=imread('shivani.jpg');
I=rgb2gray(A);
I=im2double(I);
imshow(I)
figure,
imhist(I)

OUTPUT :

3000

2500

2000

1500

1000

500

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

CONCLUSIONS :

10
Practical - 4

AIM : To write a program to learn basic functions of DIP in MATLAB.

THEORY :

11
MATLAB PROGRAM :

clc;
close all;
clear all;

% A = imread(filename, fmt)
% It reads a grayscale or color image from the file specified
by the string filename.
% And display it in the matrix form. Each element of a matrix
show the intensity of that pixel.

A=imread('lena.tif');
figure
subplot(221)
imshow(A);
title('Read an image and show');

% imwrite(A,filename,fmt)
% It writes the image to filename, inferring the format to use
from the filename's extension.

imwrite(A,'E:\abc.jpg');
subplot(222)
imshow('E:\abc.jpg');
title('Write an image and show');

% I = mat2gray(A, [amin amax])


% Convert matrix to grayscale image.The returned matrix I
contains values
% in the range 0.0 (black) to 1.0 (full intensity or white).
% amin and amax are the values in A that correspond to 0.0 and
1.0 in I.

B=mat2gray(A,[100,150]);
subplot(223)
imshow(B);
title('mat2gray');

% Z = imsubtract(X,Y)
% Subtract one image from another or subtract constant from
image

C=imsubtract(A,100);
subplot(224)
imshow(C);
title('Image subtracted from constant');

% IM2 = imcomplement(IM)
% It computes the complement of the image IM. IM can be a
binary, grayscale, or RGB image.

12
% IM2 has the same class and size as IM.

D=imcomplement(A);
figure
subplot(121)
imshow(D);
title('Complemented image');

% B = imrotate(A,angle)
% It rotates image A by angle degrees in a counterclockwise
direction around its center point. To rotate the image
clockwise,
% specify a negative value for angle. imrotate makes the
output image B large enough to contain the entire rotated
image.
% imrotate uses nearest neighbor interpolation, setting the
values of pixels in B that are outside the rotated image to 0
(zero).

E=imrotate(A,-90);
subplot(122)
imshow(E);
title('Image rotated by 90');

% Z = imdivide(X,Y)
% Divide one image into another or divide image by constant

I = imread('India.jpg');
J = imdivide(I,2);
figure
subplot(121), imshow(I)
title('Original image');
subplot(122), imshow(J)
title('Image divided by constant');

% Z = immultiply(X,Y)
% Multiply two images or multiply image by constant

W = imread('shivani.jpg');
Z = immultiply(W,0.5);
figure
subplot(121), imshow(W)
title('Original image');
subplot(122), imshow(Z)
title('Image multiplied by constant');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

13
a = imread('shivani.jpg');

figure
subplot(4,2,[1 2])
imshow(a)
title('Original')

b = imresize(a,[4,4])
subplot(4,2,3)
imshow(b)
title('Resize 4x4')
c = imresize(a,[128,128])
subplot(4,2,4)
imshow(c)
title('Resize 128x128')

d = imrotate(a,60)
subplot(4,2,5)
imshow(d)
title('Rotated by 60')

e = im2bw(a,0.6)
subplot(4,2,6)
imshow(e)
title('Binary')

f=rgb2ind(a,32);
subplot(4,2,6)
imshow(f);
title('Array Indexing')

g=rgb2gray(a)
subplot(4,2,7)
imshow(g)
title('Grayscale');

14
OUTPUT :

Read an image and show Write an image and show

Complemented image Image rotated by 90

mat2gray Image subtracted from constant

Original image Image multiplied by constant


Original image Image divided by constant

15
Original

Resize 4x4 Resize 128x128

Rotated by 60 Array Indexing

Grayscale

CONCLUSIONS :

16
Practical - 5

AIM : To write a program for mirror image generation.

THEORY :

17
MATLAB PROGRAM :
clc;
% this program produces mirror image of the image passed to it
% and also displays both the original and mirror image
a=imread('pout.tif');
[r,c]=size(a);
for i=1:1:r
k=1;
for j=c:-1:1
temp=a(i,k);
result(i,k)=a(i,j);
result(i,j)=temp;
k=k+1;
end
end
subplot(1,2,1),imshow(a)
subplot(1,2,2),imshow(result)

OUTPUT :

CONCLUSIONS :

18
Practical - 6

AIM : To write a program to find negative of an image.

THEORY :

19
MATLAB PROGRAM :

%%
% Clearing History
clear all force
close all

%%

%%
% This program finds Negative of Image
%%

%%
% Importing image into Workspace

I=imread('person.jpg');
%%

%%
% Displaying image using imshow

imshow(I);
%%

%%
% Converting Image from RGB into gray
I=rgb2gray(I);
%%

%%
% Computing size of image; m rows and n columns
[m n]=size(I);
%%

%%
% Operations ...first row wise then column wise
for q=1:n
for p=1:m
I2(p,q)=255-I(p,q);
% By using formula s=L-1-r
% Where L is no. of gray Levels of image
% and r is Intensity
end
end
%%

%%
% Displaying Negative of Original Image
figure

20
imshow(I2);title('Negative Image');
%%

%%
% Both Images in Single Window
figure
subplot(1,2,1);subimage(I);title(' Original image');
subplot(1,2,2);subimage(I2);title(' Negative image');
%%
%%
% writing averaged file to disk
imwrite(I2,'I_Negative.jpg','jpg');
disp('Image Negated and written to disk with name
I_Negative.jpg');

OUTPUT :
Negative Image

Original image Negative image

50 50
100 100

150 150

200 200

250 250
100 200 300 100 200 300

CONCLUSIONS :

21
Practical - 7

AIM : To write a program to show shrinking of an image.

THEORY :

22
MATLAB PROGRAM :
% Shrinking

clc;
close all;
clear all;

A=imread('lena.tif');
disp('Size of original image');
c=size(A)
for i=1:2:c(1)
for j=1:2:c(2)
B((i+1)/2,(j+1)/2)=A(i,j);
end
end
imshow(A);
title('Original image');
disp('Size of shrinked image');
d=size(B)
figure;
imshow(B);
title('Shrinked image');

OUTPUT:

Size of original image

c =

256 256

Size of shrinked image

d =

128 128

23
Original image

Shrinked image

CONCLUSIONS :

24
Practical - 8

AIM : To write a program to show zooming of an image.

THEORY :

25
MATLAB PROGRAM :

% Zooming

clc;
close all;
clear all;

A = imread('lena.tif');
disp('Size of original image');
c = size(A)
p=0;
for i=1:1:2*c(1)-1
k=0;
for j=1:1:2*c(2)-1
B(i,j)=A(i-p,j-k);
if rem(j,2) == 0;
k=k+1;
end
end
if rem(i,2) == 0;
p=p+1;
end
end
imshow(A);
title('Original image');
disp('Size of zoomed image');
d=size(B)
figure;
imshow(B);
title('Zoomed image');

OUTPUT:

Size of original image

c=

256 256

Size of zoomed image

d=

511 511

26
Zoomed image
Original image

CONCLUSIONS :

27
Practical - 9

AIM : To write a program to show image cropping.

THEORY :

28
MATLAB PROGRAM :

clc;
close all;
clear all;
a=imread('lena.tif');
c=size(a)
figure(1);
imshow(a)
title('The original image');
x=input('Enter value of Row:')
y=input('Enter value of Column:')

for i = x:1:c(1)-x
for j = y:1:c(2)-y
b(i+1-x,j+1-y)=a(i,j);
end
end
figure(2);
imshow(b);
title('The cropped image');
size(b)

OUTPUT :

c =

256 256

Enter value of Row:90

x =

90

Enter value of Column:90

y =

90

ans =

77 77

29
The original image
The cropped image

CONCLUSIONS:

30
Practical - 10

AIM : To write a program for the display of Image in Grayscale, Red,


Green and Blue.

THEORY :

31
MATLAB PROGRAM :

clc
image1=imread('India.jpg');
image2=rgb2gray (image1);
subplot(3,3,4)
imshow(image2)
title('GRAYSCALE')

[r c d]=size (image1);
z=zeros(r,c);
tempr=image1;
tempr(:,:,2)=z;
tempr(:,:,3)=z;
subplot(3,3,1)
imshow(tempr)
title('RED')

tempg=image1;
tempg(:,:,1)=z;
tempg(:,:,3)=z;
subplot(3,3,2)
imshow(tempg)
title('GREEN')

tempb=image1;
tempb(:,:,1)=z;
tempb(:,:,2)=z;
subplot(3,3,3)
imshow(tempb)
title('BLUE')

32
OUTPUT :

RED GREEN BLUE

GRAYSCALE

CONCLUSIONS :

33
Practical - 11

AIM : To write a program to learn Image Segmentation Processing.

THEORY :

34
MATLAB PROGRAM :

a = imread('India.jpg')

b = imresize(a, [512 512]);


subplot(4,2,1)
imshow(b)
title('Resized')

c = rgb2gray(b);
subplot(4,2,2)
imshow(c)
title('GrayScale')

[m n]=size(c);
d = zeros(m,n)

%Conversion to Binary Image


for i=1:m
for j=1:n
if(c(i,j)>128)
d(i,j) = 1;
else
d(i,j) = 0;
end
end
end
subplot(4,2,3)
imshow(d)
title('Binary')

%Processing on Segmentation
e = zeros(m/2,n/2)
f = zeros(m/2,n/2)
g = zeros(m/2,n/2)
h = zeros(m/2,n/2)

for i=1:m/2
for j=1:n/2
if(c(i,j) >128)
e(i,j) = 1;
else
e(i,j) = 0;
end
end
end
subplot(4,2,4)
imshow(e)

35
title('1/4th Segment')

for i=1:m/2
for j=n/2:n
if(c(i,j) >128)
f(i,j-255) = 1;
else
f(i,j-255) = 0;
end
end
end
subplot(4,2,5)
imshow(f)
title('1/2th Segment')

for i=m/2:m
for j=1:n/2
if(c(i,j) >128)
g(i-255,j) = 1;
else
g(i-255,j) = 0;
end
end
end
subplot(4,2,6)
imshow(g)
title('3/4th Segment')

for i=m/2:m
for j=n/2:n
if(c(i,j) >128)
h(i-255,j-255) = 1;
else
h(i-255,j-255) = 0;
end
end
end
subplot(4,2,7)
imshow(h)
title('4th Segment')

36
OUTPUT :

Resized GrayScale

Binary 1/4th Segment

1/2th Segment 3/4th Segment

4th Segment

CONCLUSIONS :

37
Practical - 12

AIM : To write a program to show Image Enhancement.

THEORY :

38
MATLAB PROGRAM :

clc;
a = imread('India.jpg');
subplot(4,2,[1 2])
imshow(a);
title('Original')

b = rgb2gray(a);
c = imresize(b,[256,256]);
subplot(4,2,3)
imshow(c);
title('Graycale')

%negetive of Image
d = imcomplement(c)
subplot(4,2,4)
imshow(d);
title('Negetive')

%Log Transformation
const=0.2;
e = const*log(1+ double(c));
subplot(4,2,5)
imshow(e);
title('Log Transformation')

%adjust
f = imadjust(c,[0.1 0.5],[0.5 0.9],[]);
subplot(4,2,6)
imshow(f);
title('Adjustment')

%Contrast Stretching
f = imadjust(c,[],[],0.3);
subplot(4,2,7)
imshow(f);

title('Contast Stretching')

f = imadjust(c,[],[],3);
subplot(4,2,8)
imshow(f);
title('Power Law Transformation')

39
OUTPUT :

Original

Graycale Negetive

Log Transformation Adjustment

Contast Stretching Power Law Transformation

CONCLUSIONS :

40
Practical - 13

AIM : To write a program to learn Line Algorithms based Image


Scanning.

THEORY :

41
MATLAB PROGRAM :

a = imread('India.jpg');
subplot(2,2,[1 2])
imshow(a)
title('Original')

b=imresize(a ,[512 512]);


subplot(2,2,3)
imshow(b)
title('Resize by 512x512')

c=rgb2gray(b);
subplot(2,2,4)
imshow(c)
%d=zeros(1,300);
[m, n]=size(c);
d=[m n];
d=zeros(1,100);

for i=1:m
for j=1:n

if(c(i,j)<=128 )
d(i,j) = 1;
else
d(i,j) = 0;
end
end
end

r1=[m];
c1=[n];
r1=zeros(r1);
c1=zeros(c1);
l=zeros(1,3)

for i=2:m
for j=2:n

if(c(i,j)==1 )

if(d(i,j-1)==0 & d(i-1,j)==0)


r1(i-1)=i;
c1(j-1)=j;
l(i,j)=1
end
if(d(i,j-1)==0 & d(i-1,j)==1)
r1(i-1)=i;
c1(j-1)=j;

42
l(i,j)=1;
% l1(r1,c1)=1;
end
if(d(i,j-1)==1 & d(i-1,j)==0)
r1(i-1)=i;
c1(j-1)=j;
%l(i,j)=l1;

end

else if(d(i,j-1)==1 & d(i-1,j)==1)


r1(i-1)=i;
c1(j-1)=j;
% l(i,j)=l2;
end

end
end
end
OUTPUT :
Original

Resize by 512x512

CONCLUSIONS :

43
Practical - 14

AIM : To write a program to show histogram equalization.

THEORY :

44
MATLAB PROGRAM :

clc;
clear all;
close all;
img=imread('lena.tif');
count=zeros(1,256);

a=size(img);

for i = 1:1:a(1)
for j = 1:1:a(2)
count(img(i,j)+1)= count(img(i,j)+1)+1;
end
end
pdf=count*(1/(a(1)*a(2)))

add=zeros(1,256);
for i = 1:256
if (i == 1 )
add(i)=pdf(i);
else
add(i) = add(i-1) + pdf(i);
end
end

for i = 1:1:a(1)
for j = 1:1:a(2)
x=img(i,j);
img1(i,j)= add(x+1);
end
end
figure()
imhist(img)
title('Histogram of original image');
figure()
imhist(img1)
title('Equalized Histogram')
figure(),imshow(img)
title('Original image');
figure(),imshow(img1)
title('Image after histogram equalization');

45
OUTPUT :

Histogram of original image Equalized Histogram


900 900

800 800

700 700

600 600

500 500

400 400

300 300

200 200

100 100

0 0

0 50 100 150 200 250 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Original image Image after histogram equalization

CONCLUSIONS :

46
Practical - 15

AIM : To write a program to illustrate Gray level(Intensity) slicing.

THEORY :

47
MATLAB PROGRAM :

%%
%%
% This is for Gray level(Intensity) slicing with background
%%

%%
% Clear History
clear all
close all
%%

%%
% Importing image to Workspace
RGB=imread('person.jpg');
%%
%%
% Converting image into gray and computing size
I=rgb2gray(RGB);
[m n]=size(I);
%%

L=255;

%%
% Creating Array a for values of a for further formulating [A,B]
such
% that... B-A=20
a=0:30:(max(max(I))-20)
%%

%%
% { I(p,q)= L=255 for a<=I(p,q)<=b
% I(p,q)= I(p,q) for otherwise }
for r=1:length(a)
b(r)=a(r)+20;
for p=1:m
for q=1:n
if (I(p,q)>= a(r) & I(p,q)<=b(r))

I6(p,q)=L;
else
I6(p,q)=I(p,q);

end
end
end

figure,imshow(I6);xlabel(a(r));title('Gray Level slicing with


background');
%pause(0.1);close all force
end

48
OUTPUT :

Gray Level slicing with background Gray Level slicing with background Gray Level slicing with background

0 30 60

Gray Level slicing with background Gray Level slicing with background Gray Level slicing with background

90 120 150

Gray Level slicing with background Gray Level slicing with background

180 210

CONCLUSIONS :

49
Practical - 16

AIM : To write a program to illustrate bit plane slicing.

THEORY :

50
MATLAB PROGRAM :

clc;
close all;
clear all;

c = imread('lena.tif');
figure(1);
clf
imshow(c);
title('Original image');

% Get all eight bitplanes


b0 = logical(bitget(c,1));
b1 = logical(bitget(c,2));
b2 = logical(bitget(c,3));
b3 = logical(bitget(c,4));
b4 = logical(bitget(c,5));
b5 = logical(bitget(c,6));
b6 = logical(bitget(c,7));
b7 = logical(bitget(c,8));

figure(2);
clf

subplot(221);
imshow(b0);
title('b0')

subplot(222);
imshow(b1);
title('b1')

subplot(223);
imshow(b2);
title('b2')

subplot(224);
imshow(b3);
title('b3')

figure(3);
clf

subplot(221);
imshow(b4);
title('b4')

subplot(222);
imshow(b5);

51
title('b5')

subplot(223);
imshow(b6);
title('b6')

subplot(224);
imshow(b7);
title('b7')

OUTPUT :

Original image b0 b1

b2 b3

b4 b5

b6 b7

CONCLUSIONS :

52
Practical - 17

AIM : To write a program to show edge detection.

THEORY :

53
MATLAB PROGRAM :

clf;
clc;
close all;
i = imread('lena.tif');
subplot(1,3,1);
imshow(i);
title('input image');
w = [-1 -1 -1;-1 8 -1; -1 -1 -1]
j = abs(imfilter(double(i),w));
T = 0.04*max(j(:))
j = j >= T;
subplot(1,3,2);
imshow(j);
title('edge detected output 1');
%second method for isolated point detection
p = imfinfo('circuit.tif');
q = p.Width
r = p.Height
s =
imsubtract(ordfilt2(i,25,ones(5,5)),ordfilt2(i,1,ones(5,5)));
%B=ORDFILT2(A,ORDER,DOMAIN)B=ORDFILT2(A,5,ONES(3,3))
implements a3-by-3
% median filter; B=ORDFILT2(A,1,ONES(3,3)) implements a 3-by-3
% minimum filter; and B=ORDFILT2(A,9,ONES(3,3)) implements a
% 3-by-3 maximum filter. B=ORDFILT2(A,4,[0 1 0; 1 0 1; 0 1 0])
% replaces each element in A by the maximum of its north,
east,
% south, and west neighbors.
T = 0.3*max(s(:))
S = s >= T;
subplot(1,3,3);
imshow(S);
title('edge detected output 2');

54
OUTPUT :

input image edge detected output 1 edge detected output 2

CONCLUSIONS :

55
Practical - 18
AIM : To write a program to convert image into high boost image.

THEORY :

56
MATLAB PROGRAM :

clc
a=2.8; % can be changed to get high quality image
f=imread('lena.tif');
subplot(121);imshow(f,[]);title('original image');
f=double(f);
f1=a*f;
b=ones(3);
f2=(1/9)*spatialfilt(f,b);
x=f1-f2;
subplot(122);imshow(x,[]);title('High boost image');

OUTPUT :

original image High boost image

CONCLUSIONS :

57
Practical - 19

AIM : To write a program to filter image using a high pass filter.

THEORY :

58
MATLAB PROGRAM :

clear,
X=imread ('lena.jpg'); %read picture as int8 matrix
x=single(X(50:200,50:200)); % crop and convert int8 to single
figure, imagesc(x), colormap gray,title('oryginal: cropped and
converted to single'), % display
x=x/max(max(x)); %normalizacja [0,1]
filtr=-ones(3); filtr(2,2)=8 % define high-pass filter
xf=conv2(x,filtr,'same'); %filtracja
xf8=uint8(256*xf); %convert result to uint8
figure, imagesc(xf8), colormap gray,title('filtr=-ones(3);
filtr(2,2)=8'), % display
imwrite(xf8,'fgp8.jpg','jpg') % save as *jpg file
%return
filtr=-ones(3); filtr(2,2)=9 % define another high-pass filter
xf=conv2(x,filtr,'same'); %filtracja
xf8=uint8(256*xf); %convert result to uint8 figure,
imagesc(xf8), colormap gray,title('filtr=-ones(3);
filtr(2,2)=9'),
imwrite(xf,'fgp9.jpg','jpg') % save as *jpg file

OUTPUT :

oryginal: cropped and converted to single filtr=-ones(3); filtr(2,2)=8

20 20

40 40

60 60

80 80

100 100

120 120

140 140

20 40 60 80 100 120 140 20 40 60 80 100 120 140

59
filtr=-ones(3); filtr(2,2)=9

20

40

60

80

100

120

140

20 40 60 80 100 120 140

CONCLUSIONS :

60
Practical - 20

AIM : To write a program to blur and deblur an image.

THEORY :

61
MATLAB PROGRAM :

clc;
close all;
clear all;
f=imread('cameraman.tif');
subplot(1,2,1);imshow(f);title('original image');
LEN = 31;
THETA = 90;
PSF = fspecial('motion',LEN,THETA);
Blurred = imfilter(f,PSF,'circular','conv');
subplot(1,2,2);imshow(Blurred);title('Blurred Image');
% deblurring
figure,subplot(1,2,1);imshow(Blurred);title('Blurred image');
wnr1 = deconvwnr(Blurred,PSF);
subplot(1,2,2);imshow(wnr1);title('Deblurred Image');
title('Restored, True PSF');

OUTPUT :

original image Blurred Image

62
Blurred image Restored, True PSF

CONCLUSIONS :

63
Practical - 21

AIM : To write a program to blur a part of an image.

THEORY :

64
MATLAB PROGRAM :

clc; % Clear the command window.


close all; % Close all figures (except those of imtool.)
clear all; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
I = imread('eight.tif');
subplot(2,2,1);
imshow(I);
title('Original');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

c = [222 272 300 270 221 194];


r = [21 21 75 121 121 75];
BW = roipoly(I,c,r);
subplot(2,2,2);
imshow(BW, []);
title('ROI');
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

H = fspecial('unsharp');
J1 = roifilt2(H,I,BW);
subplot(2,2,3);
imshow(J1)
title('Sharpened');

windowSize = 13;
H = ones(windowSize, windowSize) / windowSize^2;
J2 = roifilt2(H,I,BW);
subplot(2,2,4);
imshow(J2);
title('Blurred');

65
OUTPUT :

Original ROI

Sharpened Blurred

CONCLUSIONS :

66
Practical - 22

AIM : To write a program to apply liner and log transformation on


image.

THEORY :

67
MATLAB PROGRAM :

%%
% Clear History
clear all
close all

%%

%%
% Importing Image in Workspace
I=imread('person.jpg');
%%
%%
% Converting from RGB to GRAY
I=rgb2gray(I);
%%
% Displaying image
imshow(I);title(' Original image');

%%
% Linear transformation y=m*x+c ; here m=2 and c=0
I1=2.*I;% Linear transformation y=m*x+c ; here m=2 and c=0
figure
imshow(I1);title(' Linear Transformed image , m=2');
%%

%%
% Linear transformation y=m*x+c ; here m=0.5 and c=0
I2=0.5.*I;% Linear transformation y=m*x+c ; here m=0.5 and c=0
figure
imshow(I2);title('Linear Transformed image , m=0.5 ');
%%

%%
% Log transformation s=c*log10(1+r) ; Here c=L/log10(1+L)
I3=im2double(I);
I3=255.*I3;
I4=log10(1+I3);
I4=round(104.18.*I4);% Log Transformation s=c*log10(1+r)
%Here c=L/log10(1+L) and r=Intensity of Image
figure
imshow(uint8(I4));title(' Log Transformed image');
%%
%%
% Both Images in Single Window
figure
subplot(1,2,1);subimage(I);title(' Original image');
subplot(1,2,2);subimage(uint8(I4));title(' Log Transformed
image');

68
OUTPUT :

Original image Linear Transformed image , m=2

Linear Transformed image , m=0.5 Log Transformed image

Original image Log Transformed image

50 50
100 100

150 150

200 200

250 250
100 200 300 100 200 300

CONCLUSIONS :

69
Practical - 23

AIM : To write a program to illustrate the effect of Laplacian Derivative


on an image.

THEORY :

70
MATLAB PROGRAM :

% Matlab program <image_laplacian.m>


% This program is used to observe a Laplacian Filtered Image.

clc;
clear all;
close all;

A=imread('moon.tif');

figure(1);
subplot(1,1,1)
imshow(A)
title('Original Image');

figure(2);
h1= fspecial('laplacian',1.0)
L1=imfilter(A,h1);
subplot(2,2,1)
imshow(L1)
title('Laplacian Image(h1)');

I1=A-L1;
subplot(2,2,2)
imshow(I1)
title('Image by laplacian mask(h1),c=-1');

h2 =-1*fspecial('laplacian',0.2)
L2=imfilter(A,h2);
subplot(2,2,3)
imshow(L2)
title('Laplacian Image(h2)');

I2=A+L2;
subplot(2,2,4)
imshow(I2)
title('Image by laplacian mask(h2), c=1');

OUTPUT :

h1 =

0.5000 0 0.5000

0 -2.0000 0

71
0.5000 0 0.5000

h2 =

-0.1667 -0.6667 -0.1667

-0.6667 3.3333 -0.6667

-0.1667 -0.6667 -0.1667


Original Image
Laplacian Image(h1) Image by laplacian mask(h1),c=-1

Laplacian Image(h2) Image by laplacian mask(h2), c=1

CONCLUSIONS :

72
Practical - 24

AIM : To write a program to illustrate the effect of Unsharp Mask and


Highboost filtering.

THEORY :

73
MATLAB PROGRAM :

% Matlab program <unsharp_highboost.m>


% This program is used to observe the effect of Unsharp Mask
and Highboost filtering on a blurred image.

clc;
clear all;
close all;

A=imread('dip.tif');
figure(1);
subplot(3,2,1)
imshow(A);
title('Original Image');

hsize=input('enter the value of filter size:');


h=fspecial('average',hsize);
blur=imfilter(A,h);
subplot(3,2,2)
imshow(blur);
title('Bluured Image');

subplot(3,2,3)
mask=A-blur;
imshow(mask),
title('unsharp mask');

subplot(3,2,4)
Iunsharp=A+1*mask;
imshow(Iunsharp),
title('unsharp image');

subplot(3,2,5) %highboost
filtering
highmask=3*mask;
imshow(highmask),
title('highboost mask');

subplot(3,2,6)
Ihb=A+highmask;
imshow(Ihb);
title('highboost Image');

74
OUTPUT :

enter the value of filter size:9

Original Image Bluured Image

unsharp mask unsharp image

highboost mask highboost Image

CONCLUSIONS :

75
Practical - 25

AIM : Write a MATLAB program illustrating the difference between


arithmetic mean filter and Geometric mean filter in removing
Gaussian noise.

THEORY :

76
MATLAB PROGRAM :

clc;
clear all;
close all;

i=imread('India.jpg');
I=rgb2gray(i);
I=im2double(I);
figure(1);
imshow(I);
title('original image');

I1=imnoise(I,'gaussian');
figure(2);
imshow(I1);
title('Image formed by adding guassin noise');

w=fspecial('average');
I2=imfilter(I1,w);
figure(3);
imshow(I2);
title('Image recovered by Arithmetic mean');

f = @(x) geomean((x(:)));
I3= nlfilter(I,[3 3],f);
figure(4);
imshow(I3);
title('Image recovered by Geometric mean');

OUTPUT :

original image Image formed by adding guassin noise

77
Image recovered by Arithmetic mean Image recovered by Geometric mean

CONCLUSIONS :

78
Practical - 26

AIM : To write a program to illustrate the effect of Square Averaging


filter of different masks on an image.

THEORY :

79
MATLAB PROGRAM :

% Matlab program <image_averaging.m>


% This program is used to observe the results of smoothing due
to square averaging filters of size m=3,9,15.

clc;
clear all;
close all;
A=imread('lena.tif');
sa=size(A)
subplot(2,2,1)
imshow(A),
title('The Original Image');
Ha3= fspecial('average',3);
Ia3=imfilter(A,Ha3);
subplot(2,2,2)
imshow(Ia3),
title('Image smoothened by filter of size 3x3');

Ha9= fspecial('average',9);
Ia9=imfilter(A,Ha9);
subplot(2,2,3);
imshow(Ia9);
title('Image smoothened by filter of size 9x9');

Ha15= fspecial('average',15);
Ia15=imfilter(A,Ha15);
subplot(2,2,4)
imshow(Ia15),
title('Image smoothened by filter of size 15x15');

80
OUTPUT :

The Original Image Image smoothened by filter of size 3x3

Image smoothened by filter of size 9x9 Image smoothened by filter of size 15x15

CONCLUSIONS :

81
Practical - 27

AIM : To write a program to observe the effect of order-statistics filter


like Median Filter of size 3x3 on an image corrupted by salt &
pepper noise.

THEORY :

82
MATLAB PROGRAM :

clc;
clear all;
close all;
I=imread('pcb.tif');
figure(1);
imshow(I);
title('Fig a:The original image: X-ray image of circuit
board');

In=imread('pcbn.tif');
figure(2);
subplot(2,2,1)
imshow(In)
title('Fig b:Original image(a)corrupted by salt & pepper
noise');

Im = medfilt2(In);
subplot(2,2,3)
imshow(Im),
title('Fig c:Filtered Image with a 3x3 mask on Fig(b)');

OUTPUT :
Fig a:The original image: X-ray image of circuit board
Fig b:Original image(a)corrupted by salt & pepper noise

Fig c:Filtered Image with a 3x3 mask on Fig(b)

CONCLUSIONS :

83
Practical - 28

AIM : To write a MATLAB program to observe the result of applying the


following steps on the given image.

6) Multiplying the given image by (-1)^x+y,


7) Computing the DFT,
8) Taking the complex conjugate of the transform,
9) Computing the inverse DFT,and
10) Multiplying the real part of the result by (-1)^(x+y)

THEORY :

84
MATLAB PROGRAM :

% This program is used to observe the change that occurs in


%the given image after following the steps given above.

clc;
clear all;
close all;

A=imread('dip.tif');
s=size(A);

M=s(1);
x=M;

N=s(2);
y=N;

for x=1:1:M
for y=1:1:N
t=(-1)^(x+y);
b(x,y)=A(x,y).*t;
end
end

Y = fft2(double(b));

YC = conj(Y);

Y1 = ifft2(double(YC));

for x=1:1:M
for y=1:1:N
t=(-1)^(x+y);
I(x,y)=Y1(x,y).*t;
end
end

figure(1);
imshow(I)
title('Fig a:The Output image after applying the given
steps.');

figure(2);
L = medfilt2(I,[3 3]);
imshow(L)

85
title('Fig b:Smoothened image by applying median filter on Fig
a');

OUTPUT :

Fig a:The Output image after applying the given steps. Fig b:Smoothened image by applying median filter on Fig a

CONCLUSIONS :

86

You might also like