ECS-752(Lab Manual) Updated

30
AJAY KUMAR GARG ENGINEERING COLLEGE, GHAZIABAD DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING LAB MANUAL COURSE : B.Tech (CSE) SEMESTER : VII SUBJECT : Digital Image Processing LAB SUBJECT CODE : ECS-752 PREPARED BY: Prof. B. M. Kalra HoD CSE Faculty Name: Neeti Chadha Date: Signature:

description

digital image processing lab manual

Transcript of ECS-752(Lab Manual) Updated

Page 1: ECS-752(Lab Manual) Updated

AJAY KUMAR GARG ENGINEERING COLLEGE,GHAZIABAD

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LAB MANUAL

COURSE : B.Tech (CSE)SEMESTER : VIISUBJECT : Digital Image Processing LABSUBJECT CODE : ECS-752

PREPARED BY: Prof. B. M. KalraHoD CSE

Faculty Name: Neeti ChadhaDate: Signature:

Faculty Name: Akhilesh VermaDate: Signature:

Page 2: ECS-752(Lab Manual) Updated

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

AJAY KUMAR GARG ENGINEERING COLLEGE, GHAZIABAD

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

LIST OF EXPERIMENTS

COURSE: B.Tech SEMESTER: VII SESSION: 2012-13BRANCH: CSE SUBJECT CODE & NAME: ECS-752, Digital Image Processing Lab

SI. No.

NAME OF EXPERIMENT

1 WAP to implement conditional control sequences in MATLAB2 WAP to implement loop constructs in MATLAB.3 WAP to implement matrix operations in MATLAB4 WAP to read, write and display images in MATLAB5 WAP to perform histogram equalization in MATLAB6 WAP to perform histogram specification in MATLAB7 WAP to perform linear filtering on a gray scale image in MATLAB8 WAP to perform negative of a gray scale image in MATLAB9 WAP to implement color models in VLAB10 WAP to perform morphological operations gray scale image in VLAB

Neeti Chadha Prof.B.M.Kalra

Akhilesh Verma HoD CSE

Page 3: ECS-752(Lab Manual) Updated

Experiment- 1

Program Name:WAP to implement conditional control sequences in MATLAB

TheoryConcepts:

IF STATEMENTOften, 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!!!

>>

Page 4: ECS-752(Lab Manual) Updated

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_statementsEND LOOP;

The range is evaluated when the FOR loop is first entered and is never re-evaluated.

Page 5: ECS-752(Lab Manual) Updated

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

>>

Page 6: ECS-752(Lab Manual) Updated

Output:

iteration 1

iteration 2

iteration 3

iteration 4

>>

Page 7: ECS-752(Lab Manual) Updated

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)

Page 8: ECS-752(Lab Manual) Updated

Output:

A =

1 2 0

2 5 -1

4 10 -1

B =

1 2 4

2 5 10

0 -1 -1

C =

5 12 24

12 30 59

24 59 117

C =

1 4 0

4 25 -10

0 -10 1

X =

5 2 -2

-2 -1 1

0 -2 1

I =

1 0 0

0 1 0

0 0 1

ans =

12.3171

0.5149

0.1577

Page 9: ECS-752(Lab Manual) Updated

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’);

Page 10: ECS-752(Lab Manual) Updated

Output:

Page 11: ECS-752(Lab Manual) Updated

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

endsum=0;no_bins=255;

Page 12: ECS-752(Lab Manual) Updated

%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);

endfor i=1:size(GIm,1)

 for j=1:size(GIm,2)  HIm(i,j)=output(GIm(i,j)+1); end

endfigure,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)};   endcolumnname =   {'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 :

Page 13: ECS-752(Lab Manual) Updated
Page 14: ECS-752(Lab Manual) Updated

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 dataclc, clear all, close all;% Load input & reference imageimg_src=imread(input_image);ref=imread(reference_image);% Separate input image’s color channelimgr = img_src(:,:,1);imgg = img_src(:,:,2);imgb = img_src(:,:,3);%Separate reference image’s color channelimgr2 = ref(:,:,1);imgg2 = ref(:,:,2);imgb2 = ref(:,:,3);%% PreProcessing% Compute input’s histogramHnimgr = imhist(imgr)./numel(imgr);Hnimgg = imhist(imgg)./numel(imgg);Hnimgb = imhist(imgb)./numel(imgb);% Compute reference’s histogramHnimgr2 = imhist(imgr2)./numel(imgr2);Hnimgg2 = imhist(imgg2)./numel(imgg2);Hnimgb2 = imhist(imgb2)./numel(imgb2);% Histogram specification, using image referenceoutr = 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 Imagefigure;subplot(221);imshow(ref);title(‘Reference Image’);

Page 15: ECS-752(Lab Manual) Updated

subplot(222);imshow(img_src);title(‘Input Image’);subplot(224);imshow(histsp);title(‘Result Image’);% Plot for histogram specificationfigure;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 :

Page 16: ECS-752(Lab Manual) Updated

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:

Page 17: ECS-752(Lab Manual) Updated
Page 18: ECS-752(Lab Manual) Updated

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)

Page 19: ECS-752(Lab Manual) Updated

Output:

Page 20: ECS-752(Lab Manual) Updated

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].

Page 21: ECS-752(Lab Manual) Updated

Procedure:

The experiment is designed to understand and learn color models and processing in color domains. This experiment consists two parts

(i) Color Spaces(ii) 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

Page 22: ECS-752(Lab Manual) Updated

Output:

Page 23: ECS-752(Lab Manual) Updated

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.

A◦B = (AΘB)⊕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 

Page 24: ECS-752(Lab Manual) Updated

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.

Page 25: ECS-752(Lab Manual) Updated

Output:

Original Image Dilation

Closing Opening