Digital Image Processing Lab Manual-1 (1)
Digital Image Processing Lab Manual-1 (1)
JAIPUR
1 Introduction to MATLAB
SOFTWARE USED
MATLAB
THEORY
MATLAB stands for MATrix LABoratory and the software is built up around vectors and matrices.
It is a technical computing environment for high performance numeric computation and visualization.
It integrates numerical analysis, matrix computation, signal processing and graphics in an easy-to-use
environment, where problems and solutions are expressed just as they are written mathematically,
without traditional programming. MATLAB is an interactive system whose basic data element is a
matrix that does not require dimensioning. It enables us to solve many numerical problems in a
fraction of the time that it would take to write a program and execute in a language such as
FORTRAN, BASIC, or C. It also features a family of application specific solutions, called toolboxes.
Areas in which toolboxes are available include signal processing, image processing, control systems
design, dynamic systems simulation, systems identification, neural networks, wavelength
communication and others. It can handle linear, non- linear, continuous-time, discrete- time,
multivariable and multirate systems.
Procedure
1. Open Matlab.
2. File New Script.
3. Type the program in untitled window
4. File Save type filename.m in Matlab workspace path.
5. Debug Run.
6. Output will be displayed at Figure dialog box.
Library Functions
clc:
Clear command window
Clears the command window and homes the cursor.
clear all:
Removes all variables from the workspace.
close all:
Closes all the open figure windows.
exp:
Y = exp(X) returns the exponential ex for each element in array X.
linespace:
y = linspace(x1,x2) returns a row vector of 100 evenly spaced points between x1 and x2.
rand:
X = rand returns a single uniformly distributed random number in the interval (0,1).
ones:
X = ones(n) returns an n-by-n matrix of ones.
zeros:
X = zeros(n) returns an n-by-n matrix of zeros.
plot:
plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.
subplot:
subplot(m,n,p) divides the current figure into an m-by-n grid and creates an axes for a subplot in the
position specified by p.
stem:stem(Y) plots the data sequence, Y, as stems that extend from a baseline along the x-axis. The data
values are indicated by circles terminating each stem.
title:
title(str) adds the title consisting of a string, str, at the top and in the center of the current axes.
xlabel:
xlabel(str) labels the x-axis of the current axes with the text specified by str.
ylabel:
ylabel(str) labels the y-axis of the current axes with the string, str.
.
EXPERIMENT NO. 2
EXPERIMENT TITLE: Image Processing concept
AIM: To study the Image Processing concept.
THEORY: Digital images play an important role both in daily life applications as well as in the
areas of research technology. The digital image processing refers to the manipulation of an
image by means of processor. The different elements of an image processing system include
image acquisition, image storage, image processing and display
An image is two dimensional function that represent a message of sum characteristics such as
brightness or color of viewed scene in the first mat lab program the command used from mat
lab is imcomplement
PROGRAM:
% Program to study the image processing concept
I=imread('pout.tif');
J=imcomplement(I);
figure,imshow(I)
figure,imshow(J)
K=imadjust(I,[0;0.4],[0.5;1])
figure,imshow(K)
Result:
Original Image
Complement Image
Conclusion: Thus we have studied the how to obtain complement image from the original
image.
EXPERIMENT NO. 3
EXPERIMENT TITLE: Histogram equalization image
AIM: To obtain histogram equalization image.
PROGRAM:
% Program to obtain histogram equalization concept
I=imread('trees.tif');
J=imcomplement(I);
imhist(J,100);
imshow(I);
title('original');
figure,imshow(J);
title('complement');
I=histeq(I);
figure,imhist(I,64);
title('equilized');
figure,imhist(J,64);
title('histogram');
n=numel(I);
p=imhist(I)/n;
figure,plot(p);
title('normalized');
K=imadjust(I,[0;1],[0.4;1],0.5);
figure,imshow(K);
title('adjusted image');
T=maketform('affine',[.3 0 0;.5 1 0;0 1 1]);
tformfwd([0,0],T);
I2=imtransform(I,T);
figure,imshow(I2);
title('forward image');
Result:
Original Histogram
Equalized Histogram
Conclusion: Thus we have obtained the Equalized Histogram from the original Histogram.
EXPERIMENT NO. 4
EXPERIMENT TITLE: Averaging filter in spatial domain
AIM: To Implement smoothing or averaging filter in spatial domain.
PROGRAM:
% Program for implementation of smoothing or averaging filter in spatial domain
I=imread('trees.tif');
subplot(2,2,1);
imshow(J);
title('original image');
f=ones(3,3)/9;
h=imfilter(I,f,'circular');
subplot(2,2,2);
imshow(h);
title('averaged image');
Result:
Conclusion: Thus we have performed the smoothing or averaging filter operation on the
Original image and we get filtered image.
EXPERIMENT NO. 5
EXPERIMENT TITLE: Opening and Closing of the image
AIM: Program for opening and closing of the image.
PROGRAM:
f=imread('coins.png');
se=strel('square',20);
fo=imopen(f,se);
figure,imshow(f)
title('input image');
figure,imshow(fo)
title('opening of input image');
fc=imclose(f,se);
figure,imshow(fc)
title('opening of input image');
foc=imclose(fo,se);
figure,imshow(foc)
title('closing of opened input image');
Result:
Conclusion: Thus we have obtained the opened image and closed image from the original
Image.
EXPERIMENT NO. 6
EXPERIMENT TITLE: Region of Interest for the image
AIM: To fill the region of interest for the image.
THEORY: A region of interest (often abbreviated ROI), are samples within a data set
identified for a particular purpose. The concept of a ROI is commonly used in many
application areas. For example, in medical imaging, the boundaries of a tumor may be
defined on an image or in a volume, for the purpose of measuring its size. The endo cardial
border may be defined on an image, perhaps during different phases of the cardiac cycle,
for example, end-systole and end-diastole, for the purpose of assessing cardiac function.
In geographical information systems(GIS), a ROI can be taken literally as a polygonal
selection from a 2D map. In computer vision and optical character recognition, the ROI
defines the borders of an object under consideration. In many applications, symbolic
(textual) labels are added to a ROI, to describe its content in a compact manner. Within a
ROI may lie individual points of interest (POIs).
PROGRAM:
% Program for ROI
clc;
close all;
load trees
I=ind2gray(X,map);
imshow(I)
title('original image');
I2=roifill;
imshow(I2)
title('OUTPUT IMAGE');
Result:
Original Image
Output image
Conclusion: Thus we have filled the interested region in the original image.
EXPERIMENT NO. 7
EXPERIMENT TITLE: Edge detection algorithm
AIM: Program for edge detection algorithm.
THEORY: The Canny edge detector is an edge detection operator that uses a multi
stage algorithm to detect a wide range of edges in images. It was developed by John F.
Canny in 1986. Canny also produced a computational theory of edge detection explaining
why the technique works.
The Process of Canny edge detection algorithm can be broken down to 5 different steps:
1. Apply Gaussian filter to smooth the image in order to remove the noise
2. Find the intensity gradients of the image
3. Apply non-maximum suppression to get rid of spurious response to edge detection
4. Apply double threshold to determine potential edges
5. Track edges by hypothesis: Finalize the detection of edges by suppressing all the
other edges that are weak and not connected to strong edges.
PROGRAM:
%Program for edge detection algorithm
I=imread('coins.png');
figure,imshow(I)
title ('figure 1 original image');
h=ones(5,5)/25;
b=imfilter(I,h);
figure,imshow(b)
title ('figure 2 filtered image');
c=edge(b,'sobel');
figure,imshow(c)
title ('figure 3 edge detected output by sobel operator');
d=edge(b,'prewitt');
figure,imshow(d)
title ('figure 4 edge detected output by prewitt operator');
e=edge(b,'robert');
figure,imshow(e)
title ('figure 5 edge detected output by robert operator');
f=edge(b,'canny');
figure,imshow(f)title ('figure 6 edge detected output by canny operator');
Result:
EXPERIMENT NO. 8
EXPERIMENT TITLE: Sharpen image using gradient mask
AIM: Program of sharpen image using gradient mask.
Result:
Original Image
Conclusion: Thus we have perform the sharpening operation using gradient mask on the
Original image.
EXPERIMENT NO. 9
EXPERIMENT TITLE: Erosion and Dilation
AIM: Program for morphological operation: erosion and dilation.
With A and B as two sets in Z2 (2D integer space), the dilation of A and B is defined as
A(+)B={Z|(B )Z∩A ≠ ɸ}
In the equation,(B )Z simply means ta king the reflections of B about its origin and shifting it
by Z. Hence dilation of A with
B is a set of all displacements, Z, such that (B )Z and A overlap by at least one element.
Flipping of B about the origin and then moving it past image A is analogous to the
convolution process. In practice flipping of B is not done always.
Dilation adds pixels to the boundaries of object in an image. The number of pixels added
depends on the size and shape of the structuring element. Based on this definition, dilation
can be defined as
A(+)B={{Z|(B )Z∩A} ϵ A}
PROGRAM:
% Program for morphological operations: Erosions & Dilation
f=imread('coins.png');
B=[0 1 1;1 1 1;0 1 0];
f1=imdilate(f,B);
se=strel('disk',10);
f2=imerode(f,se);
figure,imshow(f)
title('input image');
figure,imshow(f1)title('delated image');
figure,imshow(f2)
title('eroded image');
Result:
Erroded Image
Dilated Image
Conclusion: Thus we have obtained the eroded and dilated image for the original image
EXPERIMENT NO. 10
EXPERIMENT TITLE: DCT/IDCT computation
AIM: Program for DCT/IDCT computation.
THEORY: A discrete cosine transform (DCT) expresses a finite sequence of data points in
terms of a sum of cosine functions oscillating at different frequencies. DCTs are important
to numerous applications in science and engineering, from lossy
compression of audio (e.g. MP3) and images (e.g. JPEG) (where small high-frequency
components can be discarded), to spectral methods for the numerical solution of partial
differential equations. The use of cosine rather than sine functions is critical for
compression, since it turns out (as described below) that fewer cosine functions are needed
to approximate a typical signal, whereas for differential equations the cosines express a
particular choice of boundary conditions.
Like for the DFT, the normalization factor in front of these transform definitions is merely a
convention and differs between treatments. For example, some authors multiply the
transforms by so that the inverse does not require any additional multiplicative factor.
Combined with appropriate factors of √(2/N), this can be used to make the transform
matrix orthogonal.
PROGRAM:
clc;
clear all;
close all;
m=input('Enter the basis matrix
dimension: ');
% Request user input
n=m;
alpha2=ones(1,n)*sqrt(2/n);
alpha2(1)=sqrt(1/n);
alpha1=ones(1,m)*sqrt(2/m);
alpha(1)=sqrt(1/m); % square root.
for u=0:m-1for v=0:n-1
for x=0:m-1
for y=0:n-1
a{u+1,v+1}(x+1,y+1)=alpha1(u+1)*alpha2(v+1)*...
cos((2*x+1)*u*pi/(2*n))*cos((2*y+1)*v*pi/(2*n));
end
end
end
end
mag=a;
figure(3)
% Create figure graphics object
k=1;
% Code to plot the basis
for i=1:m
for j=1:n
subplot(m,n,k)
imshow(mag{i,j},256)
k=k+1;
end
end
Enter the basis matrix dimension: 5
Result: