1

I have to use an inverse filter to remove the blurring from this image

Blurred Image.

Unfortunately, I have to figure out the transfer function H of the imaging system used to get these sharper images, It should be Gaussian. So, I should determine the approximate width of the Gaussian by trying different Gaussian widths in an inverse filter and judging which resulting images look the “best”.

The best result will be optimally sharp – i.e., edges will look sharp but will not have visible ringing.

I tried by using 3 approaches:

  1. I created a transfer function with N dimensions (odd number, for simplicity), by creating a grid of N dimensions, and then applying the Gaussian function to this grid. After that, we add zeroes to this transfer function in order to get the same size as the original image. However, after applying the filter to the original image, I just see noise (too many artifacts).
  2. I created the transfer function with size as high as the original image, by creating a grid of the same size as the original image. If sigma is too small, then the PSF FFT magnitude is wide. Otherwise it gets thinner. If sigma is small, then the image is even more blurred, but if we set a very high sigma value then we get the same image (not better at all).
  3. I used the fspecial function, playing with sizes of sigma and h. But still I do not get anything sharper than the original blurred image.

Any ideas?

Here is the code used for creating the transfer function in Approach 1:

%Create Gaussian Filter
function h = transfer_function(N, sigma, I) %N is the dimension of the kernel
%create a 2D-grid that is the same size as the Gaussian filter matrix
grid = -floor(N/2) : floor(N/2);
[x, y] = meshgrid(grid, grid);
arg = -(x.*x + y.*y)/(2*sigma*sigma);
h = exp(arg); %gaussian 2D-function
kernel = h/sum(h(:)); %Normalize so that total weight equals 1

[rows,cols] = size(I);
add_zeros_w = (rows - N)/2;
add_zeros_h = (cols - N)/2;

h = padarray(kernel,[add_zeros_w  add_zeros_h],0,'both'); % h = kernel_final_matrix

end 

And this is the code for every approach:

I = imread('lena_blur.jpg');
I1 = rgb2gray(I);
figure(1),
I1 = double(I1);
%---------------Approach 1
% N = 5; %Dimension Assume is an odd number
% sigma = 20; %The bigger number, the thinner the PSF in FREQ
% H = transfer_function(N, sigma, I1);
%I1=I1(2:end,2:end); %To simplify operations
imagesc(I1); colormap('gray'); title('Original Blurred Image')

I_fft = fftshift(fft2(I1)); %Shift the image in Fourier domain to let its DC part in the center of the image



% %FILTER-----------Approach 2---------------
% N = 5; %Dimension Assume is an odd number
% sigma = 20; %The bigger number, the thinner the PSF in FREQ
% 
% 
% [x,y] = meshgrid(-size(I,2)/2:size(I,2)/2-1, -size(I,1)/2:size(I,1)/2-1);
% H = exp(-(x.^2+y.^2)*sigma/2);
% %// Normalize so that total area (sum of all weights) is 1
% H = H /sum(H(:));
% 
% %Avoid zero freqs
% for i = 1:size(I,2) %Cols
%     for j = 1:size(I,1) %Rows
%         if (H(i,j) == 0)
%             H(i,j) = 1e-8;
%         end
%     end
% end
% 
% [rows columns z] = size(I);
% G_filter_fft = fft2(H,rows,columns);
%FILTER---------------------------------


%Filter--------- Aproach 3------------
N = 21; %Dimension Assume is an odd number
sigma = 1.25; %The bigger number, the thinner the PSF in FREQ

H = fspecial('gaussian',N,sigma)
[rows columns z] = size(I);
G_filter_fft = fft2(H,rows,columns);

%Filter--------- Aproach 3------------



%DISPLAY FFT PSF MAGNITUDE
figure(2),
imshow(fftshift(abs(G_filter_fft)),[]); title('FFT PSF magnitude 2D');


% Yest = Y_blurred/Gaussian_Filter
I_restoration_fft = I_fft./G_filter_fft;
I_restoration = (ifft2(I_restoration_fft));
I_restoration = abs(I_restoration);




I_fft = abs(I_fft);

% Display of Frequency domain (To compare with the slides) 
figure(3),
subplot(1,3,1); 
imagesc(I_fft);colormap('gray');title('|DFT Blurred Image|')
subplot(1,3,2)
imshow(log(fftshift(abs(G_filter_fft))+1),[]) ;title('| Log DFT Point Spread Function + 1|');
subplot(1,3,3)
imagesc(abs(I_restoration_fft));colormap('gray'); title('|DFT Deblurred|')
% imshow(log(I_restoration+1),[])

%Display PSF FFT in 3D
figure(4)
hf_abs = abs(G_filter_fft);
%270x270
surf([-134:135]/135,[-134:135]/135,fftshift(hf_abs));
% surf([-134:134]/134,[-134:134]/134,fftshift(hf_abs));
shading interp, camlight, colormap jet
xlabel('PSF FFT magnitude')

%Display Result (it should be the de-blurred image)
figure(5),
%imshow(fftshift(I_restoration));
imagesc(I_restoration);colormap('gray'); title('Deblurred Image')

%Pseudo Inverse restoration
% cam_pinv = real(ifft2((abs(G_filter_fft) > 0.1).*I_fft./G_filter_fft));
% imshow(fftshift(cam_pinv));
% xlabel('pseudo-inverse restoration')
Javiss
  • 765
  • 3
  • 10
  • 24
  • There are solutions in Matlab webpage : http://blogs.mathworks.com/steve/2007/08/13/image-deblurring-introduction/ , have you take a look at these?? – Konstantinos Monachopoulos Mar 09 '17 at 13:10
  • It may also be interesting to look into [deconwnr](https://nl.mathworks.com/help/images/ref/deconvwnr.html) – m7913d Mar 09 '17 at 15:27
  • @monakons yes, actually my solutions is partly based on that blog. The thing is that I do not have noise at all. The image is just blurred (not additive noise). – Javiss Mar 09 '17 at 15:50
  • @m7913d I do not have any noise... – Javiss Mar 09 '17 at 15:51
  • You can use it for a noise free image, just put NSR to zero. Note that you always have quantisation noise. – m7913d Mar 09 '17 at 16:26
  • Still, not having any useful outcomes. I = imread('lena_blur.jpg'); N = 19; %Dimension Assume is an odd number sigma =.75; %The bigger number, the thinner the PSF in FREQ H = fspecial('gaussian',N,sigma) estimated_nsr = 0; wnr3 = deconvwnr(I, H, estimated_nsr); figure, imshow(wnr3) title('Restoration of Blurred, Noisy Image Using Estimated NSR'); – Javiss Mar 09 '17 at 17:16

1 Answers1

0

A possible solution is deconvwr. I will first show its performance starting from an undistorted lena image. So, I know exactly the gaussian blurring function. Note that setting estimated_nsr to zero will destroy the performance completely due to quantisation noise.

 I_ori = imread('lenaTest3.jpg'); % Download an original undistorted lena file
 N = 19;
 sigma = 5;
 H = fspecial('gaussian',N,sigma) 
 estimated_nsr = 0.05;

 I = imfilter(I_ori, H)

 wnr3 = deconvwnr(I, H, estimated_nsr); 
 figure
 subplot(1, 4, 1);
 imshow(I_ori) 
 subplot(1, 4, 2);
 imshow(I) 
 subplot(1, 4, 3);
 imshow(wnr3) 
 title('Restoration of Blurred, Noisy Image Using Estimated NSR'); 
 subplot(1, 4, 4);
 imshow(H, []);

The best parameters I found for your problem by trial and error.

 N = 19; 
 sigma = 2; 
 H = fspecial('gaussian',N,sigma) 
 estimated_nsr = 0.05;

EDIT: calculating exactly the used blurring filter

If you download an undistorted lena (I_original_fft), you can calculate the used blurring filter as follows:

G_filter_fft = I_fft./I_original_fft
m7913d
  • 10,244
  • 7
  • 28
  • 56
  • Thanks for your answer. But I need to work with this exact image I posted in the first image. As I wrote: I should determine the approximate width of the Gaussian by trying different Gaussian widths in an INVERSE filter and judging which resulting images look the “best”. This means that if I write: I_restoration_fft = I_fft./G_filter_fft; Then the Inverse FFT should be the Image I want. I achieve this by finding the right parameters of the Gaussian. deconwnr is not useful (I have not been asked to work on the noise or use a wiener filter) – Javiss Mar 09 '17 at 22:56
  • As I showed in the example above. it is a good idea to assume some noise on the image. If you run the example above, you obtain very bad result if you set `estimated_nsr` to zero, even if the gaussian blurring filter is exactly known. If I looked correctly to your code, you are basically implementing `deconwnr` with zero noise. My second part of code are the parameters which sharpen your image slightly. – m7913d Mar 10 '17 at 07:57
  • Any way, if you use: I = imfilter(I_ori, H), you are making a convolution in spatial domain. and what I should do is a point-wise division in the frequency domain i.e., I_restoration_fft = I_fft./G_filter_fft, which is not the same as a convolution un spatial domain. – Javiss Mar 10 '17 at 08:31
  • Note that I used imfilter to blur an undistorted lena image, so I know the exact blurring function and I'm able to test `deconvwnr`. I use `deconvwnr` to restore the image, without having to write a bunch of code myself. Secondly, note that you said your method is based on [this post](http://blogs.mathworks.com/steve/2007/08/13/image-deblurring-introduction/), which is very similar to the Wiener method as discussed in the comments of that post. – m7913d Mar 10 '17 at 09:19
  • Oh Ok. Well, you are probably getting a nice result since you already know the gaussian (when you blur the image) and you de-blur the blurred image using the same gaussian. The point here, is to GUESS the parameters of the gaussian, since you are given a blurred image (which has been blurred with a gaussian with particular parameters; same parameters you have to guess). So, be my guest and try deconwnr with MY IMAGE, the same I posted in the first post. you will see that deconwnr do not give any nice result. – Javiss Mar 10 '17 at 10:01
  • That's exactly what I tried with my second block of code. It will make the face somewhat sharper, but the edges of the image are distorted. I included the first block of code to show you the maximum performance of this method. – m7913d Mar 10 '17 at 10:08
  • @Javiss I added a method to calculate exactly the blurring filter. – m7913d Mar 10 '17 at 10:26
  • I think this is not what I am looking for. What you are doing is blurring the already blurred Lena (my original picture). What I want is to make my original picture sharper. What you get eventually is a sharper image compared with the blurred version of my blurred original picture (So it is like twice blurred). However, if you compare with my original blurred lena, your final outcome is not sharper at all, it is even more blur. – Javiss Mar 10 '17 at 12:42
  • This last block of code is really useful, It did not come up in my mind to use that. But, still... I need to discuss the values of sigma and hsize, which you do not know (You just get the PSF already built). – Javiss Mar 10 '17 at 12:44
  • With original image, I meant an undistorted version of lena, as you find easily online. For the rest of your remarks, I will leave it to someone else to answer your questions, who will hopefully give you an answer which match better what you want. – m7913d Mar 10 '17 at 18:26