digital image processing ece 533 solutions to assignment...

25
Digital Image Processing ECE 533 Solutions to Assignment 5 Department of Electrical and Computing Engineering, University of New Mexico. Professor Majeed Hayat, [email protected] April 4, 2008 1 Frequency Domain Design 1. To generate and display f use for example: % This uses the RGB values of the image FullLena=sum(double(imread(’Lena.tif’)),3)/(3*255); Lena=FullLena(192:319,192:319); imshow(Lena); 2. If h(m, n) is real then H(u, v) is conjugate symmetric, i.e., H(u, v)= H * (-u, -v). Hence, we can exploit the symmetry of the Fourier transform about the pixel (N/2= 64, N/2 = 64) to complete the specification of the filter. Then, it is not hard to realize that H(u, v)=1, if (u, v) A, and H(u, v)=0, if (u, v) / A, where A =({0,..., 31}×{0,..., 31}) ({97,..., 127}×{97,..., 127}) ({0,..., 31{97,..., 127}) ({97,..., 127}×{0,..., 31}). The symmetry about the middle point imposes the rectangle {97,..., 127}×{97,..., 127} from the rectangle {0,..., 31{0,..., 31}. Note that the size of the new rectangle changes because of the effect of an even number of points. Similarly, the rectangle {0,..., 31}×{97,..., 127} imposes the rectangle {97,..., 127}×{0,..., 31}. See Figs. 1(a)–(c). 1

Upload: others

Post on 11-Aug-2020

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

Digital Image Processing ECE 533

Solutions to Assignment 5

Department of Electrical and Computing Engineering, University of New Mexico.

Professor Majeed Hayat, [email protected]

April 4, 2008

1 Frequency Domain Design

1. To generate and display f use for example:

% This uses the RGB values of the image

FullLena=sum(double(imread(’Lena.tif’)),3)/(3*255);

Lena=FullLena(192:319,192:319); imshow(Lena);

2. If h(m,n) is real then H(u, v) is conjugate symmetric, i.e., H(u, v) = H∗(−u,−v).

Hence, we can exploit the symmetry of the Fourier transform about the pixel (N/2 =

64, N/2 = 64) to complete the specification of the filter. Then, it is not hard to

realize that H(u, v) = 1, if (u, v) ∈ A, and H(u, v) = 0, if (u, v) /∈ A, where

A = (0, . . . , 31 × 0, . . . , 31) ∪ (97, . . . , 127 × 97, . . . , 127) ∪ (0, . . . , 31 ×97, . . . , 127)∪(97, . . . , 127×0, . . . , 31). The symmetry about the middle point

imposes the rectangle 97, . . . , 127×97, . . . , 127 from the rectangle 0, . . . , 31×0, . . . , 31. Note that the size of the new rectangle changes because of the effect

of an even number of points. Similarly, the rectangle 0, . . . , 31 × 97, . . . , 127imposes the rectangle 97, . . . , 127 × 0, . . . , 31. See Figs. 1(a)–(c).

1

Page 2: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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 conv2

m

n

m

n

g(m,n) via DFT

m

n

|G(u,v)|dB

uv

(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−1H(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]

2

Page 3: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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

g2(m,n) via conv2

m

n

m

n

g2(m,n) via DFT

m

n

|G(u,v)2|dB

uv

(c) (d)

Fig. 2: Processing of Lena, f(m, n), using the filter H2(u, v). (a) Lena and the magnitude

square, in dB, of its spatial Fourier transform. (b) Magnitude square, in dB, of H2(u, v).

Spectrum with symmetry in the middle point and centered. (c) Spatial representation of

h2(m, n) = F−1H2(u, v). (d) Result of convolving f(m, n) and h2(m, n) in spatial (upper-left)

and frequency (upper-right) domains. The magnitude square, in dB, of G2(u, v).

H(98:128,1:32)=1; % symmetric part of [0,31]x[0,31]

3. If we want to use the frequency domain to compute the convolution, we must zero

pad the spatial images. In this case M = N = 128; therefore, we need to use at

least N ′ = 2N − 1 = 255 DFT points and then compute the product in the spatial

frequency domain.

% Performing the convolution in frequency

Lenap=Lena; Lenap(2*N-1,2*N-1)=0; hp=h; hp(2*N-1,2*N-1)=0;

3

Page 4: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

g=ifft2(fft2(Lenap).*fft2(hp));

4. By comparing the images and the spectra in Figs. 1(a) and (d), we see that the filter

H is a low-pass filter and the resulting image is blurred.

5. In Figs. 2(a)–(d), we see the result of processing the image Lena.tif using the filter

H2(u, v) = 1−H(u, v). By comparison, we see that the filter is a high-pass and the

resulting image looks sharper than the original image.

6. In MATLAB:

%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+N*v+1,m+n0*n+1)=exp(-j*2*pi*(u*m+v*n)/N);

end

end

end

end hv=reshape((C’*C)\(C’)*Hv,n0,n0);

7. In Fig. 3(a) we show the result of the mask filter, with size 9 × 9, applied to the

image of Lena. Also, in Fig. 3(b) it is shown the restriction of h(m, n) to the first

9× 9 pixels and the designed mask filter h(m,n). Finally, the last comparison done

is in the frequency domain, in the same figure, in which the DFT of h(m,n) is shown

as well as the N ×N point 2D DFT of h(m,n).

8. To see that h(m,n) is the restriction of h(m, n) to a 9 × 9 subset of pixels we will

consider two approches.

In the first approach, we know that the mask filter h(m,n) is obtained from h =

(CHC)−1CHH, where h and H are stacked versions of h(m, n) and h(m,n), respec-

4

Page 5: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

tively, C is an N2×n20 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) CH

i Cj = N2, if i = j. Therefore, CHC = N2I,

and (CHC)−1 = N−2I, with I the identity matrix of dimension n20 × n2

0. Then, it

is easy to realize that h = N−2CHH, which corresponds to the first n20 terms of a

2D inverse DFT of H with N2 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

=N−1∑

i=0

N−1∑

j=0

|H(i, j)−H(i, j)|2

ε2 ∼N−1∑

i=0

N−1∑

j=0

|h(i, j)− h(i, j)|2 (from Parserval identity)

=n0−1∑

i=0

n0−1∑

j=0

|h(i, j)− h(i, j)|2 +N−1∑

i=n0

N−1∑

j=n0

|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, . . . , n0 − 1 × 0, . . . , n0 − 1.

9. Zero padding, resulting in a 255 × 255 image, is performed so that circular convo-

lution becomes equivalent to (linear) convolution in the range m,n = 0, 1, . . . , 254.

This is so because zero padding prevents aliasing that results from circular convo-

lution.

2 Frequency Domain Enhancement and Interactive Restora-

tion

1. In this problem we used interactive image enhancement in order to eliminate unde-

sired frequency components, where undesired means any frequency content that the

5

Page 6: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

g(m,n)

m

n

g~(m,n)

m

n

h(m,n)|9x9

m

n

|H(u,v)|dB

u

v

h~(m,n)

m

n

|H~(u,v)|dB

u

v

(a) (b)

Fig. 3: Processing of Lena, f(m, n), using the mask filter. (a) The filtered version of Lena

obtained convolving f(m, n) with h(m, n) (left) and with h(m, n) (right). (b) Comparison of

h(m, n) and h(m, n) in: i) spatial domain: the restriction of h(m, n) to the first 9 × 9 pixels

(upper-left) and the filter h(m, n) (lower-left); ii) frequency domain: the magnitude square, in

dB, of H(u, v) (upper-right) and the magnitude square, in dB, of H(u, v) using a 2D DFT with

N ×N points.

user believes it is wrong. The type of filter used is a notch filter. Recall that before

performing filtering in the frequency domain, i.e. transforming the convolution in

space into a product in frequency domain, we have to zero pad the images in order

to obtain a linear convolution. Note that, in this case of restoration, despite of the

fact that we actually multiply in the frequency domain we never zero padded the

initial image, so we expect some aliasing content in the restored image. Why doesn’t

it affect the resulting image? Or is that it really affect it but we “don’t care”?

2. Again, the symmetry in the spatial frequency domain is important because the

images in the spatial domain are real.

3. It is very important to note that the metrics Q-index and MSE are not useful in this

problem because we don’t have the original image. Therefore, only visual inspection

and roughness coefficient are valid metrics in this problem.

6

Page 7: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

3 Wiener Filtering

1. It is easy to see that the images follow the binomial distribution with p = 12 , so

P f(m,n) = r1 =

(N2−1K−1

)(N2

K

) =K

N2,

where(nk

)is the binomial coefficient.

E [f(m,n)] = r0 (1− P f(m,n) = r1) + r1P f(m, n) = r1

= r0

(1− K

N2

)+ r1

K

N2, µ

Also, the second moment is given by

E[f(m,n)2

]= r2

0 (1− P f(m, n) = r1) + r21P f(m,n) = r1

= r20

(1− K

N2

)+ r2

1

K

N2, µ2

2. Using the definition of conditional probabilities we can calculate the joint pmf.

P f(m,n) = r1, f(m′, n′) = r0 =P f(m,n) = r1|f(m′, n′) = r0

P f(m′, n′) = r0 .

But,

P f(m,n) = r1|f(m′, n′) = r0 =

(N2−2K−1

)(N2−1

K

) =K

N2 − 1

P f(m,n) = r1|f(m′, n′) = r1 =

(N2−2K−2

)(N2−1K−1

) =K − 1N2 − 1

.

So we obtain

P f(m,n) = r1, f(m′, n′) = r0 =K

N2 − 1N2 −K

N2=

K

N2 − 1

(1− K

N2

)

P f(m,n) = r1, f(m′, n′) = r1 =K − 1N2 − 1

K

N2

P f(m,n) = r0, f(m′, n′) = r0 = (1− P f(m,n) = r1|f(m′, n′) = r0)P f(m′, n′) = r0

=(

1− K

N2 − 1

) (1− K

N2

).

In addition, by symmetry we have

P f(m,n) = r0, f(m′, n′) = r1 = P f(m,n) = r1, f(m′, n′) = r0

=K

N2 − 1

(1− K

N2

).

7

Page 8: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

Therefore we obtain

E [f(m,n)f(m′, n′)] =1∑

i=0

1∑

j=0

rirjP f(m,n) = ri, f(m′, n′) = rj

= r20

(1− K

N2 − 1

)(1− K

N2

)+ 2r0r1

K

N2 − 1

(1− K

N2

)+

+ r21

K − 1N2 − 1

K

N2, ρ .

3. The elements, Rff (m,n), of autocorrelation matrix, Rff , are given by

Rff (0, 0) = E [f(m,n)f(m,n)] = E[f(m,n)2

]= µ2

Rff (m,n) = E [f(i, i)f(i + m, j + n)] = ρ, m, n = 1, 2, . . . , N − 1 .

4. In the case of the first and the second moment of η(m,n) we have

E [η(m,n)] =∫ a

−a

12a

xdx = 0

E[η(m,n)2

]=

∫ a

−a

12a

x2dx =a2

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.

5. From the previous calculation we see that Sηη(0, 0) = E[η(m,n)2

]= ση and

Sηη(m,n) = 0, m, n = 1, 2, . . . , N − 1.

6. From notes, we know that the Wiener filter is given by

Hr(u, v) =H∗(u, v)Sff (u, v)

|H(u, v)|2Sff (u, v) + Rηη(u, v),

and the restored image is

F (u, v) = G(u, v)Hr(u, v) = G(u, v)H∗(u, v)Sff (u, v)

|H(u, v)|2Sff (u, v) + Rηη(u, v),

where G(u, v) = Fg(m,n).

7. The inverse filtering was implemented using a threshold value to avoid dividing

by zero. The results obtained for three different threshold values are shown in

Fig. 4. From notes we know that the inverse filtering is extremely sensitive to noise;

therefore, we will obtain a poor performance in cases of low signal-to-noise ratio

(SNR) as it is shown in Fig. 4.

8

Page 9: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

8. The Wiener filter minimizes the square of the error in an average sense. It does not

guarantee a least squared error for each f(m,n) ∈ Ω.

9. The previous image restoration was repeated now employing a Wiener filter. The

results are shown in Fig. 5 and it results clear that the Wiener filter outperforms

the inverse filter in cases of low SNR. Moreover, assume that we now the model for

the noise and the class of images, but we don’t have the theoretical expressions for

the autocorrelation functions. In such cases, the autocorrelation functions can be

estimated from a samples of the noise and from a sample of image, assuming that

the random process are ergodic. In Fig. 5(c) the restored images obtained using

a Wiener filter calculated using Rff and Rηη, estimates of Rff and Rηη, instead

of the actual values. Finally, consider an even worst scenario where samples of the

image are not available. In such case, Wiener filtering can still be applied assuming a

certain SNR, as in the case depicted in Fig. 5(d). Note that results can be improved

if we select a different SNR for each case of degradation (Try it!). It can be seen that

the restored images are fairly good and in the case of low SNR, Wiener filtering still

outperforms the inverse filtering. Below a MATLAB code implementing the inverse

filter as well as the Wiener filter is presented.

clear all; clc;

load wiener.mat

RestorationType=1; % 1: inverse filtering

% 2: wiener filtering

Quantities=0; % 0: Teoretical, 1: experimental, 2: SNR

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

% Data

g=cat(3,fdeg001,fdeg01); g=cat(3,g,fdeg2);

[M N]=size(g(:,:,1));

A=[0.001 0.01 2];

9

Page 10: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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:3

switch RestorationType

case 1 % Inverse Filtering

% Threshold the min. values

Thr=0.05; % Try: 0.01, 0.05 and 0.5

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/(M*N-1))*(1-k/(M*N))+2*r0*r1*(k/(M*N-1))*(1-k/(M*N))+...

r1^2*k*(k-1)/(M*N*(M*N-1));

mu2=r0^2*(1-k/(M*N))+r1^2*k/(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

10

Page 11: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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/(M*N);

SampImg=rand(M,N);

SampImg=double(r0*(SampImg>Pr1))+double(r1*(SampImg<=Pr1));

rff=xcorr2(SampImg)/(M*N);

rff=rff(M:(2*M-1),N:(2*N-1));

Sff=fft2(rff);

% Experimental autocorrelation of the noise, similarly

eta=2*A(i)*(rand(M,N)-0.5);

rnn=xcorr2(eta)/(M*N);

rnn=rnn(M:(2*M-1),N:(2*N-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’));

11

Page 12: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

figure(3+i);

imshow(fhat,[]);

if RestorationType==1

print(3+i,’-deps’,strcat(’../Solutions/Prob03InvF’,num2str(i),...

num2str(fix(100*Thr)),’.eps’));

else

print(3+i,’-deps’,strcat(’../Solutions/Prob03WF’,num2str(i),...

num2str(Quantities),’.eps’));

end

end

10. Computing-time issues: A general linear convolution operation between and image,

f(x, y), with N1×N1 pixels and a mask filter, h(m,n), withN2×N2 pixels requires in

the spatial domain N21 N2

2 floating-point operations (multiplications and sums). On

the other hand, using DFT, multiplication, inverse DFT it requires 4N ′ log(2N ′)

floating-point operations, where N ′ = 2k ≥ N1 + N2 − 1. Therefore, as a good

practice we should perform convolution in the space-domain for “small” convolving

functions, and DFT if the convolving functions are “large”. What means “small”

and “large”? It depends on the case under analysis. Table .1 lists the first order

statistics of the computing-time, obtained over 100 trials, when the convolution

operation was performed in both domains for different mask filter sizes in the case

of the image restoration. Note that the computing time is more or less the same for

all mask sizes in the frequency-domain, while in the spatial domain the computing-

time severily increases when the mask size increases. Note also that in this case we

require to compute a circular convolution (why?), therefore N ′ = 2k ≥ maxN1, N2.

In Fig. 6 we show the restored versions of the images fdeg001, fdeg01 and fdeg2

using small mask sizes (5 × 5, 11 × 11, and 15 × 15). Note that the restoration

achieved by these masks has a poor performance as compared to the riginal mask

size of 128× 128.

12

Page 13: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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.0085

fdeg01 0.0070 0.0065 0.0130 0.0060 0.0195 0.0045 1.3825 0.0060

fdeg2 0.0045 0.0080 0.0115 0.0065 0.0190 0.0075 1.3935 0.0060

Standard deviation of convolution’s computing time.

fdeg001 0.0237 0.0051 0.0052 0.0037 0.0022 0.0041 0.0055 0.0037

fdeg01 0.0047 0.0049 0.0047 0.0050 0.0022 0.0051 0.0044 0.0050

fdeg2 0.0051 0.0041 0.0037 0.0049 0.0031 0.0055 0.0075 0.0050

Additional Problems on Image Restoration

4 Problem

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+

(D2

0D1(u,v)D2(u,v)

)n , if ||Di||2 ≤ D0, i = 1, 2

1 , otherwise

HG(u, v) =

1− exp− 1

2

(D1(u,v)D2(u,v)

D20

), if ||Di||2 ≤ D0, i = 1, 2

1 , otherwise

where D = (u0, v0) is the location of the center of a band of frequencies to reject, D0 is the

radius of such band, and Di(u, v) =√

(u−M/2 + (−1)iu0)2 + (v −N/2 + (−1)iv0)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

13

Page 14: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

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);

14

Page 15: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

% Compute the points of the grid

if (mod(M,2)==0)

m=(-M/2):(M/2);

M2=M/2;

else

m=(-(M-1)/2):((M-1)/2);

M2=(M-1)/2;

end

if (mod(N,2)==0)

n=-(N/2):(N/2);

N2=N/2;

else

n=(-(N-1)/2):((N-1)/2);

N2=(N-1)/2;

end

% Creates the grid

[v u]=meshgrid(n,m);

H=0;

for i=1:size(D0,1)

% Compute the points D1 and D2

D1=sqrt( ( u-(D0(i,1)-M2-1) ).^2 + ( v-(D0(i,2)-N2-1) ).^2 );

D2=sqrt( ( u+(D0(i,1)-M2-1) ).^2 + ( v+(D0(i,2)-N2-1) ).^2 );

D=D1.*D2./D0(i,3).^2;

% Compute the zones with zeros and ones

I=double(D1<=D0(i,3))+double(D2<=D0(i,3));

switch FilterType

case ’Gauss ’

H=H+I.*(1-exp(-0.5.*D));

case ’Butter’

H=H+I.*(1./(1+D.^(-d)));

end

end

% Generate the filter

H=1-H;

15

Page 16: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

Table 2: Location of the center of the rejection frequency bands, D, as well as the radius, D0, of bands

to reject using the notch filters in Problem 1. Use symmetry to eliminate the conjugate symmetric

frequencies.

D(u0, v0)

u0 v0 D0

-147 -155 6

-25 -38 7

-160 -91 7

-118 -130 6

-162 -130 2

-174 130 10

5 Problem

It is not hard to show that the restoration filter Hr(u, v), in the frequency domain, that

minimizes127∑

m=0

127∑n=0

|(q ⊗ f)(m,n)|2

subject to the constraint

127∑m=0

127∑n=0

|(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 = Fh(m,n), Q = Fq(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’sImage Processing Toolbox functions: deconvwnr and deconvreg. Also take a look at:deconvblind and deconvlucy.

16

Page 17: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

%#########################################

% 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 -1

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:3

% Power of the noise

Sigma=A(i)^2/3;

% Tolerance (in %)

a=20/100*Sigma;

% Stop condition

NoiseP=M*N*Sigma;

% Counter for number of iterations

ctr=0;

while (1)

% Calculate the restoration filter

Hr=conj(H)./((H.*conj(H))+gamma(i)*(Q.*conj(Q)));

17

Page 18: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

% Calculate the restored image

FHAT=Hr.*fft2(g(:,:,i));

fhat=real(ifft2(FHAT));

% Evaluate the constraint

e=g(:,:,i)-fhat;

Norm=e(:).’*e(:);

% Evaluate the stopping condition of the iterations

if ( abs(Norm-NoiseP)<=a || ctr>MaxIters )

ctr

break

end

if (Norm>NoiseP)

gamma(i)=gamma(i)-Dgamma(i);

end

if (Norm<NoiseP)

gamma(i)=gamma(i)+Dgamma(i);

end

ctr=ctr+1;

G(i,ctr)=gamma(i);

end

end

6 Problem

The impulse response of the image degradation system is h(x − α, y − β) = exp−(x −α)2 − (y − β)2. When the input to the system is f(x, y) = δ(x − a) the output image,

g(x, y), is given by

g(x, y) =∫ ∞

−∞

∫ ∞

−∞h(x− α, y − β)f(α, β) dαdβ

=∫ ∞

−∞

∫ ∞

−∞e−((x−α)2+(y−β)2)δ(α− a) dαdβ

=∫ ∞

−∞e−((a−α)2+(y−β)2) dβ =

√πe−(a−α)2

18

Page 19: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

7 Problem

The general equation of the Wiener filter is

Hr(u, v) =H∗(u, v)Sff (u, v)

|H(u, v)|2Sff (u, v) + Rηη(u, v).

If the noise is negligible, then Rηη(u, v) ≈ 0. Hence, the previous equation transforms in

Hr(u, v) ≈ 1H(u, v)

∴ Hr(u, v) = exp(u2 + v2)/(2σ2) ,

i.e., an inverse filter.

8 Problem

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 + 2ReH(u, v)F (u, v)N(u, v)+ |N(u, v)|2]

= E[|H(u, v)|2|F (u, v)|2] + 2ReE[H(u, v)F (u, v)N(u, v)]

+ E[|N(u, v)|2] (by linearity of expectation)

= |H(u, v)|2E[|F (u, v)|2] + 2ReH(u, v)E[F (u, v)N(u, v)]

+ E[|N(u, v)|2] (H(u, v) is not random)

= |H(u, v)|2E[|F (u, v)|2] + 2ReH(u, v)E[F (u, v)]E[N(u, v)]

+ E[|N(u, v)|2] (noise and signal are uncorrelated)

= |H(u, v)|2E[|F (u, v)|2] + E[|N(u, v)|2] (noise is zero-mean) .

Therefore, using the definitions Sff (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)|2Sff (u, v) + Sηη(u, v) .

19

Page 20: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

9 Problem

1. Since Sf f (u, v) = |R(u, v)|2|Sgg(u, v)|2, from the previous problem we obtain

Sf f (u, v) = |R(u, v)|2|Sgg(u, v)|2

= |R(u, v)|2 (|H(u, v)|2Sff (u, v) + Sηη(u, v))

.

If we assume that Sf f (u, v) = Sff (u, v) and then solve for R(u, v) we obtain

R(u, v) =(|H(u, v)|2 +

Sηη(u, v)Sff (u, v)

)− 12

. (1)

2. When R(u, v) is as in equation (1) then

F (u, v) =(|H(u, v)|2 +

Sηη(u, v)Sff (u, v)

)− 12

G(u, v) .

20

Page 21: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

(a)

(c)

(b)

(d)

Fig. 4: Restoration using inverse filtering. (a) From left to right: the images fdeg001, fdeg01 and

fdeg2. (b) Restoration of the previous images with a threshold value of: 0.01; (c) 0.05; and (d)

0.5.

21

Page 22: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

(a)

(b)

(c)

(d)

Fig. 5: Restoration using Wiener filtering. (a) From left to right: the images fdeg001, fdeg01 and

fdeg2. (b) Restoration of the previous images using a Wiener filter specified with the theoretical

values. (c) Restoration using a Wiener filter specified estimating the autocorrelation functions

of f(m, n) and η(m, n). (d) Restoration using a Wiener filter assuming a SNR=12 dB for the

three cases.22

Page 23: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

(a)

(b)

(c)

(d)

Fig. 6: Restoration using Wiener filtering with some mask sizes. From left to right: the restored

images corresponding to fdeg001, fdeg01 and fdeg2 (shown in Fig. FIG:WFa)) using a Wiener

filter of size: (a) 128× 128, (b) 5× 5, (c) 11× 11, and (d) 15× 15.

23

Page 24: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

n

m

u

v

(a) (b)

n

m

n

m

n

m

(c) (d) (e)

u

v

u

v

u

v

(f) (g) (h)

Fig. 7: Image restoration of the Lincoln.tif image using the notch filters. (a) The original image.

Restored versions of Lincoln.tif using: (c) a Gaussian filter, a Butterworth filter of order (d)

n = 2, and (e) n = 10. The magnitude square, in dB, of the FFT of the (b) original image,

restored image using (f) a Gaussian filter, a Butterworth filter of order (g) n = 2, and (h) n = 10.

The frequency bands rejected by the filters are shown in Table 2.

24

Page 25: Digital Image Processing ECE 533 Solutions to Assignment 5ece-research.unm.edu/hayat/ece533/Sol_Assignment05_08.pdfDigital Image Processing ECE 533 Solutions to Assignment 5 Department

(a) (b) (c)

(d) (e) (f)

Fig. 8: Restoration of images using constrained least-squares optimization. From left to right: the

images (a) fdeg001, (b) fdeg01, and (c) fdeg2. Restoration of the previous images with parameter

(d) γ = 0.49, (d) γ = 0.70, and (d) γ = 1.9, respectively.

25