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

Digital Image Processing

Practical

Name – Ranjan Mehta

Roll No - 3940 (20053570027)


Practical-1 : Reading an image
pkg load image
I = imread('C:\Package\billy.jpg');
figure
imshow(I);
Practical-2 : Resize an image
resizedI = imresize(I,0.25);
figure
imshow(resizedI);
Practical-3 : Gray scale image
J = rgb2gray(I);
figure
imshow(J);
Practical-4 : Black & White image
figure
imshow(im2bw(I));
Practical-5 : Color temperature of image
r = size(I,1);
c = size(I,2);
R = zeros(r,c,3);
G = zeros(r,c,3);
B = zeros(r,c,3);

R( : , : ,1) = I( : , : ,1);
G( : , : ,2) = I( : , : ,2);
B( : , : ,3) = I( : , : ,3);

figure
subplot(1,4,1);
imshow(I);
title("Original");
subplot(1,4,2);
imshow(uint8(R));
title("Red");
subplot(1,4,3);
imshow(uint8(G));
title("Green");
subplot(1,4,4);
imshow(uint8(B));
title("Blue");
Practical-6 : Negative of an image

I = imread('C:\Package\billy.jpg);
subplot(1,3,1);
imshow(I);
Negative_Image = 255-I;
subplot(1,3,2);
imshow(Negative_Image);

for row = 1 : size(I,1)


for col = 1 : size(I,2)
Negative_Image(row,col,:)=255 - I(row,col,:);
end
end
subplot(1,3,3);
imshow(Negative_Image);
Practical-7 : Stretching of an image

subplot(1,2,1)
imshow(I);
J = imadjust(I,stretchlim(I),[]);
subplot(1,2,2);
imshow(J);
Practical-8 : Applying filter on an image
lap = [0,1,0;1,-4,1;0,1,0];
output = imfilter(I,lap);
subplot(2,2,1);
imshow(output);

filteredImage = imadd(I,output);
subplot(2,2,2);
imshow(filteredImage);
l = [0,-1,0;-1,4,-1;0,-1,0];
output2 = imfilter(I,l);
subplot(2,2,3);
imshow(output2);
filteredImage2 = imadd(I,output2);
subplot(2,2,4);
imshow(filteredImage2);
Practical-9 : Prewitt Filter
subplot(2,3,1);
imshow(I);
title('Original Image');
prewitt_H = [-1,-1,-1;0,0,0;1,1,1];
output_H = imfilter(I,prewitt_H);
subplot(2,3,2);
imshow(output_H);
title('prewitt_H');
filteredImage_H = imadd(I,output_H);
subplot(2,3,3);
imshow(filteredImage_H);
title('filteredImage_H');
prewitt_V = [-1,0,1;-1,0,1;-1,0,1];
output_V = imfilter(I,prewitt_V);
subplot(2,3,4);
imshow(output_V);
title('prewitt_V');
filteredImage_V = imadd(I,output_V);
subplot(2,3,5);
imshow(filteredImage_V);
title('filteredImage_V');
filtered_img = imadd(I,output_H);
filtered_image = imadd(filtered_img,output_V);
subplot(2,3,6);
imshow(filtered_image);
title('prewitt');
Practical-10 : Zooming an image

imshow(I);
zoom(3);
title("zoomed image");
Practical-11 : Translating an image
subplot(1,2,1);
imshow(I);
title("Original image");
set(gca, "Visible", "on");
j = imtranslate(I, [20,-40]);
subplot(1,2,2);
imshow(j);
title("translated Image");
set(gca, "Visible", "on");
Practical-12 : Shrinking an image
f = input('Enter the shrinking factor: ');
s = size(I);
s1 = s/f;
k = 1;
l = 1;

for i=1:s1
for j=1:s1
B(i,j) = I(k,l);
l = l+f;
end
l = 1;
k=k+f;
end
subplot(1,2,1);
imshow(I);
title("Original image");

subplot(1,2,2);
imshow(B);
title("shrinkled image");
Practical-13 : Salt & pepper noise
subplot(2,2,1);
imshow(I);
title("Original Image");

j = imnoise(I, 'salt & pepper', 0.05);


subplot(2,2,2);
imshow(j);
title("Noisy Image");

H = fspecial('average',[3,3]);
kaverage = imfilter(j,H);
subplot(2,2,3);
imshow(H);
title("Noise free image");

I = rgb2gray(j);
Kmedian = medfilt2(H);
subplot(2,2,4);
imshow(I);
title("Noise free image after median filter");
Practical-14 : Rotating an image
j = imrotate(I, 90);
imshow(j);
Practical-15
subplot(1,3,1);
imshow(I);
title("Input image");

g = rgb2gray(I);
subplot(1,3,2);
imshow(g);
title("Gray Image");

f_transform = double(g);
f_transform1 = double(g);
h = fspecial('prewitt');
pre1 = uint8(round(filter2(h,g)));
subplot(1,3,3);
imshow(pre1);
title("prewitt image")
Practical-16 : Laplacian filter
subplot(1,3,1);
imshow(I);
title("original image");

lap = [0,1,0;1,-4,1;0,1,0];
output = imfilter(I,lap);
subplot(1,3,2);
imshow(output);
title("lap image");

filteredimage = imadd(I, output);


subplot(1,3,3);
imshow(filteredimage);
title("filtered image");
Practical-17 : Histogram equalization

imhist(I);
J = histeq(I);
imhist(J);
imshow(J);
Practical-18 : Erosion of image
L = uint8(I);
subplot(1,3,1);
imshow(L);
L = im2bw(L);
L= double(L);
subplot(1,3,2);
imshow(L);
se = [0,1,0;1,1,1;0,1,0];
se = strel("square", 3);
erodedI = imerode(L, se);
subplot(1,3,3);
imshow(L);
Practical-19 : Edge detection of an image
L=rgb2gray(I);
BW3 = edge(L,"roberts");
subplot(2,2,1);
imshow(L);
title("original");

BW1 = edge (L,"prewitt");


subplot(2,2,2);
imshow(BW1);
title("prewitt");
BW2 = edge(L,"sobel");
subplot(2,2,3);
imshow(BW2);
title("sobel");
BW3 = edge(L,"roberts");
subplot(2,2,4);
imshow(BW3);
title("roberts");
Practical-20 : Dilation of image
i = uint8(I);
subplot(2,2,1);
imshow(i);

i = im2bw(i);
i= double(i);
subplot(2,2,2);
imshow(i);

se = [0,1,0;1,1,1;0,1,0];
se = strel("square", 3);
erodedI = imerode(i, se);
subplot(2,2,3);
imshow(i);

dilateI = imdilate(erodedI, se);


subplot(2,2,4);
imshow(i);
Practical-21
r = size(I,1);
c = size(I,2);
sth = double(I);
sum = 0;
for i = 1:r
for j = 1:c
sum = sum + sth(i,j,:);
end
end
disp(sum)
disp(sum/(r*c))
J = imadjust(sth,stretchlim(sth),[]);
figure;
imshow(J);

You might also like