You are on page 1of 28

INDRAPRASTHA COLLEGE FOR WOMEN

UNIVERSITY OF DELHI

DIGITAL IMAGE
PROCESSING

PRACTICAL FILE
2022-23

Submitted by: Submitted to:


Nagadi Leela Rao Ms. Seema Jangra
College Roll No: 21/CS/28
Course: B.Sc.(H) Computer Science
Semester/Year: V/3 (Paper code- 32347509)
ACKNOWLEDGEMENT

I hereby would like to acknowledge the help and guidance


provided by Ms. Seema Jangra, my Digital Image
Processing lecturer, in completing this practical file.

I would also like to thank my family for their constant


support and my friends and classmates who helped me
traverse through any problems that I faced in the process of
completing this file.

Nagadi Leela Rao


INDEX

S. No. Practical question Pg Nos


1 Basic operations on SCILAB/MATLAB 1-9
2 Point processing methods 10-12
3 Arithmetic operations on images 13-14
4 Logical operations on images 15-16
5 Histogram calculation and equilization 17-19
6 Run length encoding 20
7 Noise reduction 21-23
8 Low pass and High pass filters 24-25
1. Write program to read and display digital image using MATLAB or SCILAB

a. Become familiar with SCILAB/MATLAB Basic commands


Declaring matrices
Source Code:
Matrix= [1 2 3; 4 5 6; 7 8 9]
Output:

Populating matrix elements with zeros


Source Code:
Zero= zeros(3,3)
Output:

Knowing size of matrix


Source Code:
A= [1 2; 3 4; 5 6]
[rows, cols]=size(A);
rows
cols

1|Page
Output:

b. Read and display image in SCILAB/MATLAB


Reading an image
Source Code:
myImage= imread('lena_color_256.tif')
Output:

2|Page
Displaying Image
Source Code:
myImage= imread('lena_color_512.tif');
imshow(myImage);
Output:

c. Resize given image


Knowing size of image in pixels
Source Code:
myImage= imread('lena_color_256.tif');
[rows, cols]=size(myImage)
Output:

3|Page
Image Resizing
Source Code:
myImage= imread('lena_color_256.tif');
myImage2= imresize(myImage, [512, 512], 'nearest');
size(myImage2)
Output:

d. Convert given color image into grayscale image


Source Code:
myImage= imread('lena_color_256.tif');
myGrayImage=rgb2gray(myImage);
imshow(myGrayImage);
Output:

4|Page
e. Convert given color/gray-scale image into black and white image
Source Code:
myImage= imread('lena_color_256.tif');
myBWImage=im2bw(myImage, 0.5);
imshow(myBWImage);
Output:

f. Draw Image profile


Source Code
myImage= imread('lena_color_256.tif');
improfile(myImage);
Output:

5|Page
g. Separate color image into three separate R,G,B planes
Source Code:
myImage=imread('lena_color_256.tif');
RED= myImage(:, :, 1);
GREEN= myImage(:, :, 2);
BLUE= myImage(:, :, 3);
subplot(1, 3, 1);
imshow(RED);
title("Red plane");
subplot(1, 3, 2);
imshow(GREEN);
title("Green plane");
subplot(1, 3, 3);
imshow(BLUE);
title("Blue plane");
Output:

6|Page
h. Create color image using R, G and B three separate planes
Source Code:
newImage= cat(3, RED, GREEN, BLUE);

imshow(newImage);
Output:

i. Flow control and loop in SCILAB


Flow control in SCILAB
Source Code:
myImage=imread('lena_color_256.tif');
a=0
if (Rows==256 && Cols==256) then
a=256;
else
a='not 256x256 pixels';
end
a

7|Page
Output:

Loops in SCILAB
Source Code:
myImage= imread('lena_color_256.tif');
[m n]= size(myImage);
c=0;
for i=1:m
for j=1:n
if myImage(i,j)<100 then
c=c+1;
end
end
end
disp('Number of pixels with intensity less than 100:', c);

Output:

8|Page
j. Write given 2-D data in image file
Source Code:
myImage= imread('woman_darkhair-Copy.tif');
for i=1:256
for j=1:256
if myImage(i, j)<200 then
myImage(i, j)=0;
else
myImage(i, j)=511;
end
end
end
imwrite(myImage, 'woman_darkhair-Copy.tif');
newImg= imread('woman_darkhair-Copy.tif');
imshow(newImg);

Output:

9|Page
2. To write and execute image processing programs using point processing
method
a. Obtain Negative image
b. Obtain Flip image
c. Thresholding
d. Contrast stretching
Source Code:
//Negative Image
myImage= imread('peppers_color.tif');
Compli= imcomplement(myImage);
figure;
subplot(231);
imshow(myImage);
title('Original Image');
subplot(232);
imshow(Compli);
title('a)Negative Image');

//Flip image
Horflp= flipdim(myImage, 2);
subplot(233);
imshow(Horflp);
title('b)Flip Image');

//Threshold
myImage2= imread('peppers_gray.tif');
[M, N] = size(myImage2);
t= input('Enter the threshold value: ');
for i = 1:M

10 | P a g e
for j=1:N
if myImage2(i,j)<t then
myImage2thres(i,j)=0;
else
myImage2thres(i,j)=255;
end
end
end
subplot(234);
imshow(myImage2thres);
title('c)Thresholding');
xlabel(sprintf('threshold value is %g',t));

//Contrast Stretching
a=0;
b=511;
c= min(myImage2(:));
d= max(myImage2(:));
myImage2= (myImage2-c)*(511/(d-c));
subplot(235);
imshow(myImage2);
title('d) Contrast Stretching');
xlabel('Gray image used');

11 | P a g e
Output:

12 | P a g e
3. To write and execute programs for image arithmetic operations
a. Addition of two images
b. Subtract one image from other image
c. Calculate mean value of image
Source Code:
image1= imread('cameraman.tif');
I1= imresize(image1, [250, 250]);
meanI1= mean2(rgb2gray(image1));

image2= imread('house.tif');
I2= imresize(image2, [250, 250]);
meanI2= mean2(rgb2gray(image2));

figure;
subplot(221);
imshow(image1);
title('Original Image 1');
xlabel(sprintf('Mean: %g',meanI1));
subplot(222);
imshow(image2);
title('Original Image 2');
xlabel(sprintf('Mean: %g',meanI2));

Addimage= imadd(I1, I2);


Addmean= mean2(rgb2gray(Addimage));
subplot(223);
imshow(Addimage);
title('Addition of I1 and I2');
xlabel(sprintf('Mean: %g',Addmean));

13 | P a g e
Subimage= imsubtract(I1, I2);
Submean= mean2(rgb2gray(Subimage));
subplot(224);
imshow(Subimage);
title('Subtraction of I2 from I1');
xlabel(sprintf('Mean: %g',Submean));

Output:

14 | P a g e
4. To write and execute programs for image logical operations
a. AND operation between two images
b. OR operation between two images
c. Calculate intersection of two images
d. NOT operation (Negative image)
Source Code:
image1= imread('ball1.png');
I1= rgb2gray(imresize(image1, [250, 250]));
subplot(231);
imshow(I1);
title('Image1');

image2= imread('ball2.png');
I2= rgb2gray(imresize(image2, [250, 250]));
subplot(232);
imshow(I2);
title('Image2');

Anded= bitand(I1, I2);


subplot(233);
imshow(Anded);
title('a)AND operation');

Ored= bitor(I1, I2);


subplot(234);
imshow(Ored);
title('b)OR operation');

intersection = ones(250, 250, 'uint8');


for i=1:250
for j=1:250

15 | P a g e
if I1(i, j)==I2(i, j) then
intersection(i, j)= int(I1(i, j));
else
intersection(i, j)= 255;
end
end
end
subplot(235);
imshow(intersection);
title('c)Intersection');

Not1= bitcmp(I1);
subplot(236);
imshow(Not1);
title('d)NOT operation (image 1)');

Output:

16 | P a g e
5. To write a program for histogram calculation and equalization using
a. Standard MATLAB function
Source Code:
myImage= imread('mandril_gray.tif');
[counts,cells] = imhist(myImage);
subplot(221)
imshow(myImage)
subplot(222)
plot2d3('gnn',cells,counts);
title("Histogram plot of Original Image");

Theq = imhistequal(myImage);
[counts,cells]=imhist(Theq);
subplot(223)
imshow(Theq);
subplot(224)
plot2d3('gnn',cells,counts);
title('Histogram of Histogram Equalized Image');

Output:

17 | P a g e
b. Program without using standard MATLAB functions
Source Code
myImage=imread('mandril_gray.tif');
myImage=rgb2gray(myImage);
[rows cols]=size(myImage);
img2=myImage;

myhist=zeros(256,1);
for i=0 : 255
myhist(i+1)=length(find(myImage==i));
end

cdf=zeros(256,1);
cdf(1)=myhist(1);
for j=2 : 256
cdf(j)=cdf(j-1)+myhist(j);
end

cumprob=cdf/(rows*cols);
equalizedhist=floor((cumprob)*255);
for i=1 : cols
for j=1 : rows
for m = 0 : 255
if(myImage(i,j)==m)
img2(i,j)=equalizedhist(m+1);
end
end
end
end

18 | P a g e
myeqhist=zeros(256,1);
for k=0 : 255
myeqhist(k+1)=length(find(img2==k));
end
figure(1);
subplot(211);
imshow(myImage);
title('Original Image');
subplot(212);
bar(myhist);
title('Histogram of Original Image');
figure(2);
subplot(211);
imshow(img2);
title('Histogram Equalized Image');
subplot(212);
bar(myeqhist);
title('Equalized Histogram of Image');

Output:

19 | P a g e
6. Write a program to implement Run Length Coding in One Dimensional Array.
Source Code:
arr= input("Enter the array: ");
runcode=[];
c=1;
for i=1: length(arr)-1
if(arr(i)==arr(i+1))
c=c+1;
else
runcode=[runcode, c, arr(i)];
c=1;
end
end
runcode=[runcode, c, arr(length(arr))];
disp('Run length coding: ', runcode);

Output:

20 | P a g e
7. To understand various image noise models and to write programs for
a. image restoration
b. Remove Salt and Pepper Noise
c. Minimize Gaussian noise
d. Median filter
Source Code:
myImage= imread('house.tif');
grayImg= rgb2gray(myImage);
subplot(231);
imshow(grayImg);
title('Original image');

//Add salt and pepper noise


noiseImg= imnoise(grayImg,'salt & pepper'); //default density 0.05
subplot(232);
imshow(noiseImg);
title('Image with salt & pepper noise');

//removal of salt & pepper noise


kmedian = immedian(noiseImg,3); //apply median filter
subplot(233);
imshow(kmedian);
title('b)Removing salt and pepper noise');

//add gaussian noise


gausImg= imnoise(grayImg,'gaussian',0,0.01);
subplot(234);
imshow(gausImg);
title('Image with Gaussian noise')

21 | P a g e
//Gaussian Filter
gauss=fspecial('gaussian',[3,3],0.5)
corrected= imfilter(gausImg, gauss);
subplot(235);
imshow(corrected);
title('c)Minimising Gaussian noise');

//median filter
img2= double(noiseImg);
[m,n] = size(img2);

for i=2:m-1
for j=2:n-1
arr= [img2(i-1, j+1), img2(i, j+1), img2(i+1, j+1), img2(i-1, j), img2(i, j), img2(i+1, j), img2(i-1,
j-1), img2(i, j-1), img2(i+1, j-1)];
b(i, j)= median(arr);
end
end

c=uint8(b);
subplot(236);
imshow(c);
title('d)Applying median filter');

22 | P a g e
Output:

23 | P a g e
8. Write and execute programs to use spatial low pass and high pass filters.
Source Code:
myImage= imread('house.tif');
myImage= rgb2gray(myImage);
subplot(131);
imshow(myImage);
title('Original Image');

//low pass
img= double(myImage);
[m,n] = size(img);
w = [1 1 1;1 1 1;1 1 1]; //low pass average filter
for i=2:m-1
for j=2:n-1
b(i,j)=[w(1)*img(i-1,j+1)+w(2)*img(i,j+1)+w(3)*img(i+1,j+1)+w(4)*img(i-
1,j)+w(5)*img(i,j)+w(6)*img(i+1,j)+w(7)*img(i-1,j-1)+w(8)*img(i,j-1)+w(9)*img(i+1,j-1)]/9
end
end
lpimg=uint8(b);
subplot(132); imshow(lpimg);
title('Low pass filtered (Avg.)');

//high pass
img=double(myImage);
[m,n] = size(img);
w = [0 -1 0,-1 4 -1,0 -1 0]; //high pass laplacian filter
for i=2:m-1
for j=2:n-1
d(i,j)=[w(1)*img(i-1,j+1)+w(2)*img(i,j+1)+w(3)*img(i+1,j+1)+w(4)*img(i-
1,j)+w(5)*img(i,j)+w(6)*img(i+1,j)+w(7)*img(i-1,j-1)+w(8)*img(i,j-1)+w(9)*img(i+1,j-1)]/9

24 | P a g e
end
end
hpimg= uint8(d);
subplot(133);
imshow(hpimg);
title('High pass filtered (Laplacian)')

Output:

25 | P a g e

You might also like