+ All Categories
Home > Documents > MATLAB Lab Project_Mtech IIsem

MATLAB Lab Project_Mtech IIsem

Date post: 01-Dec-2014
Category:
Upload: satya-narayana
View: 128 times
Download: 4 times
Share this document with a friend
Popular Tags:
73
Generation of test signals Aim: To generate the test signals (a) Unit impulse signal (b) Unit step sequence (c) Ramp sequence (d) Exponential sequence (e) Sine sequence (f) Cosine sequence. Apparatus: MATLAB Version 7.8 (R2009a) Program: %program for the generation of unit impulse signal clc ; clear all ; close all ; t=-2:1:2 ; y=[zeros(1,2),ones(1,1),zeros(1,2)] ; subplot(3,2,1) ; stem(t,y) ; ylabel('amplitude ---->') ; xlabel('(a)n ---->') ; title(' unit impulse signal ') ; 1
Transcript
Page 1: MATLAB Lab Project_Mtech IIsem

Generation of test signals

Aim: To generate the test signals

(a) Unit impulse signal

(b) Unit step sequence

(c) Ramp sequence

(d) Exponential sequence

(e) Sine sequence

(f) Cosine sequence.

Apparatus: MATLAB Version 7.8 (R2009a)

Program:

%program for the generation of unit impulse signal

clc ;

clear all ;

close all ;

t=-2:1:2 ;

y=[zeros(1,2),ones(1,1),zeros(1,2)] ;

subplot(3,2,1) ;

stem(t,y) ;

ylabel('amplitude ---->') ;

xlabel('(a)n ---->') ;

title(' unit impulse signal ') ;

%program for the generation of unit step sequence [u(n)-u(n-N)]

n=input('enter the N value ') ;

t=0:1:n-1 ;

y=ones(1,n) ;

subplot(3,2,2) ;

1

Page 2: MATLAB Lab Project_Mtech IIsem

stem(t,y) ;

ylabel('amplitude ---->') ;

xlabel('(b)n ---->') ;

title(' unit step sequence ') ;

%program for the generation of ramp sequence

n=input('enter the length of ramp sequence ') ;

t=0:n-1 ;

subplot(3,2,3) ;

stem(t,t) ;

ylabel('amplitude ---->') ;

xlabel('(c)n ---->') ;

title(' ramp sequence ') ;

%program for the generation of exponential sequence

n=input('enter the length of exponential sequence') ;

t=0:n ;

a=input('enter the value of a ') ;

y=exp(a*t) ;

subplot(3,2,4) ;

stem(t,y) ;

ylabel('amplitude ---->') ;

xlabel('(d)n ---->') ;

title(' exponential sequence ') ;

%program for the generation of sine sequence

t=0:0.01:pi ;

y=sin(2*pi*t) ;

subplot(3,2,5) ;

plot(t,y) ;

ylabel('amplitude ---->') ;

2

Page 3: MATLAB Lab Project_Mtech IIsem

xlabel('(e)n ---->') ;

title(' sine sequence ') ;

%program for the generation of cosine sequence

t=0:0.01:pi ;

y=cos(2*pi*t) ;

subplot(3,2,6) ;

plot(t,y) ;

ylabel('amplitude ---->') ;

xlabel('(f)n ---->') ;

title(' cosine sequence ') ;

3

Page 4: MATLAB Lab Project_Mtech IIsem

Result:

Fig: test signals

Parameters:

Enter the value of N : 4

Enter the length of ramp sequence : 3

Enter the length of exponential sequence : 3

Enter the value of ‘a’ : 1

4

Page 5: MATLAB Lab Project_Mtech IIsem

FIR Filter Design

Aim: To design chebyshev type-I FIR filters

(a) Low pass filter

(b) High pass filter

(c) Band pass filter

(d) Band stop filter.

Apparatus: MATLAB Version 7.8 (R2009a)

Program :

%program for the design of FIR low pass, high pass, band pass and band stop filter using

%chebyshev window

clc;

close all;

clear all;

rp=input('enter the pass band ripple.......');

rs=input('enter the stop band ripple.......');

fs=input('enter the stop band freq.......');

fp=input('enter the pass band freq.......');

f=input('enter the sampling freq.......');

r=input('enter the ripple value in dB....');

wp=2*fp/f;

ws=2*fs/f;

num=-20*log10(sqrt(rp*rs))-13;

dem=14.6*(fs-fp)/f;

n=ceil(num/dem);

if(rem(n,2)==0)

n=n+1;

end

5

Page 6: MATLAB Lab Project_Mtech IIsem

y=chebwin(n,r);

%low pass filter

b=fir1(n-1,wp,y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,1);

plot(o/pi,m);

ylabel('gain in dB------>');

xlabel('(a) normalised frequency---->');

title(‘low pass filter’);

%high pass filter

b=fir1(n-1,wp,'high',y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,2);

plot(o/pi,m);

ylabel('gain in dB------>');

xlabel('(b) normalised frequency---->');

title(‘high pass filter’);

%band pass filter

wn=[wp ws];

b=fir1(n-1,wn,y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,3);

plot(o/pi,m);

ylabel('gain in dB------>');

xlabel('(c) normalised frequency---->');

6

Page 7: MATLAB Lab Project_Mtech IIsem

title(‘band pass filter’);

%band stop filter

b=fir1(n-1,wn,'stop',y);

[h,o]=freqz(b,1,256);

m=20*log10(abs(h));

subplot(2,2,4);

plot(o/pi,m);

ylabel('gain in dB------>');

xlabel('(d) normalised frequency---->');

title(‘stop band filter’);

7

Page 8: MATLAB Lab Project_Mtech IIsem

Result:

Fig: gain responses of low pass, high pass, band pass and band stop filters

Parameters:

Enter the pass band ripple....... : 0.03

Enter the stop band ripple....... : 0.02

Enter the stop band freq....... : 2400

Enter the pass band freq....... : 1800

Enter the sampling freq....... : 10000

Enter the ripple value in dB.... : 40

8

Page 9: MATLAB Lab Project_Mtech IIsem

IIR Filter Design

Aim: To design chebyshev type-I IIR filters

(a) Low pass filter.

(b) High pass filter.

(c) Band pass filter.

(d) Band stop filter.

Apparatus: MATLAB Version 7.8 (R2009a)

Program:

%program for the design of chebyshev type-I low pass digital filter

clc ;

close all ;

clear all ;

format long

rp=input('enter the passband ripple........') ;

rs=input('enter the stopband ripple........') ;

wp=input('enter the passband freq........') ;

ws=input('enter the stopband freq........') ;

fs=input('enter the sampling frequency........') ;

w1=2*wp/fs ;

w2=2*ws/fs ;

[n,wn]=cheb1ord(w1,w2,rp,rs) ;

[b,a]=cheby1(n,rp,wn) ;

w=0:0.01:pi ;

[h,om]=freqz(b,a,w) ;

m=20*log10(abs(h));

an=angle(h) ;

9

Page 10: MATLAB Lab Project_Mtech IIsem

subplot(2,1,1) ;

plot(om/pi,m) ;

ylabel('gain in dB -------->') ;

xlabel('(a) normalised frequency--------->') ;

subplot(2,1,2) ;

plot(om/pi,an) ;

xlabel('(b) normalised frequency ---------->') ;

ylabel('phase in radians ------------->') ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%program for the design of chebyshev type-I high pass filter

clc ;

close all ;

clear all ;

format long

rp=input('enter the passband ripple........') ;

rs=input('enter the stopband ripple........') ;

wp=input('enter the passband freq........') ;

ws=input('enter the stopband freq........') ;

fs=input('enter the sampling frequency........') ;

w1=2*wp/fs ;

w2=2*ws/fs ;

[n,wn]=cheb1ord(w1,w2,rp,rs) ;

[b,a]=cheby1(n,rp,wn,'high') ;

w=0:0.01/pi:pi ;

[h,om]=freqz(b,a,w) ;

m=20*log10(abs(h));

an=angle(h) ;

subplot(2,1,1) ;

plot(om/pi,m) ;

ylabel('gain in dB -------->') ;

10

Page 11: MATLAB Lab Project_Mtech IIsem

xlabel('(a) normalised frequency --------->') ;

subplot(2,1,2) ;

plot(om/pi,an) ;

xlabel('(b) normalised frequency ---------->') ;

ylabel('phase in radians ------------->') ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%program for the design of chebyshev type-I band pass digital filter

clc ;

close all ;

clear all ;

format long

rp=input('enter the passband ripple........') ;

rs=input('enter the stopband ripple........') ;

wp=input('enter the passband freq........') ;

ws=input('enter the stopband freq........') ;

fs=input('enter the sampling frequency........') ;

w1=2*wp/fs ;

w2=2*ws/fs ;

[n]=cheb1ord(w1,w2,rp,rs) ;

wn=[w1,w2] ;

[b,a]=cheby1(n,rp,wn,'bandpass') ;

w=0:0.01:pi ;

[h,om]=freqz(b,a,w) ;

m=20*log10(abs(h));

an=angle(h) ;

subplot(2,1,1) ;

plot(om/pi,m) ;

ylabel('gain in dB -------->') ;

xlabel('(a) normalised frequency --------->') ;

11

Page 12: MATLAB Lab Project_Mtech IIsem

subplot(2,1,2) ;

plot(om/pi,an) ;

xlabel('(b) normalised frequency ---------->') ;

ylabel('phase in radians ------------->') ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%program for the design of chebyshev type-I band stop digital filter

clc ;

close all ;

clear all ;

format long

rp=input('enter the passband ripple........') ;

rs=input('enter the stopband ripple........') ;

wp=input('enter the passband freq........') ;

ws=input('enter the stopband freq........') ;

fs=input('enter the sampling frequency........') ;

w1=2*wp/fs ;

w2=2*ws/fs ;

[n]=cheb1ord(w1,w2,rp,rs) ;

wn=[w1,w2] ;

[b,a]=cheby1(n,rp,wn,'stop') ;

w=0:0.1/pi:pi ;

[h,om]=freqz(b,a,w) ;

m=20*log10(abs(h));

an=angle(h) ;

subplot(2,1,1) ;

plot(om/pi,m) ;

ylabel('gain in dB -------->') ;

xlabel('(a) normalised frequency --------->') ;

subplot(2,1,2) ;

plot(om/pi,an) ;

xlabel('(b) normalised frequency ---------->') ;

12

Page 13: MATLAB Lab Project_Mtech IIsem

ylabel('phase in radians ------------->') ;

Result:

(a) Low pass filter

Fig: chebyshev type-I low pass filter

Parameters:

Enter the pass band ripple : 0.2

Enter the stop band ripple : 45

Enter the pass band frequency : 1300

Enter the stop band frequency : 1500

Enter the sampling frequency : 10000

13

Page 14: MATLAB Lab Project_Mtech IIsem

(b) High pass filter

Fig: chebyshev type-I high pass filter

Parameters:

Enter the pass band ripple : 0.3

Enter the stop band ripple : 60

Enter the pass band frequency : 1500

Enter the stop band frequency : 2000

Enter the sampling frequency : 9000

14

Page 15: MATLAB Lab Project_Mtech IIsem

(c) Band pass filter

Fig: chebyshev type-I band pass filter

Parameters:

Enter the pass band ripple : 0.4

Enter the stop band ripple : 35

Enter the pass band frequency : 2000

Enter the stop band frequency : 2500

Enter the sampling frequency : 10000

15

Page 16: MATLAB Lab Project_Mtech IIsem

(d) Band stop filter

Fig: chebyshev type-I band stop filter

Parameters:

Enter the pass band ripple : 0.25

Enter the stop band ripple : 40

Enter the pass band frequency : 2500

Enter the stop band frequency : 2750

Enter the sampling frequency : 7000

16

Page 17: MATLAB Lab Project_Mtech IIsem

Image Enhancement

Aim: To perform image enhancement operations

(a) Adding noise to an image

(b) Enhance contrast of an image using histogram equalization

(c) Adjusting image intensity values

(d) Remove noise in an image using 2D median filtering

Apparatus: MATLAB Version 7.8(R2009a).

Program :

%Adding noise to an image

close all;

clear all;

I=imread(‘cameraman.tif’);

J=imnoise(I,'salt & pepper',0.15);

figure, imshow(I)

figure, imshow(J)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% program to Enhance contrast of an image using histogram equalization

close all;

clear all;

I=imread('cameraman.tif');

J=histeq(I);

figure, imshow(I);

17

Page 18: MATLAB Lab Project_Mtech IIsem

figure, imshow(J);

%program to adjust the intensity values

close all;

clear all;

I=imread('cameraman.tif');

J=imadjust(I,[0.3 0.7]);

figure, imshow(I);

figure, imshow(J);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%program to perform 2D median filtering

close all;

clear all;

I=imread('cameraman.tif');

J=imnoise(I,'salt & pepper',0.15);

K=medfilt2(J);

figure, imshow(I);

figure, imshow(J);

figure, imshow(K);

18

Page 19: MATLAB Lab Project_Mtech IIsem

Result:

(a) Adding noise to an image

Fig: image before adding noise

19

Page 20: MATLAB Lab Project_Mtech IIsem

Fig: image after adding noise

(b) Enhance contrast of an image using histogram equalization

Fig: image before enhancing contrast

20

Page 21: MATLAB Lab Project_Mtech IIsem

Fig: image after enhancing contrast

(c) Adjusting image intensity values

Fig: image before adjusting intensity values

21

Page 22: MATLAB Lab Project_Mtech IIsem

Fig: image after adjusting intensity values.

(d) Remove noise in an image using 2D median filtering

Fig: image before without noise

22

Page 23: MATLAB Lab Project_Mtech IIsem

Fig: image after adding some noise

Fig: image after removing noise using 2D median filtering

23

Page 24: MATLAB Lab Project_Mtech IIsem

Inverse Z-Transforms

Aim: To find inverse z-transform of the following z-domain signals

(a) 1/(1-(1.5*z^(-1))+(0.5*z^(-2)))

(b) (z*sin(a))/(z^2 - 2*cos(a)*z + 1)

Apparatus: MATLAB Version 7.8 (R2009a).

Program:

%program to determine the inverse z-transforms

syms n z a

X1=1/(1-(1.5*z^(-1))+(0.5*z^(-2)));

disp('inverse z-transform of 1/(1-(1.5*z^(-1))+(0.5*z^(-2)));');

24

Page 25: MATLAB Lab Project_Mtech IIsem

x1=iztrans(X1);

simplify(x1)

X2=(z*sin(a))/(z^2 - 2*cos(a)*z + 1);

disp('inverse z-transform of (z*sin(a))/(z^2 - 2*cos(a)*z + 1');

x2=iztrans(X2);

simplify(x2)

Result:

inverse z-transform of 1/(1-(1.5*z^(-1))+(0.5*z^(-2))) ans =

2 - (1/2)^n inverse z-transform of (z*sin(a))/(z^2 - 2*cos(a)*z + 1) ans =

sin(a*n)

25

Page 26: MATLAB Lab Project_Mtech IIsem

Poles and Zeros of Z-domain signals

Aim: To find poles and zeros of Z-domain signal (z^2+ (0.8*z) +0.8)/ (z^2+0.49)

and sketch the pole-zero plot.

Apparatus: MATLAB version 7.8 (R2009a).

Program:

%program to determine poles &zeros of rational function of

%(z^2+(0.8*z)+0.8)/(z^2+0.49)

%to plot the poles and zeros in z-plane

clear all;

26

Page 27: MATLAB Lab Project_Mtech IIsem

syms z

num_coeff=[1 0.8 0.8];%find the factors of (z2+(0.8*z)+0.8)

disp('roots of numerator polynomial (z2+(0.8*z)+0.8) are zeros ');

zeros=roots(num_coeff)

den_coeff=[1 0 0.49];%find the factors of z2+0.49

disp('roots of denominator polynomial z2+0.49 are poles ');

poles=roots(den_coeff)

H=tf('z');

Ts=0.1;

H=tf([num_coeff],[den_coeff],Ts);

zgrid on;

pzmap(H);

Result:

27

Page 28: MATLAB Lab Project_Mtech IIsem

roots of numerator polynomial (z2+(0.8*z)+0.8) are zeros

zeros =

-0.4000 + 0.8000i

-0.4000 - 0.8000i

roots of denominator polynomial z2+0.49 are poles

poles =

0 + 0.7000i

0 - 0.7000i

DFT Properties

Aim: To verify convolution and circular convolution properties of DFT.

28

Page 29: MATLAB Lab Project_Mtech IIsem

Apparatus: MATLAB Version 7.8 (R2009a)

Program:

%program to perform Circular convolution

clear all;

close all;

a=input('enter the first sequence');

b=input('enter the second sequence');

y=0:1:length(a)-1;

subplot(3,1,1);

stem(y,a,'filled');

z=0:1:length(b)-1;

subplot(3,1,2);

stem(z,b,'filled');

c=cconv(a,b);

m= length(c)-1;

n=0:1:m;

disp('output sequence');

disp(c);

subplot(3,1,3);

stem(n,c);

xlabel('time index');

ylabel('amplitude');

% Program for convolution two sequence

syms t real

x1= exp(-2*t).*heaviside(t);

29

Page 30: MATLAB Lab Project_Mtech IIsem

x2= exp(-6*t).*heaviside(t);

disp('Fourier Transform of x1(t ) is');

x1= fourier(x1)

disp('Fourier Transform of x2(t ) is');

x2= fourier(x2)

y=x1*x2;

disp('let x3 be convolution of x1(t ) and x2(t)');

x3=ifourier(y,t)

Result:

30

Page 31: MATLAB Lab Project_Mtech IIsem

Circular convolution:

Fig: circular convolution of two given sequences

Parameters:

enter the first sequence : [1 2 4]

enter the second sequence : [1 2]

output sequence : 1 4 8 8

Convolution:

x1 =

1/(2+i*w)

Fourier Transform of x2(t ) is

x2 =

1/(6+i*w)

let x3 be convolution of x1(t ) and x2(t)

x3 =

1/4*heaviside(t)*(-exp(-6*t)+exp(-2*t))

Image Closing

31

Page 32: MATLAB Lab Project_Mtech IIsem

Aim: To close an image using morphological operations

Apparatus: MATLAB Version 7.8(R2009a)

Program:

% Step-1. Read the image into the MATLAB workspace and view it.

originalBW = imread('circles.png');

imshow(originalBW);

% Step-2. Create a disk-shaped structuring element. Use a disk structuring

% element to preserve the circular nature of the object.

% Specify a radius of 10 pixels so that the largest gap gets filled.

se = strel('disk',10);

% Step-3:Perform a morphological close operation on the image.

closeBW = imclose(originalBW,se);

figure, imshow(closeBW)

Result:

32

Page 33: MATLAB Lab Project_Mtech IIsem

Fig: image before closing

Fig: image after closing

33

Page 34: MATLAB Lab Project_Mtech IIsem

Image Dilation

Aim: To perform image dilation using morphological operations.

(a) Dilate a binary image with a vertical line structuring element.

(b) Dilate a grayscale image with a rolling ball structuring element.

Apparatus: MATLAB Version 7.8(R2009a)

Program:

% program to Dilate a binary image with a vertical line structuring element

bw = imread('text.png');

se = strel('line',11,90);

bw2 = imdilate(bw,se);

imshow(bw), title('Original')

figure, imshow(bw2), title('Dilated')

%Dilate a grayscale image with a rolling ball structuring element.

I = imread('cameraman.tif');

se = strel('ball',5,5);

I2 = imdilate(I,se);

imshow(I), title('Original')

figure, imshow(I2), title('Dilated')

34

Page 35: MATLAB Lab Project_Mtech IIsem

Result:

(a) Dilate a binary image with a vertical line structuring element.

Fig: image before dilation with a vertical line structuring element.

Fig: image after dilation image with a rolling ball structuring element.

35

Page 36: MATLAB Lab Project_Mtech IIsem

(b) Dilate a grayscale image with a rolling ball structuring element.

Fig: image before dilation with rolling ball structuring element.

Fig: image after dilation with rolling ball structuring element.

36

Page 37: MATLAB Lab Project_Mtech IIsem

Image Erosion

Aim: To perform image erosion using morphological operations

(a) Erode a binary image with a disk structuring element.

(b) Erode a grayscale image with a rolling ball.

Apparatus: MATLAB Version 7.8(R2009a)

Program:

%Erode a binary image with a disk structuring element.

originalBW = imread('circles.png');

se = strel('disk',11);

erodedBW = imerode(originalBW,se);

imshow(originalBW), figure, imshow(erodedBW)

% Erode a grayscale image with a rolling ball.

I = imread('cameraman.tif');

se = strel('ball',5,5);

I2 = imerode(I,se);

imshow(I), title('Original')

figure, imshow(I2), title('Eroded')

37

Page 38: MATLAB Lab Project_Mtech IIsem

Result:

(a) Erode a binary image with a disk structuring element.

Fig: image before erosion with disk structuring element.

38

Page 39: MATLAB Lab Project_Mtech IIsem

Fig: image after erosion with disk structuring element.

(b) Erode a grayscale image with a rolling ball.

Fig: image before erosion with a rolling ball.

Fig: image after erosion with a rolling ball.

39

Page 40: MATLAB Lab Project_Mtech IIsem

Image Opening

Aim: To Remove the smaller objects in an image.

Apparatus: MATLAB Version 7.8(R2009a)

Program:

% 1. Read the image into the MATLAB® workspace and display it.

I = imread('snowflakes.png');

imshow(I)

% 2. Create a disk-shaped structuring element with a radius of 5 pixels.

se = strel('disk',5);

% 3. Remove snowflakes having a radius less than 5 pixels by opening it

% with the disk-shaped structuring element created in step 2.

I_opened = imopen(I,se);

figure, imshow(I_opened,[])

40

Page 41: MATLAB Lab Project_Mtech IIsem

Result:

Fig: image before Remove the smaller objects in an image.

Fig: image after Remove the smaller objects in an image.

41

Page 42: MATLAB Lab Project_Mtech IIsem

Image Segmentation

Aim: To perform image segmentation operations on an image.

Apparatus: MATLAB Version 7.8(R2009a)

Program:

I = imread('cell.tif');

figure, imshow(I), title('original image');

[junk threshold] = edge(I, 'sobel');

fudgeFactor = .5;

BWs = edge(I,'sobel', threshold * fudgeFactor);

figure, imshow(BWs), title('binary gradient mask');

se90 = strel('line', 3, 90);

se0 = strel('line', 3, 0);

BWsdil = imdilate(BWs, [se90 se0]);

figure, imshow(BWsdil), title('dilated gradient mask');

BWdfill = imfill(BWsdil, 'holes');

figure, imshow(BWdfill);

title('binary image with filled holes');

BWnobord = imclearborder(BWdfill, 4);

figure, imshow(BWnobord), title('cleared border image');

seD = strel('diamond',1);

BWfinal = imerode(BWnobord,seD);

BWfinal = imerode(BWfinal,seD);

figure, imshow(BWfinal), title('segmented image');

BWoutline = bwperim(BWfinal);

42

Page 43: MATLAB Lab Project_Mtech IIsem

Segout = I;

Segout(BWoutline) = 255;

figure, imshow(Segout), title('outlined original image');

Result:

Fig: original image before segmentation

Fig: image of binary gradient mask

43

Page 44: MATLAB Lab Project_Mtech IIsem

Fig: image of dilated gradient mask

Fig: image of binary image with filled holes

Fig: image of cleared border image

44

Page 45: MATLAB Lab Project_Mtech IIsem

Fig: segmented image

Fig: outlined original image

45

Page 46: MATLAB Lab Project_Mtech IIsem

Spatial TransformationsAim: To perform

(a) Resizing an image

(b) Rotating an image

(c) Cropping an image using Spatial Transformations

Apparatus: MATLAB Version 7.8(R2009a)

Program:% Resizing an Image

% To resize an image, use the imresize function. When you resize an image,

% you specify the image to be resized and the magnification factor.

% To enlarge an image, specify a magnification factor greater than 1.

% To reduce an image, specify a magnification factor between 0 and 1.

% For example, the command below increases the size of an image by 1.25 times.

I = imread('circuit.tif');

J = imresize(I,1.25);

imshow(I)

figure, imshow(J)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%

46

Page 47: MATLAB Lab Project_Mtech IIsem

% To rotate an image, use the imrotate function. When you rotate an image,

% you specify the image to be rotated and the rotation angle, in degrees.

% If you specify a positive rotation angle, imrotate rotates the image

% counterclockwise; if you specify a negative rotation angle, imrotate

% rotates the image clockwise.

% This example rotates an image 35° counterclockwise and specifies bilinear

interpolation.

I = imread('circuit.tif');

J = imrotate(I,35,'bilinear');

imshow(I)

figure, imshow(J)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%

I = imread('circuit.tif');

J = imcrop(I);

I = imread('circuit.tif');

J = imcrop(I,[60 40 100 90]); % [xmin ymin width height].

47

Page 48: MATLAB Lab Project_Mtech IIsem

Result:

(a) Resizing an image

Fig: image before resizing

Fig: image after resizing

48

Page 49: MATLAB Lab Project_Mtech IIsem

(b) Rotating an image

Fig: image before rotating

Fig: image after rotating

49

Page 50: MATLAB Lab Project_Mtech IIsem

(c) Cropping an image

Fig: image of cropping

50

Page 51: MATLAB Lab Project_Mtech IIsem

Texture SegmentationAim: To perform texture segmentation operations on an image. Apparatus: MATLAB Version 7.8(R2009a)

Program:clc;

clear all;

close all;

%Step 1: Read Image

I = imread('bag.png');

figure, imshow(I);

%Step 2: Create Texture Image

%Use entropyfilt to create a texture image. The function

%entropyfilt returns an array where each output pixel

%contains the entropy value of the 9-by-9 neighborhood

%around the corresponding pixel in the input image I.

%Entropy is a statistical measure of randomness.

E = entropyfilt(I);

%Use mat2gray to rescale the texture image E so that its

%values are in the default range for a double image.

Eim = mat2gray(E);

figure, imshow(Eim);

%Step 3: Create Rough Mask for the Bottom Texture

%Threshold the rescaled image Eim to segment the textures.

%A threshold value of 0.8 is selected because it is roughly

%the intensity value of pixels along the boundary between the

%textures.

BW1 = im2bw(Eim, .8);

figure, imshow(BW1);

figure, imshow(I);

%The segmented objects in the binary image BW1 are white.

%If you compare BW1 to I, you notice the top texture is overly

51

Page 52: MATLAB Lab Project_Mtech IIsem

%segmented (multiple white objects) and the bottom texture is

%segmented almost in its entirety. You can extract the bottom

%texture using bwareaopen.

BWao = bwareaopen(BW1,2000);

figure, imshow(BWao);

%Use imclose to smooth the edges and to close any open holes

%in the object in BWao. A 9-by-9 neighborhood is selected because

%this neighborhood was also used by entropyfilt.

nhood = true(9);

closeBWao = imclose(BWao,nhood);

figure, imshow(closeBWao);

%Use imfill to fill holes in the object in closeBWao.

roughMask = imfill(closeBWao,'holes');

%Step 4: Use Rough Mask to Segment the Top Texture

%Compare the binary image roughMask to the original image I.

%Notice the mask for the bottom texture is not perfect because

%the mask does not extend to the bottom of the image. However,

%you can use roughMask to segment the top texture.

figure, imshow(roughMask);

figure, imshow(I);

%Get raw image of the top texture using roughMask.

I2 = I;

I2(roughMask) = 0;

figure, imshow(I2);

%Use entropyfilt to calculate the texture image.

E2 = entropyfilt(I2);

E2im = mat2gray(E2);

figure, imshow(E2im);

%Threshold E2im using graythresh.

BW2 = im2bw(E2im,graythresh(E2im));

figure, imshow(BW2)

52

Page 53: MATLAB Lab Project_Mtech IIsem

figure, imshow(I);

%If you compare BW2 to I, you notice there are two objects

%segmented in BW2. Use bwareaopen to get a mask for the top texture.

mask2 = bwareaopen(BW2,1000);

figure, imshow(mask2);

%Step 5: Display Segmentation Results

%Use mask2 to extract the top and bottom texture from I.

texture1 = I;

texture1(~mask2) = 0;

texture2 = I;

texture2(mask2) = 0;

figure, imshow(texture1);

figure, imshow(texture2);

%Outline the boundary between the two textures.

boundary = bwperim(mask2);

segmentResults = I;

segmentResults(boundary) = 255;

figure, imshow(segmentResults);

%Using Other Texture Filters in Segmentation

%Instead of entropyfilt, you can use stdfilt and rangefilt with

%other morphological functions to achieve similar segmentation results.

S = stdfilt(I,nhood);

figure, imshow(mat2gray(S));

R = rangefilt(I,ones(5));

figure, imshow(R);

53

Page 54: MATLAB Lab Project_Mtech IIsem

Result:

Fig: Reading texture image

Fig: Rescale the texture image using mat2gray

54

Page 55: MATLAB Lab Project_Mtech IIsem

Fig: rough mask for bottom texture

Fig: original image

55

Page 56: MATLAB Lab Project_Mtech IIsem

Fig: extraction of bottom texture using bwareaopen

Fig: Smoothening the edges

56

Page 57: MATLAB Lab Project_Mtech IIsem

Fig: segmenting the top texture using rough mask

Fig: original image

57

Page 58: MATLAB Lab Project_Mtech IIsem

Fig: Raw image using rough mask

Fig: Calculate the texture image using entropyfilt

58

Page 59: MATLAB Lab Project_Mtech IIsem

Fig: Threshold E2im using graythresh

Fig: original image

59

Page 60: MATLAB Lab Project_Mtech IIsem

Fig: Mask for the top texture using bwareopen

60

Page 61: MATLAB Lab Project_Mtech IIsem

Fig: Extraction of top and bottom texture from I using mask2

Fig: Outline the boundary between the two texture

61

Page 62: MATLAB Lab Project_Mtech IIsem

Fig: segmentation using stdfilt

Fig: segmentation using rangefilt

62


Recommended