0% found this document useful (0 votes)
74 views22 pages

Index: Name:-Fadadu Sharad Roll No.: - P10Ec949

The document describes the experiments to be performed in the DIP lab. It contains 20 experiments related to digital image processing topics like color space conversion, filtering, noise removal, histogram processing, frequency domain operations etc. For each experiment, it lists the aim, program code and expected output. It also contains details like student name, roll number and signature column for each experiment.

Uploaded by

SHARAD FADADU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views22 pages

Index: Name:-Fadadu Sharad Roll No.: - P10Ec949

The document describes the experiments to be performed in the DIP lab. It contains 20 experiments related to digital image processing topics like color space conversion, filtering, noise removal, histogram processing, frequency domain operations etc. For each experiment, it lists the aim, program code and expected output. It also contains details like student name, roll number and signature column for each experiment.

Uploaded by

SHARAD FADADU
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

DIP Lab S.V.N.I.T.

Name:- Fadadu Sharad Roll No.:- P10EC949


INDEX

No. Experiment Description Page Signature


1. Read the image and perform --RGB to Gray image --RGB to
Indexed image and --Gray to Indexed image
2. Read an Image and perform --Observe Brightness modification
and Contrast manipulation --Perform Thresholding on given
Image --Find the –ve Image of given Image --Apply Log
transformation to the given Image
3. Gray Level Slicing With and Without back ground.
4. Perform Histogram Equalization.
5. Read an Image and perform Bit Plane Slicing on Image.
6. Read an image and perform Average and Weightage lowpass
filter in spatial domain for 3x3 and 5x5 masks.
7. Read an Image, corrupt it by Salt-Pepper noise, Gaussian noise
and Speckle noise. Apply 3x3 & 5x5 box filter and comment
on the result.
8. Read an Image, corrupt it by Salt-Pepper noise. Apply 3x3 &
5x5 box filter and Median Filter. Comment on obtained result.
9. Read an Image and do Unsharp Masking.
10. Analyze 3x3 Avg filter in freq domain as a lowpass filter.
11. Prove that Convolution in Spatial domain is equal to
Multiplication in Frequency domain by taking two different
binary Images.
12. For the given two images find Magnitude and Phase of each
Image. Interchange the Phase and Magnitude with each other
and reconstruct the images. Comment on the result.
13. Generate an Image having a Square on centre of dimension
50x50 and Rotate this Image at 45 degree. Find Spectrum of
Original and Rotated Images.
14. Get an Image consisting of white square with black back
ground. Rotate image 30 & 45 degree and observe --Imrotate
using nearest option and --Using bilinear option.
15. Observe Low pass filter in frequency domain.
16. Observe High pass filter in frequency domain.
17. Observe Band reject filter in frequency domain as periodic
noise remover.
18. Write a Matlab code to blending two images in user defined
manner.
19. Write a Matlab code to perform Erosion, Dilation, Opening
and Closing.
20. Write a Matlab code to generate different Walsh and Harr basis
Image.

PRACTICAL:-01

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read the image and perform


--RGB to Gray image
--RGB to Indexed image
--Gray to Indexed image

Program:-
clc; clear all; close all;
I=imread('peppers.png');
J=rgb2gray(I);
subplot 221; imshow(I);
title('Original RGB Image');
subplot 222; imshow(J);
title('Gray Image');

[K,map]=rgb2ind(I,128);
M=ind2rgb(K,map);
subplot 223; imshow(M);
title('Indexed RGB Image 128 Color Levels');

[L,map]=gray2ind(J,128);
N=ind2gray(L,map);
subplot 224; imshow(N);
title('Indexed Gray Image 128 Gray Levels');

PRACTICAL:-02

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an Image and perform


--Observe Brightness modification and Contrast manipulation
--Perform Thresholding on given Image
--Find the –ve Image of given Image
--Apply Log transformation to the given Image

Program :-

clc; clear all; close all;

f=imread('circuit.tif'); %Read an image in f(2D matrix)


subplot 231; imshow(f);
title('Original Image');

B=50; g=f+B; %increase the intensity by values of B


subplot 232; imshow(g);
title('After Increase in Brigtness');

c=1.5; h=f.*c; %multiplication by c


subplot 233; imshow(h);
title('After Improving Contrast');

[m n]=size(f); Th=90; %Threshold value


for a=1:m,
for b=1:n,
if (f(a,b)<=Th),
k(a,b)=f(a,b);
%if intensity level is less Th store pixel
else
k(a,b)=255; %White back ground
end;
end;
end;
subplot 234; imshow(k);
title('After Thesholing');

max1=max(max(f)); %Find the max value of original image


l=max1-f; %Subtract image from max value
subplot 235; imshow(l);
title('Negative Image');

f=double(f);
s=10*(1+log(f)); s=uint8(s);
subplot 236; imshow(s);
title('After Logarithmic');

PRACTICAL:-03

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Gray Level Slicing With and Without back ground.

Program:-

clc; clear all;


h=imread('tire.tif'); %read an image
[m,n]=size(h); %Dimensions of the image
for a=1:m,
for b=1:n,
if(h(a,b)>0 && h(a,b)<30) % if the intensity level between
% 10 to 150
H1(a,b)=h(a,b).*1.5+30; % contrast straching
else
H1(a,b)=255;
end;
end;
end;
for a=1:m,
for b=1:n,
if(h(a,b)>0 && h(a,b)<30)
H2(a,b)=h(a,b).*1.5+30;
else
H2(a,b)=h(a,b);
end;

end;
end;
subplot 131; imshow(h);
title('Original Image');
subplot 132; imshow(H1);
title('With White back ground');
subplot 133; imshow(H2);
title('With No back ground');

Output:-

Original Image With White back ground With No back ground

PRACTICAL:-04

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Perform Histogram Equalization.

Program:-

clc; clear all;


h=imread ('circuit.tif');
subplot 221; imshow(h);
title('Original Image');
[m,n]=size(h);
for a=1:256,
hist1(a)=0; % clear array
end;

for a=1:m,
for b=1:n,
hist1(h(a,b)+1)=hist1(h(a,b)+1)+1; % Occurance of gray value
end;
end;

a=0:255;
subplot 222; stem(a,hist1);
set(gca,'Xlim',[0 255]);
title('Histogram of Original');

prob=hist1./(m*n); % probability of Gray Values


cdf(1)=prob(1);
for a=2:length(prob),
cdf(a)=cdf(a-1)+prob(a); %Cumulative Distribution Function
end;
s=uint8(round(255.*cdf)); % Mul. by (L-1)

for a=1:256,
hist2(a)=0;
end;
for a=1:m,
for b=1:n,
g(a,b)=s(h(a,b)+1); % New Gray Value Mapping
hist2(g(a,b)+1)=hist2(g(a,b)+1)+1; % Occurance of Gray
end;
end;

subplot 223; imshow(g);


title('Histeqalized Image');

a=0:255;
subplot 224; stem(a,hist2);
set(gca,'Xlim',[0 255]);
title('Histogram of Modified');

PRACTICAL:-05

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an Image and perform Bit Plane Slicing on Image.

Program:-

clc; clear all;

f=imread('cameraman.tif');
subplot 331; imshow(f);
title('Original Image');

[m,n]=size(f);

for c=1:8,
for a=1:m,
for b=1:n,
h1(c,a,b)=bitand(uint8(f(a,b)),uint8(2^(c-1)));
% Mask other bits
end;
end;
end;

for c=1:8,
g(:,:)=h1(c,:,:); % Create Image For each Plane
subplot (3,3,c+1); imshow(g);
title(['Bit-Sliced (Plane :- ',num2str(c),')']);
end;

g1(:,:)=h1(5,:,:)+h1(6,:,:)+h1(7,:,:)+h1(8,:,:);

figure,
subplot 121; imshow(f);
title('Original Image');
subplot 122; imshow(g1);
title('Summation of plane 5,6,7 and 8');

PRACTICAL:-06

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an image and perform Average and Weightage lowpass filter in spatial domain
for 3x3 and 5x5 mask.

Program:-

clc; clear all;

f=imread('eight.tif');
f=f(20+[1:180],50+[1:180]); %crop an Image
subplot 231; imshow(f);
title('Original Image');

a=1/9.*ones(3); % Avg 3X3 Mask


h=conv2(double(f),double(a),'same');
h=uint8(h); % Double to Integer After 2-D Convolution
subplot 232; imshow(h);
title('After Filtering with Mask 3X3');

a=1/25.*ones(5); % Avg 5X5 Mask


h=conv2(double(f),double(a),'same');
h=uint8(h);
subplot 233; imshow(h);
title('After Filtering with Mask 5X5');

a=1/16.*[1,2,1; 2,4,2; 1,2,1]; % Weightage 3X3 Mask


h=conv2(double(f),double(a),'same');
h=uint8(h);
subplot 234; imshow(h);
title('After Filtering with Weightage 3X3');

a=1/256.*[1,4,6,4,1; 4,16,24,16,4; 6,24,36,24,6; 4,16,24,16,4;


1,4,6,4,1]; % Weightage 5X5 Mask
h=conv2(double(f),double(a),'same');
h=uint8(h);
subplot 235; imshow(h);
title('After Filtering with Weightage 5X5');

PRACTICAL:-07

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an Image, corrupt it by Salt-Pepper noise, Gaussian noise and Speckle noise.
Apply 3x3 & 5x5 box filter and comment on the result.

Program:-

clc; clear all;

f=imread('rice.png');
subplot 221; imshow(f);
title('Original Image');

g=imnoise(f,'salt & pepper',0.02);


g=imnoise(g,'gaussian',0,0.01);
g=imnoise(g,'speckle',0.01);

subplot 222; imshow(g);


title('Image with Salt&Pepper, Gaussian and Speckle Noise');

a=1/(3*3).*ones(3);
g1=conv2(double(g),double(a),'same');
g1=uint8(g1);

subplot 223; imshow(g1);


title('After 3x3 box filter');

a=1/(5*5).*ones(5);
g2=conv2(double(g),double(a),'same');
g2=uint8(g2);

subplot 224; imshow(g2);


title('After 5x5 box filter');

PRACTICAL:-08

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an Image, corrupt it by Salt-Pepper noise. Apply 3x3 & 5x5 box filter and
Median Filter. Comment on obtained result.

Program:-

clc; clear all;

f=imread('coins.png');
f=f(20+[1:180],20+[1:256]); %crop an Image
subplot 321; imshow(f);
title('Original Image');

g=imnoise(f,'salt & pepper',0.04);


subplot 322; imshow(g);
title('Noisy Image');

a=1/(3*3).*ones(3);
g1=conv2(double(g),double(a),'same');
g1=uint8(g1);
subplot 323; imshow(g1);
title('Result of 3x3 box filter');

a=1/(5*5).*ones(5);
g1=conv2(double(g),double(a),'same');
g1=uint8(g1);
subplot 324; imshow(g1);
title('Result of 5x5 box filter');

g2=medfilt2(g,[3 3]);
subplot 325; imshow(g2);
title('Result of 3x3 Median filter');

g2=medfilt2(g,[5 5]);
subplot 326; imshow(g2);
title('Result of 5x5 Median filter');

PRACTICAL:-09

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Read an Image and do Unsharp Masking.

Program:-

clc; clear all;

f=imread('moon.tif');
[m n]=size(f);
f=f(0+[1:m],30+[1:n-80]);

subplot 131; imshow(f);


title('Original Image');

a=[1 1 1; 1 -8 1; 1 1 1]; % Laplacian Operator


g=conv2(double(f),a,'same');
g=uint8(g);

subplot 132; imshow(g);


title('Laplacian of Image');

g1=f-0.5.*g; %subtract laplacian from image with scale 0.5

subplot 133; imshow(g1);


title('Unsharp Image');

PRACTICAL:-10

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Analyze 3x3 Average filter in frequency domain as a lowpass filter.

Program:-

clc; clear all;

num=1;

for lop=1:2,
m=50; n=50;
ord=2*lop+1;
Mask=1/(ord*ord).*ones(ord); % Generate Mask

Y=fft2(Mask,m,n); % Fourier Transform


a=0:m-1; b=0:n-1;
subplot (2,2,num);
surf(a*2*pi/m,b*2*pi/n,abs(Y)); % Made GUI
set(gca,'xlim',[0 2*pi],'xtick',(0:pi/2:2*pi),
'xticklabel',{'0','pi/2','pi','3pi/2','2pi'});
set(gca,'ylim',[0 2*pi],'ytick',(0:pi/2:2*pi),
'yticklabel',{'0','pi/2','pi','3pi/2','2pi'});
xlabel('\omega_x (rad)');
ylabel('\omega_y (rad)');
zlabel('Magnitude');
title(['Freq Responce of Mask:- ',num2str(ord)','X',
num2str(ord),' without fftshift']);
Y1=fftshift(fft2(Mask,m,n)); % Fftshift of fft
a=0:m-1; b=0:n-1;

subplot (2,2,num+1);
surf((a*2*pi/m)-pi,(b*2*pi/n)-pi,abs(Y1));
set(gca,'xlim',[-pi pi],'xtick',(-pi:pi/2:pi),
'xticklabel',{'-pi','-pi/2','0','pi/2','pi'});
set(gca,'ylim',[-pi pi],'ytick',(-pi:pi/2:pi),
'yticklabel',{'-pi','-pi/2','0','pi/2','pi'});
xlabel('\omega_x (rad)');
ylabel('\omega_y (rad)');
zlabel('Magnitude');
title(['Freq Responce of Mask:- ',num2str(ord)','X',
num2str(ord),' with fftshift']);
num=num+2;;
end;

PRACTICAL:-11
P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Prove that Convolution in Spatial domain is equal to Multiplication in Frequency


domain by taking two different binary Images.

Program:-

clc; clear all;

f=255.*[ones(40,10) zeros(40,10) ones(40,10) zeros(40,10)];


g=255.*[ones(10,40); zeros(10,40); ones(10,40); zeros(10,40)];

subplot 221; imshow(f);


title('Image-1');
subplot 222; imshow(g);
title('Image-2')
[m1,n1]=size(f);
[m2,n2]=size(g);

h=conv2(f,g);
h=uint8(h);
subplot 223; imshow(h);
title('Spatial Convolution');

m=m1+m2-1; n=n1+n2-1;
Y1=fft2(f,m,n); % Fourier of 1
Y2=fft2(g,m,n); % Fourier of 2

Y=(Y1.*Y2); % Multiplication of ffts


s=abs(uint8(ifft2(Y,m,n))); % Inverse Fourier to get Image
subplot 224; imshow(s);
title('Freq Mutiplication');

PRACTICAL:-12

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- For the given two images find Magnitude and Phase of each Image. Interchange the
Phase and Magnitude with each other and reconstruct the images. Comment on the
result.

Program:-

clc; clear all; close all;

f=imread('cameraman.tif');
g=imread('rice.png');

[m n]=size(f);

Y1=fftshift(fft2(f,m,n)); % Fourier of Image-1


Y2=fftshift(fft2(g,m,n)); % Fourier of Image-2

Z1=abs(Y1); % Magnitude of Im-1


Z2=angle(Y2); % Phase of Im-2
Z3=Z1.*cos(Z2)+j.*Z1.*sin(Z2); % Made Complex Number
h12=uint8(ifft2(fftshift(Z3))); % Ifft2 to get New Image

Z1=abs(Y2); % Magnitude of Im-2


Z2=angle(Y1); % Phase of Im-1
Z3=Z1.*cos(Z2)+j.*Z1.*sin(Z2); % Made Complex Number
h21=uint8(ifft2(fftshift(Z3))); % Ifft2 to get New Image

subplot 221; imshow(f);


title('Original Image1');
subplot 222; imshow(g);
title('Original Image2');
subplot 223; imshow(h12);
title('Image After Mag of Im-1 Phase of Im-2');
subplot 224; imshow(h21);
title('Image After Mag of Im-2 Phase of Im-1');

PRACTICAL:-13

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Generate an Image having a Square on centre of dimension 50x50 and Rotate this
Image at 45 degree. Find Spectrum of Original and Rotated Images.

Program:-

clc; clear all; close all;

f=[ zeros(50),zeros(50),zeros(50);
zeros(50), ones(50),zeros(50);
zeros(50),zeros(50),zeros(50)];

subplot 221; imshow(f);


title('Original Image');

g=imrotate(f,45,'crop'); % Rotate Image by 45 degree


subplot 222; imshow(g);
title('Rotated Image');

Y1=fftshift(fft2(f,50,50));
Y2=fftshift(fft2(g,50,50));

subplot 223; imshow(uint8(60.*log(abs(Y1)))); colorbar;


title('Spectrum of Original Image');
subplot 224; imshow(uint8(60.*log(abs(Y2)))); colorbar;
title('Spectrum of Rotated Image');

PRACTICAL:-14

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Get an Image consisting of white square with black back ground. Rotate image 30 &
45 degree and observe i) Imrotate using nearest option and ii) Using bilinear option.

Program:-

clc; clear all;

f=[zeros(10) zeros(10) zeros(10);


zeros(10) ones(10) zeros(10);
zeros(10) zeros(10) zeros(10)];

g=imrotate(f,30,'nearest');
subplot 221; imshow(g);
title('Rotated 30^o For Nearest Option');

g=imrotate(f,45,'nearest');
subplot 222; imshow(g);
title('Rotated 45^o For Nearest Option');

g=imrotate(f,30,'bilinear');
subplot 223; imshow(g);
title('Rotated 30^o For Bilinear Option');

g=imrotate(f,45,'bilinear');
subplot 224; imshow(g);
title('Rotated 45^o For Bilinear Option');

PRACTICAL:-15

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Observe Low pass filter in frequency domain.

Program:-

clc; clear all;


f=imread('cameraman.tif');
[m n]=size(f);
for a=1:m,
for b=1:n,
d(a,b)=double(sqrt((a-m/2)^2+(b-n/2)^2));
end;
end;
D0=25;
for a=1:m,
for b=1:n,
HB(a,b)=1/(1+((d(a,b)/D0)^(2*2)));
HG(a,b)=exp(-((d(a,b)^2)/(2*(D0^2))));
if(d(a,b)<D0),
H(a,b)=1;
else
H(a,b)=0;
end;
end;
end;
subplot 331; imshow(f);
title('Original Image');
subplot 332; imshow(H);
title('Responce of Ideal Lowpass Filter');
subplot 333; imshow(HB);
title('Responce of Butterworth Lowpass Filter');
subplot 334; imshow(HG);
title('Responce of Gaussian Lowpass Filter');

Y=fftshift(fft2(f,m,n));
YI=Y.*H;
YB=Y.*HB;
YG=Y.*HG;
yi=ifft2(fftshift(YI));
yb=ifft2(fftshift(YB));
yg=ifft2(fftshift(YG));

subplot 335; imshow(uint8(yi));


title('After Ideal Lowpass Filtering');
subplot 336; imshow(uint8(yb));
title('After Butterworth Lowpass Filtering');
subplot 337; imshow(uint8(yg));
title('After Gaussian Lowpass Filtering');

PRACTICAL:-16

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Observe High pass filter in frequency domain.

Program:-

clc; clear all;


f=imread('cameraman.tif');
[m n]=size(f);
for a=1:m,
for b=1:n,
d(a,b)=double(sqrt((a-m/2)^2+(b-n/2)^2));
end;
end;
D0=20;
for a=1:m,
for b=1:n,
HB(a,b)=1/(1+((D0/d(a,b))^(2*2)));
HG(a,b)=1-exp(-((d(a,b)^2)/(2*(D0^2))));
if(d(a,b)>=D0),
H(a,b)=1;
else
H(a,b)=0;
end;
end;
end;
subplot 331; imshow(f);
title('Original Image');
subplot 332; imshow(H);
title('Responce of Ideal Highpass Filter');
subplot 333; imshow(HB);
title('Responce of Butterworth Highpass Filter');
subplot 334; imshow(HG);
title('Responce of Gaussian Highpass Filter');

Y=fftshift(fft2(f,m,n));
YI=Y.*H;
YB=Y.*HB;
YG=Y.*HG;
yi=ifft2(fftshift(YI));
yb=ifft2(fftshift(YB));
yg=ifft2(fftshift(YG));

subplot 335; imshow(uint8(yi));


title('After Ideal Highpass Filtering');
subplot 336; imshow(uint8(yb));
title('After Butterworth Highpass Filtering');
subplot 337; imshow(uint8(yg));
title('After Gaussian Highpass Filtering');

PRACTICAL:-17

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Observe Band reject filter in frequency domain as periodic noise remover.

Program:-

clc; clear all;


f=imread('cameraman.tif');
f=f(30+[1:200],20+[1:200]);
[m n]=size(f);
for a=1:m,
for b=1:n,
d(a,b)=double(sqrt((a-m/2)^2+(b-n/2)^2));
end;
end;
Y0=fftshift(fft2(double(f),m,n));
D0=70; w=10;
for a=1:m,
for b=1:n,
f(a,b)=double(f(a,b))+25.*sin(sqrt((a^2+b^2)/0.2));
HB(a,b)=1/(1+(((D0*w)/(d(a,b)*d(a,b)-D0*D0))^(2*2)));
HG(a,b)=1-exp(-((d(a,b)*d(a,b)-D0*D0)/(D0*w))^2);
if(d(a,b)>D0-w/2 && d(a,b)<D0+w/2),
H(a,b)=0;
else
H(a,b)=1;
end;
end;
end;
subplot 331; imshow(f);
title('Original Image Corrupted by Noise');
Y1=fftshift(fft2(f,m,n));
subplot 332; imshow(uint8(10.*log(abs(Y0))));
title('Spectrum of Original Image');
subplot 333; imshow(uint8(10.*log(abs(Y1))));
title('Spectrum of Noisy Image');
subplot 334; imshow(H);
title('Responce of Ideal Bandreject');
subplot 335; imshow(HB);
title('Responce of Butterworth Bandreject');
subplot 336; imshow(HG);
title('Responce of Gaussian Bandreject');
Y=fftshift(fft2(f,m,n));
YI=Y.*H; YB=Y.*HB; YG=Y.*HG;
yi=ifft2(fftshift(YI));
yb=ifft2(fftshift(YB));
yg=ifft2(fftshift(YG));
subplot 337; imshow(uint8(yi));
title('After Ideal Bandreject Filtering');
subplot 338; imshow(uint8(yb));
title('After Butterworth Bandreject Filtering');
subplot 339; imshow(uint8(yg));
title('After Gaussian Bandreject Filtering');

PRACTICAL:-18

P10EC949 Page:-
DIP Lab S.V.N.I.T.

AIM:- Write a Matlab code to blending two images in user defined manner.

Program:-

clc; clear all;


f=imread('cameraman.tif');
subplot 221; imshow(f);
title('Image-1')
g=imread('rice.png');
subplot 222; imshow(g);
title('Image-2');

alpha=0.3;
h=(1-alpha).*f+alpha.*g;
subplot 223; imshow(uint8(h));
title('Blending For \alpha=0.3');

alpha=1.0;
h=(1-alpha).*f+alpha.*g;
subplot 224; imshow(uint8(h));
title('Blending For \alpha=1.0');

P10EC949 Page:-
DIP Lab S.V.N.I.T.

PRACTICAL:-19

AIM:- Write a Matlab code to perform Erosion, Dilation, Opening and Closing.

Program:-

clc; clear all;

f=imread('coins.png');
f=f(10+[1:180],20+[1:230]);

se =strel('square',5); % Square Element of length=5

g=imerode(f,se); % Erosion
subplot 231; imshow(f);
title('Original Image');

subplot 232; imshow(g);


title('After Erosion');

h=imdilate(f,se); % Dilation
subplot 233; imshow(h);
title('After Dilation');

g=imopen(f,se); % Opening(Erosion followed by Dilation)


subplot 234; imshow(g);
title('After Opening');

h=imclose(f,se); % Closing(Dilation followed by Erosion)


subplot 235; imshow(h);
title('After Closing');

P10EC949 Page:-
DIP Lab S.V.N.I.T.

PRACTICAL:-20

AIM:- Write a Matlab code to generate different Walsh and Harr basis Images .

Program (Walsh Basis):-

clc; clear all; close all;


N=input('Enter Order:- ');
n=log2(N); n1=floor(n);
if(n~=n1),
disp('Error in Input Data');
else
A=ones(N);
for a=1:N,
for b=1:N,
p=dec2bin(a-1,n); q=dec2bin(b-1,n);
for c=1:n,
A(a,b)=A(a,b)*((-1)^(p(n+1-c)*q(c)));
end;
end;
end;
[m n]=size(A);
for a=1:m,
Im=[]; Im1=[];
for b=1:n,
if(A(a,b)==1);
Im=[Im 255.*ones(10*N,10)];
else
Im=[Im zeros(10*N,10)];
end;
if(A(b,a)==1);
Im1=[Im1;255.*ones(10,10*N)];
else
Im1=[Im1;zeros(10,10*N)];
end;
end;
ff(1,a,:,:)=Im; ff(a,1,:,:)=Im1;
end;
for a=2:N,
for b=2:N,
ff(a,b,:,:)=255-bitxor(uint8(ff(a,1,:,:)),uint8(ff(1,b,:,:)));
end;
end;

count=1;
for a=1:N,
for b=1:N,
subplot(N,N,count)
Im(:,:)=uint8(ff(a,b,:,:)); imshow(Im);
count=count+1;
end;
end;
end;

P10EC949 Page:-
DIP Lab S.V.N.I.T.

Program (Harr Basis):-

clc; clear all;

N=input('Enter the Order:- ');


n=log2(N);

for a=1:N,
k(a)=a-1;
for b=1:n,
b1=b-1;
for c=1:N,
c1=c-1;
if (k(a)==(2^b1)+c1-1),
if(b1==0 && (c1==0 || c1==1))
p(a)=b1; q(a)=c1;
elseif(b1~=0 && (c1>=1 && c1<=2^b1))
p(a)=b1; q(a)=c1;
end;
end;
end;
end;
end; % to obtain k, p and q

for a=1:N,
for b=1:N,
z=(b-1)/N;
if(k(a)==0)
A(a,b)=1/sqrt(N);
elseif(z>=(q(a)-1)/(2^p(a)) && z<(q(a)-0.5)/(2^p(a)))
A(a,b)=(2^(p(a)/2))/sqrt(N);
elseif(z>=(q(a)-0.5)/(2^p(a)) && z<(q(a))/(2^p(a)))
A(a,b)=-(2^(p(a)/2))/sqrt(N);
else
A(a,b)=0;
end;
end;
end; % to obtain Harr Matrix h
display(A);

Output Matrix:-

Enter the Order:- 4

A =

0.5000 0.5000 0.5000 0.5000

0.5000 0.5000 -0.5000 -0.5000

0.7071 -0.7071 0 0

0 0 0.7071 -0.7071

P10EC949 Page:-

You might also like