

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Assignment; Class: Digital Image Processing; Subject: Electrical & Computer Engineer; University: University of New Mexico; Term: Spring 2008;
Typology: Assignments
1 / 25
This page cannot be seen from the preview
Don't miss anything!


















Professor Majeed Hayat, [email protected]
f(m,n)
m
n
|F(u,v)|dB
u
v
|H(u,v)|dB
u
v
|H(u,v)|dB centered
u
v
(a) (b)
h(m,n)
m
n
h(m,n) centered
m
n
g(m,n) via conv
m
n
m
n
g(m,n) via DFT
m
n
|G(u,v)|dB
u
v
(c) (d)
Fig. 1: Processing of Lena, f (m, n), using the filter H(u, v). (a) Lena and the magnitude square, in dB, of its spatial Fourier transform. (b) Magnitude square, in dB, of H(u, v). Spectrum with symmetry in the middle point and centered. (c) Spatial representation of h(m, n) = F −^1 {H(u, v)}. (d) Result of convolving f (m, n) and h(m, n) in spatial (upper-left) and frequency (upper-right) domains. The magnitude square, in dB, of G(u, v).
%######################################### % Filter specification % Pixels [0,31]x[97,127] H(1:32,1:32)=1; % Pixels [0,31]x[0,31] H(1:32,98:128)=1; % Completing the filter: symmetry point (64.5,64.5) H(98:128,98:128)=1; % symmetric part of [0,31]x[97,127]
g=ifft2(fft2(Lenap).*fft2(hp));
%Create C matrix n0=9; C=zeros(N^2,n0^2); Hv=reshape(H,N^2,1); % Very inefficient way to do it for u=0:(N-1) for v=0:(N-1) for m=0:(n0-1) for n=0:(n0-1) C(u+Nv+1,m+n0n+1)=exp(-j2pi(um+vn)/N); end end end end hv=reshape((C’C)(C’)*Hv,n0,n0);
tively, C is an N 2 ×n^20 DFT matrix and H stands for conjugate-transpose (Hermitian operation). In the notes we saw that the columns of C are linearly independent, and moreover, we calculated the inner product between any two columns, say i and j, of C: i) CHi Cj = 0, if i 6 = j; and ii) CHi Cj = N 2 , if i = j. Therefore, CH^ C = N 2 I, and (CH^ C)−^1 = N −^2 I, with I the identity matrix of dimension n^20 × n^20. Then, it is easy to realize that h˜ = N −^2 CH^ H, which corresponds to the first n^20 terms of a 2D inverse DFT of H with N 2 points. Recalling that we have to undo the stacking the claim is proved. In the second approach (as done in class), consider the optimization criteria used to construct the mask filter: ε^2 = || H˜ − H||^22 =
i=
j=
| H˜(i, j) − H(i, j)|^2
ε^2 ∼
i=
j=
|˜h(i, j) − h(i, j)|^2 (from Parserval identity)
=
n∑ 0 − 1 i=
n∑ 0 − 1 j=
|˜h(i, j) − h(i, j)|^2 +
i=n 0
j=n 0
|h(i, j)|^2 (by mask size).
It is therefore seen right-hand side can be minimized by setting its first term to zero, or equivalently, by setting ˜h = h for all (m, n) ∈ { 0 ,... , n 0 − 1 } × { 0 ,... , n 0 − 1 }.
P {f (m, n) = r 1 } =
K− 1
K
where (nk^ )^ is the binomial coefficient. E [f (m, n)] = r 0 (1 − P {f (m, n) = r 1 }) + r 1 P {f (m, n) = r 1 } = r 0
′, n′) = r 0 } P {f (m′, n′) = r 0 }. But,
P {f (m, n) = r 1 |f (m′, n′) = r 0 } =
K− 1
K
P {f (m, n) = r 1 |f (m′, n′) = r 1 } =
K− 2
K− 1
So we obtain P {f (m, n) = r 1 , f (m′, n′) = r 0 } = (^) N 2 K (^) − 1 N^
P {f (m, n) = r 1 , f (m′, n′) = r 1 } = (^) NK 2 − (^) −^1 1 N^ K 2 P {f (m, n) = r 0 , f (m′, n′) = r 0 } = (1 − P {f (m, n) = r 1 |f (m′, n′) = r 0 }) P {f (m′, n′) = r 0 } =
In addition, by symmetry we have P {f (m, n) = r 0 , f (m′, n′) = r 1 } = P {f (m, n) = r 1 , f (m′, n′) = r 0 } = (^) N 2 K (^) − 1
Therefore we obtain E [f (m, n)f (m′, n′)] =
i=
j=
rirj P {f (m, n) = ri, f (m′, n′) = rj }
= r^20
Rf f (0, 0) = E [f (m, n)f (m, n)] = E [f (m, n)^2 ]^ = μ 2 Rf f (m, n) = E [f (i, i)f (i + m, j + n)] = ρ, m, n = 1, 2 ,... , N − 1.
E [η(m, n)] =
∫ (^) a −a
2 a xdx^ = 0 E [η(m, n)^2 ]^ =
∫ (^) a −a
2 a x
(^2) dx = a^2 3 =^ ση^ , while E [η(m, n)η(m′, n′)] = E [η(m, n)] E [η(m′, n′)] = 0, because of the indepen- dence between the entries of the noise in the noise matrix.
Hr (u, v) = H
∗(u, v)Sf f (u, v) |H(u, v)|^2 Sf f (u, v) + Rηη (u, v) , and the restored image is Fˆ (u, v) = G(u, v)Hr (u, v) = G(u, v) (^) |H(u, vH)|∗ 2 (Su, v)Sf f^ (u, v) f f (u, v) +^ Rηη (u, v)^ , where G(u, v) = F {g(m, n)}.
k=900; L=15; r0=5; r1=10; % Blurring model h(1:L,1:L)=L^(-2); % Wiener Filter H=fft2(h,M,N);
for i=1: switch RestorationType case 1 % Inverse Filtering % Threshold the min. values Thr=0.05; % Try: 0.01, 0.05 and 0. Idx=abs(H)<=Thr; H(Idx)=Thr; % Calculate the FFT of the degraded image G=fft2(g(:,:,i),M,N); % Convolve in frequency FHAT=G./H; fhat=ifft2(FHAT); case 2 % Wiener Filtering switch Quantities case 0 % Use theoretical values % Calculating rho and mu for the autocorrelation functions rho=r0^2(1-k/(MN-1))(1-k/(MN))+2r0r1(k/(MN-1))(1-k/(MN))+... r1^2k(k-1)/(MN(MN-1)); mu2=r0^2(1-k/(MN))+r1^2k/(M*N); % Autocorrelation of the image class and PSD rff(1:M,1:N)=rho; rff(1,1)=mu2; Sff=fft2(rff); % Autocorrelation of the noise and PSD
rnn=zeros(M,N); rnn(1,1)=A(i)^2/3; Snn=fft2(rnn); Hr=conj(H).Sff./((H.conj(H)).Sff+Snn); case 1 % Try to approximate the theoretical values % Experimental autocorrelation of the images: generate % a sample image and compute its autocorrelation Pr1=k/(MN); SampImg=rand(M,N); SampImg=double(r0(SampImg>Pr1))+double(r1(SampImg<=Pr1)); rff=xcorr2(SampImg)/(MN); rff=rff(M:(2M-1),N:(2N-1)); Sff=fft2(rff); % Experimental autocorrelation of the noise, similarly eta=2A(i)(rand(M,N)-0.5); rnn=xcorr2(eta)/(MN); rnn=rnn(M:(2M-1),N:(2N-1)); Snn=fft2(rnn); Hr=conj(H).Sff./((H.conj(H)).Sff+Snn); case 2 SNR=10^(12/10); % Try others (rff./rnn Theoretical value) Hr=conj(H)./((H.conj(H))+1./SNR); end FHAT=Hr.*fft2(g(:,:,i)); fhat=real(ifft2(FHAT)); end
figure(i); imshow(g(:,:,i),[]); print(i,’-deps’,strcat(’../Solutions/Prob03InvF’,num2str(i),’.eps’));
Table 1: The average and the standard deviation, in seconds, of the computing-time of the convolution between a Wiener filter and a degraded image. Calculations were performed in spatial-domain (SD) as well as in the frequency-domain (FD). Average of convolution’s computing time. Mask: 5× 5 Mask: 11× 11 Mask: 15× 15 Mask: 128× 128 SD FD SD FD SD FD SD FD fdeg001 0.0115 0.0055 0.0120 0.0085 0.0205 0.0080 1.3825 0. fdeg01 0.0070 0.0065 0.0130 0.0060 0.0195 0.0045 1.3825 0. fdeg2 0.0045 0.0080 0.0115 0.0065 0.0190 0.0075 1.3935 0. Standard deviation of convolution’s computing time. fdeg001 0.0237 0.0051 0.0052 0.0037 0.0022 0.0041 0.0055 0. fdeg01 0.0047 0.0049 0.0047 0.0050 0.0022 0.0051 0.0044 0. fdeg2 0.0051 0.0041 0.0037 0.0049 0.0031 0.0055 0.0075 0.
The type of noise corrupting Lincoln image is periodic. This is a typical example of elec- trical or electromechanical interference arising during the image acquisition step. In such scenarios, the periodic noise can be reduced significantly via frequency-domain filtering. Hence, we employ to different types of notch filters: i) a Butterworth notch filter of order n, and ii) a Gaussian notch filter. The transfer function of such filters are
HB (u, v) =
1 1+
( (^) D (^20) D 1 (u,v)D 2 (u,v)
)n , if ||Di|| 2 ≤ D 0 , i = 1, 2
1 , otherwise
HG(u, v) =
1 − exp
( (^) D 1 (u,v)D 2 (u,v) D^20
, if ||Di|| 2 ≤ D 0 , i = 1, 2 1 , otherwise
where D = (u 0 , v 0 ) is the location of the center of a band of frequencies to reject, D 0 is the radius of such band, and Di(u, v) = √(u − M/2 + (−1)iu 0 )^2 + (v − N/2 + (−1)iv 0 )^2 , i = 1 , 2, formulation that considers the symmetry of the DFT. The results of applying a Gaussian filter and two Butterworth filters (of order n = 2
and n = 10) are shown in Fig. 7. The frequency bands rejected by the filters are shown in Table 2. Clearly, the poriodic noise is reduced in all cases. Note that for a Butterworth filter of degree n = 10 the decay is much more abrupt than in the case of the same type of filter with degree n = 2 or for the Gaussian filter. Note also that ringing effects does not appear, to the naked eye at least, because the filters are not ideal. Notes: i) Recall that if we want to perform a linear convolution operation, then we have to zero pad the image and the filter properly before multiplying in the frequency domain. Note that, in this type of restoration we do not zero pad the image. You should be able to answer yourself why this does not affect the restored image. ii) The symmetry in the spatial frequency domain is important because the images in the spatial domain are real. iii) Some of you still use the metrics Q-index and MSE in this problem, so it is important that you understand that those are not useful in this problem because we don’t have the original image. Therefore, only visual inspection and roughness coefficient are valid metrics. A MATLAB function implementing the filters is the following:
function H=NotchFilters(D0,SizeImg,FilterType,d) % % NotchFilters: function to create a Gaussian or Butterworth notch filter % % Inputs: % D0: matrix of size Mx3 where M is the center of the frequencies to % eliminate. The first two columns give the position and the last one % the radius of the notch % SizeImg: [M N] the dimension of the filter % FilterType: ’Gauss ’ Gaussian notch filter; ’Butter’ Butterworth % filter % d: order of the Butterworth filter % % Output: % H: the notch filter
M=SizeImg(1); N=SizeImg(2);
Table 2: Location of the center of the rejection frequency bands, D, as well as the radius, D 0 , of bands to reject using the notch filters in Problem 1. Use symmetry to eliminate the conjugate symmetric frequencies. D(u 0 , v 0 ) u 0 v 0 D 0 -147 -155 6 -25 -38 7 -160 -91 7 -118 -130 6 -162 -130 2 -174 130 10
It is not hard to show that the restoration filter Hr (u, v), in the frequency domain, that minimizes (^127) ∑ m=
n=
|(q ⊗ fˆ )(m, n)|^2
subject to the constraint
∑^127 m=
n=
|(h ⊗ fˆ )(m, n) − g(m, n)|^2 = (128)^2 ση ,
is given by Hr (u, v) = H
∗(u, v) |H(u, v)|^2 + γ|Q(u, v)|^2 , where Hr = F {h(m, n)}, Q = F {q(m, n)}, and γ is a parameter adaptively calculated using an iterative procedure, which tries to satisfy the constraint. Fig. 8 shows the results obtained for the restoration of the figures fdeg001, fdeg01, and fdeg2. The significance of the minimization is that it reduces the roughness of the image, while the constraint guarantees that h ⊗ fˆ − g is equivalent to the noise η, in a second-moment sense. Below you can find a MATLAB script to calculate the restoring filter using constrained optimization. However, you are encouraged to take a look to the following MATLAB’s Image Processing Toolbox functions: deconvwnr and deconvreg. Also take a look at: deconvblind and deconvlucy.
%######################################### % Problem 02 HW #5 ECE 533 Spring 07 %######################################### clear all; clc; load wiener.mat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Data g=cat(3,fdeg001,fdeg01); g=cat(3,g,fdeg2); [M N]=size(g(:,:,1)); A=[0.001 0.01 2]; k=900; L=15; r0=5; r1=10; % Blurring model h(1:L,1:L)=L^(-2); % Wiener Filter H=fft2(h,M,N); % Laplacian q=[0 0 0; 0 -1 0 -1 4 - 0 -1 0]; Q=fft2(q,M,N); % Parameter gamma=[0.5 0.7 2]; Dgamma=[10^-6 10^-6 10^-6]; MaxIters=5000; G=zeros(3,MaxIters); for i=1: % Power of the noise Sigma=A(i)^2/3; % Tolerance (in %) a=20/100Sigma; % Stop condition NoiseP=MNSigma; % Counter for number of iterations ctr=0; while (1) % Calculate the restoration filter Hr=conj(H)./((H.conj(H))+gamma(i)(Q.conj(Q)));
The general equation of the Wiener filter is
Hr (u, v) = H
∗(u, v)Sf f (u, v) |H(u, v)|^2 Sf f (u, v) + Rηη (u, v).
If the noise is negligible, then Rηη (u, v) ≈ 0. Hence, the previous equation transforms in
Hr (u, v) ≈ (^) H(u, v^1 ) ∴ Hr (u, v) = exp{(u^2 + v^2 )/(2σ^2 )} ,
i.e., an inverse filter.
Assume that E[N (n, m)] = 0 and that the noise and the signal are uncorrelated, then
E[|G(u, v)|^2 ] = E[|H(u, v)F (u, v) + N (u, v)|^2 ] = E[|H(u, v)|^2 |F (u, v)|^2 + 2Re{H(u, v)F (u, v)N (u, v)} + |N (u, v)|^2 ] = E[|H(u, v)|^2 |F (u, v)|^2 ] + 2Re{E[H(u, v)F (u, v)N (u, v)]}
Therefore, using the definitions Sf f (u, v) , E[|F (u, v)|^2 ], Sηη (u, v) , E[|N (u, v)|^2 ], and Sgg (u, v) , E[|G(u, v)|^2 ] we obtain
Sgg (u, v) = |H(u, v)|^2 Sf f (u, v) + Sηη (u, v).
S (^) fˆ fˆ (u, v) = |R(u, v)|^2 |Sgg (u, v)|^2 = |R(u, v)|^2 (|H(u, v)|^2 Sf f (u, v) + Sηη (u, v))^.
If we assume that S (^) fˆ fˆ (u, v) = Sf f (u, v) and then solve for R(u, v) we obtain
R(u, v) =
|H(u, v)|^2 + S Sηηf f^ ((u, vu, v))
Fˆ (u, v) =
|H(u, v)|^2 + S Sf fηη^ ((u, vu, v))
G(u, v).