Sunteți pe pagina 1din 25

AJAY KUMAR GARG ENGINEERING COLLEGE,

GHAZIABAD
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

COURSE
SEMESTER
SUBJECT
SUBJECT CODE

: B.Tech (CSE)
: VII
: Digital Image Processing LAB
: ECS-752

PREPARED BY:
Faculty Name: Neeti Chadha
Date:
Signature:
Faculty Name: Akhilesh Verma
Date:
Signature:

Prof. B. M. Kalra
HoD CSE

AKGEC/LP/ECS-554/01
REV No:00

AJAY KUMAR GARG ENGINEERING COLLEGE,


GHAZIABAD
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
LIST OF EXPERIMENTS
COURSE: B.Tech
SEMESTER: VII
SESSION: 2012-13
BRANCH: CSE
SUBJECT CODE & NAME: ECS-752, Digital Image Processing Lab
SI.
No.
1
2
3
4
5
6
7
8
9
10

NAME OF EXPERIMENT
WAP to implement conditional control sequences in MATLAB
WAP to implement loop constructs in MATLAB.
WAP to implement matrix operations in MATLAB
WAP to read, write and display images in MATLAB
WAP to perform histogram equalization in MATLAB
WAP to perform histogram specification in MATLAB
WAP to perform linear filtering on a gray scale image in MATLAB
WAP to perform negative of a gray scale image in MATLAB
WAP to implement color models in VLAB
WAP to perform morphological operations gray scale image in VLAB

Neeti Chadha
Akhilesh Verma

Prof.B.M.Kalra
HoD CSE

Experiment- 1
Program Name:
WAP to implement conditional control sequences in MATLAB

TheoryConcepts:
IF STATEMENT
Often, it is necessary to take alternative actions depending on circumstances.
The IF statement lets you execute a sequence of statements conditionally. That is, whether the
sequence is executed or not depends on the value of a condition. There are three forms
of IF statements: IF-THEN, IF-THEN-ELSE, and IF-THEN-ELSIF.

Code:
a = 1;
b = 2;
if a<b
fprintf('Hello World !!! \n');
else fprintf('If loop is executed !!! \n');
end

% program saved by p1.m


Output:
>> p1
Hello World!!!
>>

Experiment No.-2
Program Name:
WAP to implement loop constructs in MATLAB.

Theory Concepts:
WHILE-LOOP
The WHILE-LOOP statement associates a condition with a sequence of statements enclosed
by the keywords LOOP and END LOOP, as follows:
WHILE condition LOOP
sequence_of_statements
END LOOP;
Before each iteration of the loop, the condition is evaluated. If the condition is true, the
sequence of statements is executed, then control resumes at the top of the loop. If the
condition is false or null, the loop is bypassed and control passes to the next statement.
FOR LOOP
Whereas the number of iterations through a WHILE loop is unknown until the loop
completes, the number of iterations through a FOR loop is known before the loop is
entered. FOR loops iterate over a specified range of integers. The range is part of an iteration
scheme, which is enclosed by the keywords FOR and LOOPS. A double dot (..) serves as the
range operator. The syntax follows:
FOR counter IN [REVERSE] lower_bound..higher_bound LOOP
sequence_of_statements
END LOOP;
The range is evaluated when the FOR loop is first entered and is never re-evaluated.

FOR LOOP
clc;
a = 1;
for a=1:1:10
disp(a);
end

% program saved by p2.m

WHILE LOOP
clc;
i=1;
while i<5
fprintf('iteration %d \n',i);
i=i+1;
end

% program save as p3.m

Output:
1
2
3
4
5
6
7
8
9
10
>>

Output:
iteration 1
iteration 2
iteration 3
iteration 4
>>

Experiment No-3
Program name:
WAP to implement matrix operations in MATLAB

Theory Concepts:
This is a demonstration of some aspects of the MATLAB language.

Code:
A = [1 2 0; 2 5 -1; 4 10 -1]
B = A'
C=A*B
C = A .* B
X = inv(A)
I = inv(A) * A
eig(A)
svd(A)

Output:
A =
1

-1

10

-1

10

-1

-1

12

24

12

30

59

24

59

117

25

-10

-10

-2

-2

-1

-2

B =

C =

C =

X =

I =

ans =
12.3171
0.5149
0.1577

Experiment No. -4

Program Name:
WAP to read, write and display images in MATLAB

Theory:
In the MATLAB workspace, most images are represented as two-dimensional arrays
(matrices), in which each element of the matrix corresponds to a single pixel in the displayed
image. For example, an image composed of 200 rows and 300 columns of different colored
dots stored as a 200-by-300 matrix. Some images, such as RGB, require a three-dimensional
array, where the first plane in the third dimension represents the red pixel intensities, the
second plane represents the green pixel intensities, and the third plane represents the blue
pixel intensities.
This convention makes working with graphics file format images similar to working with any
other type of matrix data. For example, you can select a single pixel from an image matrix
using normal matrix subscripting:
I(2,15)
This command returns the value of the pixel at row 2, column 15 of the image I.

Code:
To read an image
>> O=imread('D:/163.jpg');
To show an image

>> imshow(O);
To write an image

>>imwrite(I,D:/grayimage.jpg,jpg);

Output:

Experiment No. - 5
Program Name:
WAP to perform histogram equalization in MATLAB

Theory Concepts:
Histogram Processing
The histogram of a digital image with intensity levels in the range [0, L-1] is a discrete
function h(rk)B=nk, where rk is the kth intensity level and nk is the number of pixels in the
image with intensity rk. It is common practice to normalize a histogram by dividing each of
its component by the total number of pixels in the image, denoted by the product MN, where
M and N are the row and column dimensions of the image. Thus, a normalized histogram is
given by
p(rk) = nk / MN for k=0, 1, 2,..,L-1.

Code:
GIm=imread('tire.tif');
numofpixels=size(GIm,1)*size(GIm,2);
figure,imshow(GIm);
title('Original Image');
HIm=uint8(zeros(size(GIm,1),size(GIm,2)));
freq=zeros(256,1);
probf=zeros(256,1);
probc=zeros(256,1);
cum=zeros(256,1);
output=zeros(256,1);
%freq counts the occurrence of each pixel value.
%The probability of each occurrence is calculated by probf.
for i=1:size(GIm,1)
for j=1:size(GIm,2)
value=GIm(i,j);
freq(value+1)=freq(value+1)+1;
probf(value+1)=freq(value+1)/numofpixels;
end
end
sum=0;
no_bins=255;

%The cumulative distribution probability is calculated.

for i=1:size(probf)
sum=sum+freq(i);
cum(i)=sum;
probc(i)=cum(i)/numofpixels;
output(i)=round(probc(i)*no_bins);
end
for i=1:size(GIm,1)
for j=1:size(GIm,2)
HIm(i,j)=output(GIm(i,j)+1);
end
end
figure,imshow(HIm);
title('Histogram equalization');
figure('Position',get(0,'screensize'));
dat=cell(256,6);
for i=1:256
dat(i,:)={i,freq(i),probf(i),cum(i),probc(i),output(i)};
end
columnname = {'Bin', 'Histogram', 'Probability', 'Cumulative histogram','CDF','Output'};
columnformat = {'numeric', 'numeric', 'numeric', 'numeric', 'numeric','numeric'};
columneditable = [false false false false false false];
t = uitable('Units','normalized','Position',..[0.1 0.1 0.4 0.9], 'Data', dat,... 'ColumnName',
columnname,...'ColumnFormat', columnformat,'ColumnEditable', columneditable,...
'RowName',[]);
subplot(2,2,2); bar(GIm);
title('Before Histogram equalization');
subplot(2,2,4); bar(HIm);
title('After Histogram equalization');

Output :

Experiment No. - 6

Program Name:
WAP to perform histogram specification in MATLAB

Theory Concepts:
Histogram Specification essentially gives us the capability to change the histogram of a
image to any specified histogram. Here the specified histogram is taken from an image.
However this process is a little inaccurate on account of the grey levels which may be
missing from the specified histogram and due to rounding of errors in computing the inverse
function.

Code:
% Clear all previous data
clc, clear all, close all;
% Load input & reference image
img_src=imread(input_image);
ref=imread(reference_image);
% Separate input images color channel
imgr = img_src(:,:,1);
imgg = img_src(:,:,2);
imgb = img_src(:,:,3);
%Separate reference images color channel
imgr2 = ref(:,:,1);
imgg2 = ref(:,:,2);
imgb2 = ref(:,:,3);
%% PreProcessing
% Compute inputs histogram
Hnimgr = imhist(imgr)./numel(imgr);
Hnimgg = imhist(imgg)./numel(imgg);
Hnimgb = imhist(imgb)./numel(imgb);
% Compute references histogram
Hnimgr2 = imhist(imgr2)./numel(imgr2);
Hnimgg2 = imhist(imgg2)./numel(imgg2);
Hnimgb2 = imhist(imgb2)./numel(imgb2);
% Histogram specification, using image reference
outr = histeq(imgr,Hnimgr2);
outg = histeq(imgg,Hnimgg2);
outb = histeq(imgb,Hnimgb2);
histsp(:,:,1) = outr;
histsp(:,:,2) = outg;
histsp(:,:,3) = outb;
%% Plot histogram & Display Image
%Show Image
figure;
subplot(221);imshow(ref);title(Reference Image);

subplot(222);imshow(img_src);title(Input Image);
subplot(224);imshow(histsp);title(Result Image);

% Plot for histogram specification


figure;
subplot(331);plot(Hnimgr);title(Red Input);
subplot(334);plot(Hnimgg);title(Green Input);
subplot(337);plot(Hnimgb);title(Blue Input);
subplot(332);plot(Hnimgr2);title(Red Ref);
subplot(335);plot(Hnimgg2);title(Green Ref);
subplot(338);plot(Hnimgb2);title(Blue Ref);
subplot(333);imhist(outr);title(Red Result);
subplot(336);imhist(outg);title(Green Result);
subplot(339);imhist(outb);title(Blue Result);

Output :

Experiment No. -7

Program Name:
WAP to perform linear filtering on a gray scale image in MATLAB

Theory Concept:
One can use linear filtering to remove certain types of noise. Certain filters, such as averaging
or Gaussian filters, are appropriate for this purpose. For example, an averaging filter is useful
for removing grain noise from a photograph. Because each pixel gets set to the average of the
pixels in its neighbourhood, local variations caused by grain are reduced.
Removing Noise by Median Filtering
Median filtering is similar to using an averaging filter, in that each output pixel is set to an
average of the pixel values in the neighborhood of the corresponding input pixel. However,
with median filtering, the value of an output pixel is determined by the median of the
neighborhood pixels, rather than the mean. The median is much less sensitive than the mean
to extreme values (called outliers). Median filtering is therefore better able to remove these
outliers without reducing the sharpness of the image. The medfilt2 function implements
median filtering.

Code:
Read in the image and display it.
I = imread('eight.tif');
imshow(I)
Add noise to it.
J = imnoise(I,'salt & pepper',0.02);
figure, imshow(J)
Filter the noisy image with an averaging filter and display the results.
K = filter2(fspecial('average',3),J)/255;
figure, imshow(K)
Now use a median filter to filter the noisy image and display the results. Notice
that medfilt2 does a better job of removing noise, with less blurring of edges.
L = medfilt2(J,[3 3]);
figure, imshow(L)

Output:

Experiment No.-8
Program Name:
WAP to perform negative of a gray scale image in MATLAB

Theory Concept:
Image Negatives
The negative of an image with the intensity levels in the range [0,L-1] is obtained by using
the negative transformation which is given by the expression
s=L-1-r
IM2 = imcomplement(IM) computes the complement of the image IM. IM can be a binary,
grayscale, or RGB image. IM2 has the same class and size as IM.
In the complement of a binary image, zeros become ones and ones become zeros; black and
white are reversed. In the complement of an intensity or RGB image, each pixel value is
subtracted from the maximum pixel value supported by the class (or 1.0 for double-precision
images) and the difference is used as the pixel value in the output image. In the output image,
dark areas become lighter and light areas become darker.

Code:
Create the complement of a uint8 array.
X = uint8([ 255 10 75; 44 225 100]);
X2 = imcomplement(X)
X2 =
0 245 180
211 30 155
Reverse black and white in a binary image.
bw = imread('text.png');
bw2 = imcomplement(bw);
subplot(1,2,1),imshow(bw)
subplot(1,2,2),imshow(bw2)
Create the complement of an intensity image.
I = imread('glass.png');
J = imcomplement(I);
imshow(I), figure, imshow(J)

Output:

EXPERIMENT NO.-9
Program Name:
WAP to implement color models in VLAB

Theory Concepts:
A color image is represented and stored as a set of three matrices each of size MXN. Each
matrix represents a colour plane. Thus if an RGB model is used, we have a red image, blue
image and a green image and thus 3 corresponding matrices. Other colour models are also
popular in practice.
The RGB and CMY colour models can be visualized as forming a colour cube .Here, red,
green and blue form the three orthogonal edges of the cube while cyan, magenta and yellow
form the opposite set of edges of the same cube. Note that the corner (S) where the RGB
edges meet corresponds to black colour while the corner (W) where the CMY edges meet
corresponds to the white colour. Any point within this cube can therefore be specified in
terms of 3 coordinates, namely RGB or CMY values. The diagonal line that connects the
black and white points will correspond to the grayscale.
Relationship between the RGB and CMY colour model is hence as follows.

Other colour models separate the colour (chromatic) and intensity (achromatic)
information. The HSI and YCbCr are two such models you will study.
HSI colour model: Here the chromatic information is represented in two components: hue
(H)and saturation (S), while the achromatic information is represented by the third intensity
component (I). The Hue component represents what we commonly understand to be colour. It
is represented as a point on a circle and hence is specified as an angle between [0,360]
degrees. 0 degree mean red, 120 means green 240 means blue. 60 degrees is yellow, 300
degrees is magenta.
The saturation component signals the spectral purity of the color, i.e. how much it is
diluted with white color. For example it helps differentiate between sky blue and navy blue.
The value of the saturation component is specified as a number in the interval [0,1].

Procedure:
The experiment is designed to understand and learn color models and processing in color
domains. This experiment consists two parts
(i)
(ii)

Color Spaces
Color Processing
Steps to run the experiments

(i) Color Spaces


1. Select image from the mosaic using 'select image' option
a) Select region of the image to load it in the input image panel
2. Select one option from color spaces
a) HIS
b) CMY
c) YCbCr
3. Select run option to perform the operations
a) Output result will be displayed in the output panel
(A) Processing:
1. Select image from the mosaic using 'select image' option
a. Select region of the image to load it in the input image panel
2. Select one option from color spaces
3. Select one or more plane from color space to apply parameters
4. Select the one option from Linear and Histogram processing
a. For Linear select the value of slope and offset
5. Select run option to perform the operations
a. Output result will be displayed in the output panel

Output:

EXPERIMENT NO.-10
Program Name:
WAP to perform morphological operations gray scale image in VLAB

Theory Concepts:
Erosion :
The value of the output pixel is the minimum value of all the pixels in the input pixel's
neighborhood. In a binary image, if any of the pixels is set to 0, the output pixel is set to 0.

Dilation:
The dilation process is performed by laying the structuring element B on the image A and
sliding it across the image in a manner similar to convolution. The difference is in the
operation performed. It is best described in a sequence of steps:
1. If the origin of the structuring element coincides with a 'white' pixel in the image, there is
no change; move to the next pixel.
2. If the origin of the structuring element coincides with a 'black' in the image, make black all
pixels from the image covered by the structuring element.

Opening on an image:
Opening consists of an erosion followed by a dilation and can be used to eliminate all pixels in
regions that are too small to contain the structuring element. In this case the structuring element is
often called a probe, because it is probing the image looking for small objects to filter out of the
image.
AB = (AB)B

Closing on an image:
The closing of A by B is obtained by the dilation of A by B, followed by erosion of the resulting
structure by B :
A .B = (A XOR B) XNOR B
The closing can also be obtained by,
A.B = (Ac . Bs)c

Procedure:
The experiment is designed to understand and learn the morphological operations in the
images.
Steps to run the experiments
1. Select image from the mosaic using 'select image' option
2. Select one option from 'Dilation','Erosion','Closing'and 'Opening'
3. Select options from 'Shape' and 'Size' of structuring elements properties
4.Select run option to perform the operations.
Interesting Observations
1. Select different size of structuring elements and observe the change in final output.

Output:

Original Image

Dilation

Closing

Opening

S-ar putea să vă placă și