Professional Documents
Culture Documents
DIPpracticalfile
DIPpracticalfile
UNIVERSITY OF DELHI
DIGITAL IMAGE
PROCESSING
PRACTICAL FILE
2022-23
1|Page
Output:
2|Page
Displaying Image
Source Code:
myImage= imread('lena_color_512.tif');
imshow(myImage);
Output:
3|Page
Image Resizing
Source Code:
myImage= imread('lena_color_256.tif');
myImage2= imresize(myImage, [512, 512], 'nearest');
size(myImage2)
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:
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:
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));
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');
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');
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