Вы находитесь на странице: 1из 17

T.E (C.

E) SEM-VI Digital Image Processing

Sr. Page Sign of


Date Title of Experiment
No. No. Fculty

Program for addition Operation


1. 1-1

2. Program for bilinear rotation of an image. 2-2

3. Program for 2d Median Filtering. 3-6

Program for compressing an image using Huffman


4. 7-8
coding

5. 9-9
Program for dilation of image.

6. Program for Erosion operation. 10-10

Program for histogram of a digital Image.


7. 11-11

Program for complementing image.


8. 12-13

Program for detecting edges in an image


9. 14-16

INDEX

A.Y 2011-12 0 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 1
Aim: Program for addition Operation

Apparatus Required: Computer,Matlab Software

MATLAB Code:

clear,close all;
I=imread('testpat1.png');
figure,imshow(I);
J=imread('rice.png');
figure,imshow(J);
K=imadd(I,J);
figure,imshow(K);

a)Testpat1.png b) rice.png

Added Image

A.Y 2011-12 1 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 2
Aim: Program for bilinear rotation of an image

Apparatus Required: Computer, Matlab Software

MATLAB Code:

clear,close all;
I=imread('trees.tif');
J=imrotate(I,35,'bilinear');
imshow(I);
figure,imshow(J);

OUTPUT:

Trees.tif rotated image

A.Y 2011-12 2 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 3

Aim: Program for 2d Median Filtering for Salt and Pepper Noise without using
MEDFILT2 Function.

Apparatus Required: Computer, Matlab Software.

Theory:

MEDIAN FILTER:
In digital Image processing, removing the noise is one of the preprocessing techniques.

The image noise may be termed as random variation of brightness or color information.
There are various types of image noise.
The random occurrence of black and white pixels is salt and pepper noise.

Median filtering preserves the image without getting blurred. Median filtering is done on an
image matrix by finding the median of the neighborhood pixels by using a window that
slides pixel by pixel.

A.Y 2011-12 3 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

The procedural steps for 2D median filtering

A.Y 2011-12 4 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

MATLAB Code:

%READ AN 2D IMAGE
A=imread('zebra.jpg');
title('IMAGE WITH SALT AND PEPPER NOISE');
figure,imshow(A);

%PAD THE MATRIX WITH ZEROS ON ALL SIDES


modifyA=zeros(size(A)+2);
B=zeros(size(A));

%COPY THE ORIGINAL IMAGE MATRIX TO THE PADDED MATRIX


for x=1:size(A,1)
for y=1:size(A,2)
modifyA(x+1,y+1)=A(x,y);
end
end

%LET THE WINDOW BE AN ARRAY


%STORE THE 3-by-3 NEIGHBOUR VALUES IN THE ARRAY
%SORT AND FIND THE MIDDLE ELEMENT

for i= 1:size(modifyA,1)-2
for j=1:size(modifyA,2)-2
window=zeros(9);
inc=1;
for x=1:3
for y=1:3
window(inc)=modifyA(i+x-1,j+y-1);
inc=inc+1;
end
end

med=sort(window);
%PLACE THE MEDIAN ELEMENT IN THE OUTPUT MATRIX
B(i,j)=med(5);

end
end
%CONVERT THE OUTPUT MATRIX TO 0-255 RANGE IMAGE TYPE
B=uint8(B);
title('IMAGE AFTER MEDIAN FILTERING');
figure,imshow(B);

A.Y 2011-12 5 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

A.Y 2011-12 6 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 4

Aim: Program for compressing an image using Huffman coding

Apparatus Required: Computer, Matlab Software

MATLAB Code:

function CODE = huffman(p)


%HUFFMAN Builds a variable-lenght Huffman code for a symbol source.
%CODE = HUFFMAN(P) returns a huffman code as binary strings in cell
%array CODE for input symbol probability vector P. Each word in CODE
%corresponds to a symbol whose probability is at the corresponding index
% of P.
%
%check the input arguments for reasonableness.
error(nargchk(1,1,nargin));
if (ndims(p) ~= 2) | (min(size(p))>1) | ~isreal(p) | ~isnumeric(p)
error(P must be a real numeric vector.);
end
% Global variable surviving all recursions of function makecode
global CODE
CODE = cell(length(p),1); % Init the global cell array
if length(p) > 1 % When more than one symbol
p = p/sum(p); % Normalize the input probabilities
s = reduce(p); % Do Huffman source symbol reductions
makecode(s, []); % Recursively generate the code
else
CODE = {1}; % Else, trivial one symbol case!
end
%.%
function s = reduce(p);
%Create a Huffman source reduction tree in a MATLAB cell structure by
%performing source symbol reductions until there are only two reduced
%symbols remaining.
s = cell(length(p),1);
%Generate a starting tree with symbol nodes 1,2,3,.. to reference the
%symbol probabilities.
for i = 1:length(p)
s{i} = i;
end
while numel(s) > 2
[p,i] = sort(p); % Sort the symbol probabilities
p(2) = p(1) + p(2); % Merge the 2 lowest probabilities
p(1) = []; % and prune the lowest one
s = s(i); % Reorder tree for new prbabilities.
s{2} = {s{1},s{2}}; % and merge & prune its nodes

A.Y 2011-12 7 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

s(1) = []; % to match the probabilities


end

%.%
function makecode(sc, codeword)
% Scan the nodes of a Huffman source reduction tree recursively to
% generate the indicated variable length code words.
% Global variable surviving all the recursive calls
global CODE
if isa(sc,cell) % For cell array nodes,
makecode(sc{1},[codeword 0]); % add 0 if the 1st element,
makecode(sc{2}, [codeword 1]); % or a 1 if the 2nd
else % For leaf (numeric) nodes,
CODE{sc} = char(0 + codeword); % create a char code string
end

Result:

f = [0.1875 0.5 0.125 0.1875];


c = huffman(f)

A.Y 2011-12 8 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 5
Aim: Program for Dilation Operation

Apparatus Required: Computer, Matlab Software

MATLAB Code:

I = IMREAD('CAMERAMAN.TIF');
SE = STREL('BALL',5,5);
I2 = IMDILATE(I,SE);
IMSHOW(I), TITLE('ORIGINAL')
FIGURE, IMSHOW(I2), TITLE('DILATED')

OUTPUT

CAMERAMAN.TIF DILATED IMAGE

A.Y 2011-12 9 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 6
Aim: Program for Erosion operation.

Apparatus Required: Computer, Matlab Software

MATLAB Code:

I = imread('cameraman.tif');
se = strel('ball',5,5);
I2 = imerode(I,se);
imshow(I), title('Original');
figure, imshow(I2), title('Eroded');

OUTPUT

Cameraman.tif eroded image

A.Y 2011-12 10 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 7

Aim: Program for histogram of a digital Image.

Apparatus Required: Computer, Matlab Software

MATLAB Code:

clear;

close all;

I= imread('moon.tif');

imshow(I);

figure, imhist(I);

OUTPUT:

A.Y 2011-12 11 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 8

Aim: Program for complementing image.

Apparatus Required: Computer, Matlab Software

MATLAB Code:

clear;

close all;

I = imread('coins.PNG');

k= imcomplement(I);

imview(I);

imview(k);

A.Y 2011-12 12 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

OUTPUT:

image 1 image2

image 3 image4

A.Y 2011-12 13 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

Experiment No: 9
Aim: Program for detecting edges in an image

Apparatus Required: Computer, Matlab Software

Theory:

There are many operators in MATLAB for finding images


1. Sobers Operator
2. Roberts Operator
3. Prewitts Operator
4. Laplacian of Gaussian Method
5. Zero-Cross Method, etc.

To use them in MATLAB there is a function called as edge(I).

Syntax :

BW = edge(I)

BW = edge(I,'sobel')

BW = edge(I,'prewitt')

BW = edge(I,'roberts') etc.

Description :
BW = edge(I) takes a grayscale or a binary image I as its input, and returns a binary image
BW of the same size as I, with 1's where the function finds edges in I and 0's elsewhere.

By default, edge uses the Sobel method to detect edges but the following provides a complete
list of all the edge-finding methods supported by this function:
The Sobel method finds edges using the Sobel approximation to the derivative. It
returns edges at those points where the gradient of I is maximum.

A.Y 2011-12 14 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

The Prewitt method finds edges using the Prewitt approximation to the derivative. It
returns edges at those points where the gradient of I is maximum.

The Roberts method finds edges using the Roberts approximation to the derivative. It
returns edges at those points where the gradient of I is maximum.

The Laplacian of Gaussian method finds edges by looking for zero crossings after
filtering I with a Laplacian of Gaussian filter.

The zero-cross method finds edges by looking for zero crossings after filtering I with
a filter you specify.

The Canny method finds edges by looking for local maxima of the gradient of I. The
gradient is calculated using the derivative of a Gaussian filter. The method uses two
thresholds, to detect strong and weak edges, and includes the weak edges in the output
only if they are connected to strong edges. This method is therefore less likely than
the others to be fooled by noise, and more likely to detect true weak edges.

The parameters you can supply differ depending on the method you specify. If you do not
specify a method, edge uses the Sobel method.
MATLAB Code:

i = imread('far.jpg');
I = rgb2gray(i);
BW1 = edge(I,'prewitt');
BW2= edge(I,'sobel');
BW3= edge(I,'roberts');
subplot (2,2,1);
imshow(I);
title('original');
subplot(2,2,2);
imshow(BW1);
title('Prewitt');
subplot(2,2,3);
imshow(BW2);
title('Sobel');
subplot(2,2,4);

A.Y 2011-12 15 K.T.P.C.O.E& T, OSMANABAD


T.E (C.E) SEM-VI Digital Image Processing

imshow(BW3);
title('Roberts');

Result:
The edge detection of the image is done.

A.Y 2011-12 16 K.T.P.C.O.E& T, OSMANABAD