ima = imread('flowers.tif');
ima = rgb2gray(ima);
ima = double(ima); 
ima = ima /max(max(ima)); 
sx = size(ima,1); 
sy = size(ima,2); 
buf = ima(1:2:sx,1:2:sy); 
buf1 = zeros(sx,sy); 
ima = buf1; 
ima(sx/2 - sx/4:sx/2+sx/4-1,sy/2-sy/4:sy/2+sy/4-1) = buf; 
figure(1); 
imagesc(ima); 
colormap(gray); 
pause; % Now blur image
h = ones(7,7); 
ima1 = filter2(h,ima); 
figure(2); 
colormap(gray); 
imagesc(ima1); 
H = fft2(h,size(ima1,1),size(ima1,2)); 
disp('Now inverse filtering the inage'); 
% inverse filter 
Filter = (abs(H) > 0.0001) .* (1./H); 
ima_out = ifft2(fft2(ima1).*Filter); 
figure(3); 
imagesc(real(ima_out)); 
drawnow;
pause;
disp('How sensitive is that to noise?');
ima2 = imnoise(ima1,'gaussian',0,0.05);
figure(2);
imagesc(ima2);
Filter = (abs(H) > 10) .* (1./H); 
%Filter = H./abs(H);
ima_out = ifft2(fft2(ima2).*Filter); 
figure(3); 
imagesc(real(ima_out)); 
drawnow;
