For Midterm: You are allowed to bring 2 sheets, 8 1/2 by 11
with your own handwritten notes on one side.

Suggested exercises:

Basic		Advanced		Applied
2.16,17		2.18,20
3.2,3,5,8,10	3.4,6,9
4.4,7,8,10,15	4.2,3,9,10,17,		20,24
5.3,4,14	5.1,2				Note 5.5 impossible??

-----------------------------------------------------------------------

% Restoration: Remove bars from image

RGB = imread('JagFish.jpg');
image(RGB)
axis equal
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);


% Degrade, image:
Rb = double(R);
Gb = double(G);
Bb = double(B);

[mI, nI] = size(Gb);

Dist = 128*ones(mI,1)*(sin(1:nI)>0.7);
Gb = Gb + Dist;

RGB2 = RGB;
RGB2(:) = [Rb,Gb,Bb];	% Wierd trick!!
figure(1)
image(RGB2)

% Image restoration:
=======================

FRb = fft2(Rb);
FGb = fft2(Gb);
FBb = fft2(Bb);

figure(2)
image(sqrt(abs(FGb/8 )))
colormap gray

mesh(fftshift(abs(FGb )))

FGb(1,20:200) = 0;

FixedR = real(ifft2(FRb));
FixedG = real(ifft2(FGb));
FixedB = real(ifft2(FBb));

FixedRGB = uint8(zeros(mI,nI,3));
FixedRGB(:) = [FixedR,FixedG,FixedB];
figure(2)
image(FixedRGB)

imwrite(FixedRGB,'CaravRest.jpg','jpeg')  
imwrite(RGB2,'CaravDegraded.jpg','jpeg')     
 

----------------------------------------------------

% gauss2         2-D Gaussian
%
%       function A = g2(sz,s)
%
% sz : size of returned matrix, [s1 s2]: variance
 
%       M Jagersand 1993
 
 
function A = gauss2(sz,s)
 
  A = zeros(sz);
  xc = (sz(2)+1)/2;
  yc = (sz(1)+1)/2;
  r = s*2;
  for y = 1:sz(1)
    yd = (y-yc)^2/r(1);
    for x = 1:sz(2)
      cr = (x-xc)^2/r(2) + yd;
      A(y,x) = exp(-cr);
    end
  end

-----------------------------------------------------

% g2000    2-D Gaussian
%
%       function A = g2(sz,s)
%
% sz : size of returned matrix, [s1 s2]: variance
 
%       M Jagersand 2000
 
 
function A = gauss2(sz,s)

  A = zeros(sz);
  xc = (sz(2)+1)/2;   % Centerpoints
  yc = (sz(1)+1)/2;
  r = s*2;


  y = 1:sz(1);
  yd = (y-yc).^2/r(1);  % Change .^
  yM = yd'*ones(1,sz(2));

  x = 1:sz(2);
  xd = (x-xc).^2/r(2);
  xM = ones(sz(1),1)*xd;
  A = exp(-xM-yM);

------------------------------------------------------

% g3000    2-D Gaussian
%
%       function A = g2(sz,s)
%
% sz : size of returned matrix, [s1 s2]: variance
 
%       M Jagersand 2000
 
 
function A = g3000(sz,s)
  A = exp(-(((1:sz(1))' -(sz(1)+1)/2).^2/(s(1)*2))*ones(1,sz(2))...
          -ones(sz(1),1)*(((1:sz(2))-(sz(2)+1)/2)).^2/(s(2)*2));



----------------------------------------------------------

sum(sum(gauss2([20,20],[10,10])-g2000([20,20],[10,10])))

tic; gauss2([200,200],[10,10]); toc
tic; g2000([200,200],[10,10]); toc
tic; g3000([200,200],[10,10]); toc

--------------------------------------------------------------

%Restoration, un-smoothing
%1/ Find best approximation to degradation that happened
%2/ Pad or not to pad with 0's, image mean value or other.
%2/ Compute FFT of degradation
%3/ FFT real, symmetric? If not maybe force (abs( ))
%4/ Deconvolute/divide out degradation
%5/ Ifft back to get "restored" image


mask = gauss2([20,20],[10,10]);
mask = mask/sum(mask(:));
mesh(mask)

% Image restoration:

RGB = imread('CaravanYuLongShan.jpg');
image(RGB)
R = RGB(:,:,1);
G = RGB(:,:,2);
B = RGB(:,:,3);

% Degrade, blur image:
Rb = filter2(mask,R);
Gb = filter2(mask,G);
Bb = filter2(mask,B);

RGB2 = RGB;
RGB2(:) = [Rb,Gb,Bb];	% Wierd trick!!
image(RGB2)

% Image restoration:
=======================
[mI, nI] = size(Rb);
[mM, nM] = size(mask);

% Pad the image with zeros:
Rbm = zeros(mI+mM,nI+nM);
Gbm = zeros(mI+mM,nI+nM);
Bbm = zeros(mI+mM,nI+nM);

Rbm(1:mI,1:nI) = Rb;
Gbm(1:mI,1:nI) = Gb;
Bbm(1:mI,1:nI) = Bb;

% Pad the mask
mmask = zeros(mI+mM,nI+nM);
[m,n] = size(mmask);

mmask(m/2-mM/2+1:m/2+mM/2,n/2-nM/2+1:n/2+nM/2) = mask; % Stick in the middle
mmask = fftshift(mmask);			       % Shift out, why?

FRb = fft2(Rbm);
FGb = fft2(Gbm);
FBb = fft2(Bbm);

Fmask = fft2(mmask);

% Fix the image by "deconvoluting" to take away blur

d = 0.05; % To avoid F/0 % Try 0, .5, .01 .05

FixedR = real(ifft2(FRb./((Fmask)+d)));
FixedG = real(ifft2(FGb./((Fmask)+d)));
FixedB = real(ifft2(FBb./((Fmask)+d)));

FixedRGB = uint8(zeros(mI+mM,nI+nM,3));
FixedRGB(:) = [FixedR,FixedG,FixedB];
figure(2)
image(FixedRGB)

imwrite(FixedRGB,'CaravRest.jpg','jpeg')  
imwrite(RGB2,'CaravDegraded.jpg','jpeg')     
 




