You are on page 1of 19

DIGITAL

IMAGE
PROCESSING
LAB

Name: Anubhav Shrivastava

Roll Number: 1005210009

B.Tech [2014 Batch]

Computer Science and engineering, Final Year,


INDEX
NO SUBJECT SIGN
1 To write a program to display an
image and its histogram.
2 To write a program to find negative
of an image.
3 To write a program for the display
of Image in Grayscale, Red, Green
and Blue.
4 To write a program to learn Image
Segmentation Processing.
5 To write a program to show Image
Enhancement.
6 To write a program to show
histogram equalization.
7 To write a program to illustrate
Gray level(Intensity) slicing.
8 To write a program to illustrate bit
plane slicing.
9 To write a program to show edge
detection.

10 To write a program to filter image using a


high pass filter.

11 To write a program to apply liner and log


transformation on image.

12 To write a program to illustrate the effect of


Square Averaging filter of different masks on
an image.

13 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.
14 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,
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)
Practical -1

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

MATLAB PROGRAM :

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

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

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
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');
Practical - 3

AIM : To write a program for thedisplay of Image in Grayscale, Red, Green


and Blue

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
Practical - 4
AIM : To write a program to learn Image Segmentation Processing.

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)

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

AIM : To write a program to show Image Enhancement.

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

AIM : To write a program to show histogram equalization.

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

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

MATLAB PROGRAM :

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

%%
%
ClearHistory
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
Practical - 8

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

MATLAB PROGRAM :

clc;
close all; clear all;

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

% Get all eight bitpl anes 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);

title('b5')

subplot(223);
imshow(b6);
title('b6')
subplot(224);
imshow(b7);
title('b7')
Practical - 9

AIM : To write a program to show edge detection.

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');
Practical - 10

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

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
Practical - 11

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


image.

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');
Practical - 12

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


Averaging filter of different masks on an image.
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;
clea
r
all;
clos
e
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');
Practical - 13

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.
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)');
Practical - 14

AIM : 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)

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)
title('Fig b:Smoothened image by applying median filter on Fig
a');

You might also like