Docsity

Final Exam Solutions - Image Processing | EECS 556, Exams of Electrical and Electronics Engineering

Toggle sidebar
bg1
EECS 556, Winter 2002 Final Exam Solutions 1
Dear EECS 556 Class – Thanks for your attention and participation in class. I hope you learned
as much from taking this class as I did from teaching it. The class performance on this exam was
excellent and the median grade on the final was 91. Please accept my best wishes for an
enjoyable summer. Cheers, DN
1. Transforms and transform coding.
a. Consider the N-length Fourier transform coefficients:
∑∑ −
=
−
=
−
=
−= 1
0
1
0
2sin2cos)(
1
2exp)(
1
)( N
n
N
nN
kn
i
N
kn
nx
NN
kn
inx
N
kXπππ .
For real valued x(n), we know that X(k) = X*(N-k). We also know that X(0) and X(N/2)
are real-valued. Thus, there are exactly N unique real-valued coefficients: X(0), X(N/2),
real{X(k)}, and imag{X(k)} for k=1, 2, … N/2-1. The transform kernel for each of these
is
N
1 times [1 1 … 1], [1 -1 1 … -1],
N
kn
π2cos , and
−N
kn
π2sin , respectively, for
n=0, 1, 2, … N-1. For an energy preserving transformation, we’ll need to normalize each
kernel to have unit energy. Ordering form highest to lowest energy usually means
ordering from lowest frequency to highest frequency. So, the 8x8 transformation matrix
is:
1 1 1 1 1 1 1 1
8
2cos2n
π
−8
2sin2n
π
8
2
2cos2n
π
−8
2
2sin2n
π
8
3
2cos2n
π
−8
3
2sin2n
π
A = 8
1
1 -1 1 -1 1 -1 1 -1
For n=0, 1, 2, … N-1.
b. Since the Fourier kernel is orthogonal and we have made the rows unit length, the A
matrix is unitary and thus, the inverse transform is the transpose of A: B = A’.
c. The 8x8 transformation of some subimage x is simply the transform of the columns of x
by A (e.g. y = Ax), followed by the transformation of the columns of y’ by A (e.g. X =
(Ay’)’ ). The net result is X = AxA’ .
d. The 8x8 inverse transformation of some transform block X is simply the transform of the
columns of x by A’ (e.g. y = A’X), followed by the transformation of the columns of y’ by
A’ (e.g. x = (A’y’)’ ). The net result is x = A’XA .
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Final Exam Solutions - Image Processing | EECS 556 and more Exams Electrical and Electronics Engineering in PDF only on Docsity!

Dear EECS 556 Class – Thanks for your attention and participation in class. I hope you learned as much from taking this class as I did from teaching it. The class performance on this exam was excellent and the median grade on the final was 91. Please accept my best wishes for an enjoyable summer. Cheers, DN

  1. Transforms and transform coding. a. Consider the N -length Fourier transform coefficients:

−

=

−

=

1

0

1

0

( ) cos 2 sin 2

( )exp 2

N

n

N

n N

kn i N

kn xn N N

kn xn i N

X k π π π.

For real valued x ( n ), we know that X ( k ) = X* ( N - k ). We also know that X (0) and X ( N /2) are real-valued. Thus, there are exactly N unique real-valued coefficients: X (0), X ( N /2), real{ X ( k )}, and imag{ X ( k )} for k =1, 2, … N /2-1. The transform kernel for each of these

is N

times [1 1 … 1], [1 -1 1 … -1], (^)  

N

kn cos 2 π , and (^)  

N

kn sin 2 π , respectively, for

n =0, 1, 2, … N -1. For an energy preserving transformation, we’ll need to normalize each kernel to have unit energy. Ordering form highest to lowest energy usually means ordering from lowest frequency to highest frequency. So, the 8x8 transformation matrix is:

1 1 1 1 1 1 1 1

 

2 cos 2

n π

2 sin 2

n π

2 cos 2

n π

2 sin 2

n π

2 cos 2

n π

2 sin 2

n π

A =

For n =0, 1, 2, … N -1. b. Since the Fourier kernel is orthogonal and we have made the rows unit length, the A matrix is unitary and thus, the inverse transform is the transpose of A : B = A’. c. The 8x8 transformation of some subimage x is simply the transform of the columns of x by A (e.g. y = Ax ), followed by the transformation of the columns of y’ by A (e.g. X = ( Ay’ ) ’ ). The net result is X = AxA’. d. The 8x8 inverse transformation of some transform block X is simply the transform of the columns of x by A’ (e.g. y = A’X ), followed by the transformation of the columns of y’ by A’ (e.g. x = ( A’y’ ) ’ ). The net result is x = A’XA.

e. See plots and code. This transform can be implemented in a similar manner to transforms used in Homework #9. We could also get the transform coefficients directly from the output of fft or fft2 and scale those appropriately. For larger blocks, this latter approach may be desirable for computational reasons O ( N log N ) vs. O ( N^2 ).

Matlab code: % solution to final, problem 1 im = [1:8]'*[5:12];

% build transform matrix N = 8; nn=[0:N-1]; rftmat = zeros([N N]); rftmat(1,:) = ones([1 N]); for lp = 1:N/2- rftmat(lp2,:) = sqrt(2)cos(2pilpnn/N); rftmat(lp2+1,:) = -sqrt(2)sin(2pilpnn/N); end rftmat(N,:) = cos(2pinn/2); rftmat = rftmat./sqrt(N);

% inverse is rftmat' - this makes it a unitary (energy preserving) transform

% 2DFT coeffs = zeros([8 8]); iminv = zeros([8 8]); % can do like solutions to HW#9 or compactly like this coeffs = rftmatimrftmat'; coeffs = real(coeffs);

% 2D IDFT iminv = rftmat'coeffsrftmat;

% check energy conservation and invertability eim = sum(im(:).^2); ecoeffs = sum(coeffs(:).^2); mse = mean((im(:)-iminv(:)).^2); subplot(221); imagesc(im); colormap(gray); colorbar; title(['original image, energy = ' num2str(eim)]); subplot(222); imagesc(coeffs); colormap(gray); colorbar; title(['transform coeffs, energy = ' num2str(ecoeffs)]); subplot(223); imagesc(iminv); colormap(gray); colorbar; title(['inverted image, MSE = ' num2str(mse)]);

c. See code and figures:

d. The edge artifacts result from the fact that the blur happen by linear convolution and the deblurring used circular convolution. Clearly, the circularity assumption was not real good. In our discussion of convolution, we discussed numerous ways to treat edge pixels. Zero padding will result in a discontinuity at the edge that is not desirable. Replication of the edge pixel is not desirable because it doesn’t replicate the blurring character, but would be better then zero padding or circular convolution. The best solution is the replicate by mirroring around the edge. This only needs to be done in x.

e. The clipped/mirrored extension has the lowest MSE, but method 2 with mirrored extensions is better visually (less ringing off of the edges). f. The non-extended deblurring kernels were dominated by edge artifacts and the MSE was not affected in a reliable way by the change in model parameters. For the extended images, the MSE increased with change in model parameters. For the clipped/extended

method 4. 5

dW

d MSE and method 2/ext. 2

dW

d MSE for a unit change in W. Thus,

method 2 is more robust to model errors. One can also plot MSE vs. W for these cases and also see that method 2 is more robust.

Matlab code: load finalimage2; % blurho is blurred image of house % refim is to be used only for calculation of MSEs and comparison

bhwin = blurho.(ones([480 1])hanning(512)'); spec = sum(abs(fft(bhwin,[],2)),1); figure(1) subplot(211); semilogy(spec); title('Average spatial spectrum') axis([115 165 1e2 1e4]); grid subplot(212); plot(blurho(125,:)); title('Plot line 125') axis([295 335 50 250]); grid

% find spectral response of blur W = 35; [sx sy] = size(blurho); [xx yy] = ndgrid([-sx/2:sx/2-1]./sx, [-sy/2:sy/2-1]./sy); B = sinc(W.*yy);

gam = 3; msk = 1./abs(B) < gam; H1 = 1./B .(msk + (1-msk).abs(B).gam); a = 0.03; H2 = (B(sx/2+1,sy/2+1)^2 + a)/(B(sx/2+1,sy/2+1)^2)B./(B.^2+a);

HB = fftshift(fft2(blurho));

imres1 = real(ifft2(fftshift(HB.H1))); imres2 = real(ifft2(fftshift(HB.H2)));

mseblur = mean((refim(:) - blurho(:)).^2); msere1 = mean(mean((imres1-refim).^2)); msere2 = mean(mean((imres2-refim).^2));

figure(2) subplot(221); imagesc(refim,[0 255]); colormap(gray); title('Reference Image'); subplot(222); imagesc(blurho,[0 255]); colormap(gray); title(['Blurred Image, MSE = ' int2str(mseblur)]); subplot(223); imagesc(imres1,[0 255]); colormap(gray); title(['Deblurred - Clipped, MSE = ' int2str(msere1)]); subplot(224); imagesc(imres2,[0 255]); colormap(gray); title(['Deblurred - Method 2, MSE = ' int2str(msere2)]);

% extend by mirrorred reflection (do in x only) blurhoext = [blurho(:,256:-1:1) blurho blurho(:,512:-1:257)];

[sx sy] = size(blurhoext); [xx yy] = ndgrid([-sx/2:sx/2-1]./sx, [-sy/2:sy/2-1]./sy); B = sinc(W.*yy);

gam = 3; msk = 1./abs(B) < gam; H1 = 1./B .(msk + (1-msk).abs(B).gam); a = 0.03; H2 = (B(sx/2+1,sy/2+1)^2 + a)/(B(sx/2+1,sy/2+1)^2)B./(B.^2+a);

HB = fftshift(fft2(blurhoext));

imres1ex = real(ifft2(fftshift(HB.H1))); imres2ex = real(ifft2(fftshift(HB.H2))); imres1ex = imres1ex(:,257:768); imres2ex = imres2ex(:,257:768);

msere1ex = mean(mean((imres1ex-refim).^2)); msere2ex = mean(mean((imres2ex-refim).^2));

figure(3); clf subplot(221); imagesc(imres1ex,[0 255]); colormap(gray); title(['Deblurred - Clipped/Ext, MSE = ' int2str(msere1ex)]); subplot(222); imagesc(imres2ex,[0 255]); colormap(gray); title(['Deblurred - Method 2/Ext, MSE = ' int2str(msere2ex)]);

g. The entropy was H = 0.3345, so our coding scheme did not come very close to the optimal. There are lots of ways of getting closer to the entropy. First, we could concatenate codes (like Shannon’s 1st^ Theorem) and derived a new codebook. Seeing the spatial correlations of ILH in the figure below (Shannon’s result assumes independence), one logically thinks of using alternate quantization techniques like VQ or coding schemes that incorporate run length coding for efficient coding of the long runs of zeros. h. The average bit rate here was (5 + 1.06 + 1.06 + 0)/4 = 1.78 bits/pixel (our target was 2 bits/pixel). The distortion was comparable to VQ encoded with 2 bits/pixel ( Dsubband = 26.8 vs. DVQ = 26.4).

Matlab code: % solution problem 4 load ../hw7image; % forward transform subimLL = (ho(1:2:end,1:2:end) + ho(2:2:end,1:2:end) + ho(1:2:end,2:2:end) + ho(2:2:end,2:2:end))/2; subimHL = (ho(1:2:end,1:2:end) - ho(2:2:end,1:2:end) + ho(1:2:end,2:2:end) - ho(2:2:end,2:2:end))/2; subimLH = (ho(1:2:end,1:2:end) + ho(2:2:end,1:2:end) - ho(1:2:end,2:2:end) - ho(2:2:end,2:2:end))/2; subimHH = (ho(1:2:end,1:2:end) - ho(2:2:end,1:2:end) - ho(1:2:end,2:2:end) + ho(2:2:end,2:2:end))/2; im2 = [subimLL subimLH; subimHL subimHH]; figure(1); imagesc(im2); colormap gray; title('Subband Coding') eorig = sum(ho(:).^2) etrans = sum(im2(:).^2) % inverse transform newho = zeros(size(ho)); newho(1:2:end,1:2:end) = (subimLL + subimHL + subimLH + subimHH)/2; newho(2:2:end,1:2:end) = (subimLL - subimHL + subimLH - subimHH)/2; newho(1:2:end,2:2:end) = (subimLL + subimHL - subimLH - subimHH)/2;

newho(2:2:end,2:2:end) = (subimLL - subimHL - subimLH + subimHH)/2; inverror = mean((ho(:)-newho(:)).^2) % variacens and bit rates vll = var(subimLL(:)); vhl = var(subimHL(:)); vlh = var(subimLH(:)); vhh = var(subimHH(:)); vm = (vllvhlvlhvhh).^(1/4); bll = (2 + 0.5log2(vll/vm)) bhl = (2 + 0.5log2(vhl/vm)) blh = (2 + 0.5log2(vlh/vm)) bhh = (2 + 0.5log2(vhh/vm)) % quantize HH and LL subimHH = zeros(size(subimHH)); L = 2^5; mn = min(subimLL(:)); mx = max(subimLL(:)); delt = (mx-mn)/L; subimLL = round((subimLL - mn + delt/2)./delt).delt - delt/2 + mn;

% find best a dat = [subimHL(:); subimLH(:)]; mm = max(abs(dat)); a = 1; astep = 0.1; qtest = zeros(size(dat)); qtest(find(dat > a/2)) = a; qtest(find(dat < -a/2)) = -a; mse = mean((dat - qtest).^2); mseold = mse+1; while (mseold > mse) % loop through values mseold = mse; a = a+astep; qtest = zeros(size(dat)); qtest(find(dat > a/2)) = a; qtest(find(dat < -a/2)) = -a; mse = mean((dat - qtest).^2); end a = a-astep qtest = zeros(size(dat)); qtest(find(dat > a/2)) = a; qtest(find(dat < -a/2)) = -a; % probabilities, average bit rate, entropy pposa = sum(qtest > 0)/length(qtest) pnega = sum(qtest < 0)/length(qtest) pzero = sum(qtest == 0)/length(qtest) bbar = pposa2 + pnega2 + pzero h = -(pposalog(pposa) + pnegalog2(pnega) + pzero*log2(pzero)) % quantize HL and LH subimHL = reshape(qtest(1:end/2),size(subimHL)); subimLH = reshape(qtest(end/2+1:end),size(subimHL)); % inverse transform newho = zeros(size(ho)); newho(1:2:end,1:2:end) = (subimLL + subimHL + subimLH + subimHH)/2; newho(2:2:end,1:2:end) = (subimLL - subimHL + subimLH - subimHH)/2; newho(1:2:end,2:2:end) = (subimLL + subimHL - subimLH - subimHH)/2; newho(2:2:end,2:2:end) = (subimLL - subimHL - subimLH + subimHH)/2; %distortion D = mean((ho(:)-newho(:)).^2) figure(2); subplot(221); imagesc(ho); colormap(gray); colorbar; title('Original Image'); subplot(222); imagesc(newho); colormap(gray); colorbar; title(['Quantized Image, D = ' num2str(D)]); subplot(223); imagesc(ho-newho); colormap(gray); colorbar; title(['Error Image, D = ' num2str(D)]);