-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency_domain_filtering_butterworth_bpf.m
97 lines (81 loc) · 1.91 KB
/
frequency_domain_filtering_butterworth_bpf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
clear all
close all
I=imread('peppers_gray.bmp');
dim_1=size(I);
I=double(I);
%Zero Padding the image
Ip=[I zeros(dim_1); zeros(dim_1) zeros(dim_1)];
dim_2=size(Ip)
figure(1)
subplot(121)
imshow(uint8(I))
title('Original Image')
subplot(122)
imshow(uint8(Ip))
title('Image after Zero Padding')
%Creating meshgrid
x = 1:dim_2(1);
y = 1:dim_2(2);
[X,Y]=meshgrid(x,y);
M=(-1).^(X+Y);
%2-D butterworth filter
n=2; %order of filter
P=dim_2(1);
Q=dim_2(2);
D0=100;%Lower cutoff- frequency
D1=200;%upper cutoff- frequency
D=((X-P/2).^2+(Y-Q/2).^2).^.5;
%Butterworth Lowpass_1
H_L1=1./(1+(D/D0).^(2*n));
%Butterworth Lowpass_2
H_L2=1./(1+(D/D1).^(2*n));
%Butterworth Bandpass
H_bp=H_L2-H_L1;
H_bs=1-(H_L2-H_L1);
%2-D filter plot
figure(2)
subplot(221)
surfc(X,Y,H_bp)
shading interp
title('Butterworth Bandpass Filter')
subplot(222)
surfc(X,Y,H_bs)
shading interp
title('Butterworth Bandstop Filter')
%Lowpass filter mask
subplot(223)
imshow(H_bp)
title('Butterworth Bandpass Filter')
%Highpass filter mask
subplot(224)
imshow(H_bs)
title('Butterworth Bandstop Filter')
% Frequency Domain Filtering using fft
I_f=fft2(M.*Ip);
I_bp=I_f.*H_bp;
I_bs=I_f.*H_bs;
I1=real(ifft2(I_bp)).*M;
I2=real(ifft2(I_bs)).*M;
figure(3)
subplot(131)
imshow(uint16(I_f))
title('Fourier Transform of Image')
subplot(232)
imshow(uint16(I_bp))
title('Image after Band Pass Filtering(Frequency Domain)')
subplot(233)
imshow(uint16(I_bs))
title('Image after Band Stop Filtering(Frequency Domain)')
subplot(235)
imshow(uint8(I1))
title('Image after Band Pass Filtering')
subplot(236)
imshow(uint8(I2))
title('Image after Band Stop Filtering')
figure(4)
subplot(121)
imshow(uint8(I1(1:dim_1(1),1:dim_1(2))))
title('Image after Band Pass Filtering')
subplot(122)
imshow(uint8(I2(1:dim_1(1),1:dim_1(2))))
title('Image after Band Stop Filtering')