Fundamentals of Image Processing Lab Manual SET 1
Fundamentals of Image Processing Lab Manual SET 1
Image Processing
Laboratory Manual
CERTIFICATE
This
is
to
certify
that
Mr/Miss
Date:
Page 2
List of Experiments
Sr.
No.
1.
Name of Experiment
11.
12.
13.
14.
15.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Solve exercise given at the end of each practical, write answers and execute it
Write and execute all programs either in MATLAB or SCILAB. You can cut and
paste results of image processing in soft copy of this lab manual. Write all the
programs in separate folder. Prepare your folder in your laptop/computer.
Page 3
EXPERIMENT NO. 1
AIM: Write program to read and display digital image using MATLAB or
SCILAB
Introduction of SCILAB:
SciLab is a programming language for mathematical operations It provides
development environment for mathematical programming. It is very much
useful for matrix manipulation hence it is good choice for signal processing
and image processing applications. It is similar to MATLAB. MATLAB is
commercial software while SCILAB is open source software. It has one
thousand four hundred in-built functions and it is growing day by day.
Image and Video processing toolbox will be useful for this subject.
Students can download free SCILAB software from following website:
http://www.scilab.in
You can also create your log in on the e-Prayog website:
http://59.181.142.81 and get scilab link from there. For image processing
experiments you need to download SIVP (Speech, Image, Video Processing)
Toolbox.
Saving and Loading Programs in SCILAB:
SCILAB can be used as a prototyping environment in which we try things
out, examine the results and then adjust our programs to give better
performance. We can give SCILAB commands on command prompt. For
large number of commands, we can use SCILAB editor. We can write
sequence of commands in SCILAB editor and then save as .sce files. We can
execute .sce file to see the execution of the program.
Some commands and programs in SCILAB:
[1] Declaring matrices:
In SciLab, there is no need to declare variables; new variables are simply
introduced as they are required.
A = [1 2 3; 4 5 6; 7 8 9]
The above SCILAB command declares following matrix:
1 2 3
A = 4 5 6
7 8 9
Keep following things in mind while declaring variables/matrices:
No spaces
Dont start with a number
Variable names are case sensitive
Lab Manual of Fundamentals of Image Processing
Page 4
Page 5
Page 6
Page 7
:: WORKSHEET ::
[1] Give following commands in SCILAB, observe output and write
explanation of each command
t = 0:0.01:1
y=sin(2*%pi*10)
plot(y)
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Page 8
[2] Create image of size 512x512 black square using monochrome, 256
gray-level using paint or any other relevant software. Read and display
image using SCILAB commands
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
[3] Execute following program for the image created in exercise[2] and
observe the result. Write comments on the result :
imgData=imread('white.bmp');
[Rows,Cols]=size(imgData);
imshow(imgData);
for i=1:Rows
for j=1:Cols
if(i>Rows/4 && i<3*Rows/4)
if(j>Cols/4 && j<3*Cols/4)
imgData(i,j)=0;
end
end
end
end
figure;
imshow(imgData);
Government Engineering College, Rajkot
Page 9
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
[3] Write program to read image, create negative image, display original and
negative image : (Hint: for negative image subtract pixel value from 255)
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Fundamentals of Image Processing
Page 10
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Copy and paste original image and negative image here:
Page 11
EXPERIMENT NO. 2
AIM: Write a program to perform convolution operation for 1D and 2D data
in MATLAB/SCILAB
Program for 1D convolution:
clear;
clc;
x=input("Enter value of x: ")
y=input("Enter value of y: ")
n=length(x)
k=length(y)
for z=n+1:n+k-1
x(z)=0;
end
for u=k+1:n+k-1
y(u)=0;
end
for i=1:n+k-1
s(i)=0
for j=1:i
s(i)=(x(j)*y(i-j+1))+s(i)
end
end
subplot(3,1,1)
plot2d3(x)
subplot(3,1,2)
plot2d3(y)
subplot(3,1,3)
plot2d3(s)
Take value of x: [ 1 2 3 4 5 6 7 8 9 0 10 11 12 13 14 15]
Y: [-1 1]
Observe result of convolution and write your comments:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Fundamentals of Image Processing
Page 12
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Program for 2D convolution:
clear;
clc;
x=input("Enter value of x in matrix form: ")
y=input("Enter value of y in matrix form: ")
[xrows,xcols]=size(x);
[yrows,ycols]=size(y);
result=zeros(xrows+yrows,xcols+ycols)
for r = 1:xrows-1
for c = 1:xcols-1
sum=0;
for a=0:yrows
for b=0:ycols
sum=sum+x(r+a,c+b)*y(a+1,b+1);
end
end
result(r,c)=sum;
end
end
Enter following 2D matrix for x:
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10
Page 13
1 1 1
0
1
0
1
0
1
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Write program of 2D convolution for image i.e. get variable x by reading
image pixels, use any 3x3 mask as variable y, process the result and write
your comments
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Fundamentals of Image Processing
Page 14
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Page 15
EXPERIMENT NO. 3
AIM: To write and execute programs for image arithmetic operations
Introduction:
Arithmetic operations like image addition, subtraction,
multiplication and division is possible. If there is two images I1 and I2 then
addition of image can be given by:
I(x,y) = I1(x,y) + I2(x,y)
Where I(x,y) is resultant image due to addition of two images. x and y are
coordinates of image. Image addition is pixel to pixel. Value of pixel should
not cross maximum allowed value that is 255 for 8 bit grey scale image.
When it exceeds value 255, it should be clipped to 255.
To increase overall brightness of the image, we can add some constant value
depending on brightness required. In example program we will add value 50
to the image and compare brightness of original and modified image.
To decrease brightness of image, we can subtract constant value. In example
program, value 100 is subtracted for every pixel. Care should be taken that
pixel value should not less than 0 (minus value). Subtract operation can be
used to obtain complement (negative) image in which every pixel is
subtracted from maximum value i.e. 255 for 8 bit greyscale image.
Multiplication operation can be used to mask the image for obtaining region
of interest. Black and white mask (binary image) containing 1s and 0s is
multiplied with image to get resultant image. To obtain binary image
function im2bw() can be used. Multiplication operation can also be used to
increase brightness of the image
Division operation results into floating point numbers. So data type required
is floating point numbers. It can be converted into integer data type while
storing the image. Division can be used to decrease the brightness of the
image. It can also used to detect change in image.
Execute following MATLAB program:
%Experiment No. 3 Image Arithmetic Operations
%Image Processing Lab, E.C. Department, GEC Rajkot
%Image addition is done between two similar size of image, so image resize
%function is used to make size of both image same.
%I=I1+I2
clc
close all
I1 = imread('cameraman.tif');
I2 = imread('rice.png');
Lab Manual of Fundamentals of Image Processing
Page 16
Original image I2
Page 17
Original image I1
Bright Image I
Dark Image I
Masked Image I
Exercise:
[1] In above program, use functions imadd() functions to addition,
imsubtract() for subtraction immultiply() for multiplication operations.
Use imcomplement() function to get complement of image. Write
program again using these functions in following space.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Lab Manual of Fundamentals of Image Processing
Page 18
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
[2] Write Program to read any image, resize it to 256x256. Apply square
mask shown in following figure so that only middle part of the image is
visible.
128
256
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Page 19
EXPERIMENT NO. 4
AIM: To write and execute programs for image logical operations
Introduction: Bitwise logical operations can be performed between pixels of
one or more than one image.
AND/NAND Logical operations can be used for following applications:
Compute intersection of the images
Design of filter masks
Slicing of gray scale images
OR/NOR logical operations can be used for following applications:
Merging of two images
XOR/XNOR operations can be used for following applications:
To detect change in gray level in the image
Check similarity of two images
NOT operation is used for:
To obtain negative image
Making some features clear
Program:
Experiment No. 4 Image Logical Operations
%Image Processing Lab, E.C. Department, GEC Rajkot
%Logical operations between two image circle and pentagon is shown
clc
close all
clc
close all
myimageA=imread('circle.jpg');
myimageA=rgb2gray(myimageA);
myimageB=imread('pentagon.jpg');
myimageB=rgb2gray(myimageB);
subplot(3,2,1)
imshow(myimageA),title('Image A ');
% Display the Original Image B
subplot(3,2,2)
imshow(myimageB),title('Image B');
% Take a complement of Image A and Display it
subplot(3,2,3)
cimageA= ~myimageA ;
imshow(cimageA), title('Complement of Image A');
% Take a Ex-OR of Image A and Image B and Display it
subplot(3,2,4)
xorimage= xor(myimageA,myimageB);
imshow(xorimage), title('Image A XOR Image B');
Lab Manual of Fundamentals of Image Processing
Page 20
Image B
Complement of Image A
Image A OR Image B
Exercise:
[1] Prepare any two images of size 256x256 in paint. Save it in JPEG format
256 gray levels. Perform logical NOR, NAND operations between two images.
Write program and paste your results.
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Government Engineering College, Rajkot
Page 21
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Result after execution of program:
(Copy and paste original and processed image here)
Page 22
EXPERIMENT NO. 5
AIM: To write and execute program for geometric transformation of image
Introduction: We will perform following geometric transformations on the
image in this experiment
Translation: Translation is movement of image to new position.
Mathematically translation is represented as:
x = x +x and y = y + y
In matrix form translation is represented by:
x '
1
y' 1 = 0
0
x
y
1
0
1
0
x
y
1
x '
S x
y' 1 = 0
0
0
Sy
0
0
0
1
x
y
1
x '
cos
y' 1 = sin
0
sin
cos
0
0
0
1
x
y
1
x = shx y
y = y
Page 23
1
Xshear= sh x
0
0
1
0
0
0
1
x
y
1
x = x
y = y shy
1
Yshear= 0
0
sh y
1
0
0
0
1
x
y
1
Page 24
y = imtransform(x,tform);
subplot(2,2,4); imshow(y); title('Shear in X-Y direction');
Result after execution of program:
Orignial Image
Orignial Image
Shear in Y direction
Shear in X direction
Page 25
Exercise:
[1] In above program, modify matrix for geometric transformation and use
imtransform() function for modified matrix. Show the results and your
conclusions.
Program:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
Result after execution of program:
Conclusion:
Page 26
Page 27