Answer To: 7060 Image Sensors & Processing / 4061 Image Processing Assignment 2 August 2018 (Total Marks: 12%)...
Abr Writing answered on Sep 11 2020
A2 code/BBC_test_card_F.tif
A2 code/BBC_test_card_F_barrel.tif
A2 code/BBC_test_card_F_pincushion.tif
A2 code/butterworth.m
% butterworth_2d - a simple function for generating the weights array for a
% 2d butterwoth filter
%
% width,height - size of filter to construct
% n - filter order 1,2 etc
% cut - cutoff value
% x0,y0 - shift offset of filter away from centre (def. (0,0))
%
% B - resulting filter weights (assumes coefficients are fftshifted)
function B = butterworth(width,height,n,cut,x0,y0)
if (nargin<6); y0=0; end
if (nargin<5); x0=0; end
if (nargin<4); error('butterworth function needs at least 4 arguments'); end
% -- insert your code below --
% HINT: meshgrid() might be useful here. Note that after FFT shift
% the zero frequency term (DC) is located at the image centre plus one
% row/column (eg. pixel (201,201) if the image was 400x400)
%B = rand(height,width); % <-- delete this dummy line
[x,y]=meshgrid(linspace(-width/2,width/2,width),linspace(-height/2,height/2,height));
B=1./(1+((sqrt((x-x0).^2+(y-y0).^2))./cut).^(2*n));
% -- insert your code above --
return
A2 code/fft_filter.m
function [Ifilt,B] = fft_filter(img,B)
if (size(img,1)~=size(B,1))||(size(img,2)~=size(B,2))
error('image and fft filter are of different sizes');
end
% -- (INSERT YOUR OWN CODE BELOW (DELETE THE DUMMY CODE) --
%Ifilt = rand(size(img,1),size(img,2)); B=Ifilt; % DELETE ME
F = (fft2((img)));
Ifilt=ifft2((F));
%F = fft2(img);
% -- INSERT YOUR OWN CODE ABOVE --
return
end
% END OF FILE
A2 code/fft_test.m
k=0.01;
I=double(imread('pout.tif'))/255;
Inoisy = imnoise(I,'gaussian',0,k);
% butterworth test
B = butterworth(size(Inoisy,2),size(Inoisy,1),1,32);
Ib = fft_filter( Inoisy , B );
% wiener test
[Iw,Fw] = wiener_denoise_filter(I,k,Inoisy);
figure
colormap(gray);
subplot(2,4,1);
imagesc(I); title('I original');
subplot(2,4,5);
imagesc(Inoisy); title('I noisy');
subplot(2,4,2);
imagesc( log(abs(fftshift(fft2(I)))) ); title('FFT original');
subplot(2,4,6);
imagesc( log(abs(fftshift(fft2(Inoisy)))) ); title('FFT noisy');
subplot(2,4,3);
imagesc( log(abs(B)) ); title('Butterworth filter (n=1,fcut=32)');
subplot(2,4,4);
imagesc(Ib); title('Butterworth filtered (n=1,fcut=32)');
subplot(2,4,7);
imagesc( log(abs(fftshift(Fw))) ); title('FFT Wiener filter');
subplot(2,4,8);
imagesc(Iw); title('Wiener noise filtered k=0.01');
drawnow;
A2 code/iminterp.m
function Ifixed = iminterp(I,xd,yd,interpmethod)
% Inputs:
% I - original image (either NxM or NxMx3
% xd,yd - arrays defining mapping of new values BACK to input image
% coordinates s.t. Ifixed(i,j) <-- I(yd(i,j),xd(i,j)). Note xd and yd
% may be a different size to I.
% interpmethod - interpolation method ('nearest','bilinear','bicubic')
% Outputs:
% Ifixed - reconstructed image based on xd,yd mappings and interpolation
% the output is NxM or NxMx3 depending on the input image.
%
% NOTES: The value of each pixel is based on examining xd and yd to
% determine which location in the original image data relates to this
% pixel. As the reference point in the original image is likely to be
% a non-integer value, the stated interpolation method is used to
% estimate the value bsed on the local neighbouring values.
%
% convert uint8 imagery to doubles in range 0..1
if (isa(I,'uint8'))
I=double(I)/255;
end
if (nargin<4)
interpmethod='bilinear';
end
% size of output image
outrows=size(xd,1);
outcols=size(yd,2);
% size of input image I
inrows=size(I,1);
incols=size(I,2);
layers=size(I,3);
%
Ifixed=zeros(outrows,outcols,layers);
for lay=1:layers % for each colour layer
for row=1:outrows
for col=1:outcols
switch( interpmethod)
case 'nearest'
% use nearest pixel in I to required location
xd_rc=round(xd(row,col));
yd_rc=round(yd(row,col));
% if inside image then...
if (xd_rc>0)&&(xd_rc<=incols)&&(yd_rc>0)&&(yd_rc<=inrows)
Ifixed(row,col,lay)=I(yd_rc,xd_rc,lay);
end
case 'bilinear'
% bi-linear interpolation
% Note: Use floor() not round()
% ---- INSERT YOUR 'BILINEAR' SOLUTION BELOW ----
% use nearest pixel in I to required location
xd_rc_b=floor(xd(row,col));
yd_rc_b=floor(yd(row,col));
a=xd(row,col)-xd_rc_b;
b=yd(row,col)-yd_rc_b;
% if inside image then...
if (xd_rc_b>0)&&(xd_rc_b<=incols-1)&&(yd_rc_b>0)&&(yd_rc_b<=inrows-1)
Ifixed(row,col,lay)=(1-a)*(1-b)*I(yd_rc_b,xd_rc_b,lay)+(1-a)*(b)*I(yd_rc_b+1,xd_rc_b,lay)+(a)*(1-b)*I(yd_rc_b,xd_rc_b+1,lay)+(a)*(b)*I(yd_rc_b+1,xd_rc_b+1,lay);
end
%Ifixed(row,col,lay)= rand(1,1); % DUMMY CODE - REMOVE ME
% ---- INSERT YOUR 'BILINEAR' SOLUTION ABOVE ----
case 'bicubic'
% bicubic (see assignment sheet for details)
% Use NEAREST for locations where there is no 4x4 neighbourhood
% Fill in and Use the function bicubic() to simplify your code
% You may need to ensure the bicubic estimate lies within 0..1
% ---- INSERT YOUR 'BICUBIC' SOLUTION BELOW ----
xd_rc_b=floor(xd(row,col));
yd_rc_b=floor(yd(row,col));
ta=xd(row,col)-xd_rc_b;
tb=yd(row,col)-yd_rc_b;
% if inside image then...
if (xd_rc_b>1)&&(xd_rc_b<=incols-2)&&(yd_rc_b>1)&&(yd_rc_b<=inrows-2)
A=I(yd_rc_b-1:yd_rc_b+2,xd_rc_b-1:xd_rc_b+2,lay);
Ifixed(row,col,lay)=bicubic(ta,tb,A);
else
if (xd_rc_b>0)&&(xd_rc_b<=incols)&&(yd_rc_b>0)&&(yd_rc_b<=inrows)
Ifixed(row,col,lay)=I(yd_rc_b,xd_rc_b,lay);
end
end
%Ifixed(row,col,lay)= rand(1,1); % DUMMY CODE - REMOVE ME
% ---- INSERT...