在matlab中使用高斯滤波器进行图像去模糊,没有加性噪声

时间:2017-03-09 12:13:51

标签: image matlab gaussian gaussianblur

我必须使用逆滤镜来消除此图像中的模糊

Blurred Image

不幸的是,我必须弄清楚成像的传递函数H 系统用于获得这些更清晰的图像,应该是高斯图像。因此,我应该通过在逆滤波器中尝试不同的高斯宽度来确定高斯的近似宽度,并判断哪些结果图像看起来“最佳”。

最佳效果将是最佳的 - 即边缘看起来很清晰,但不会有明显的响铃。

我试过使用3种方法:

  1. 我创建了一个具有N维度的传递函数(为简单起见,奇数),通过创建N维的网格,然后将高斯函数应用于此网格。之后,我们为此传递函数添加零,以便获得与原始图像相同的大小。但是,在将滤镜应用于原始图像后,我只看到了噪音(太多的瑕疵)。
  2. 通过创建与原始图像大小相同的网格,我创建了尺寸与原始图像一样高的传递函数。如果sigma太小,则PSF FFT幅度很大。否则会变瘦。如果sigma很小,那么图像会更加模糊,但如果我们设置了非常高的sigma值,那么我们会得到相同的图像(根本不是更好)。
  3. 我使用fspecial功能,使用sigmah的尺寸。但是我仍然没有比原始模糊图像更清晰。
  4. 有什么想法吗?

    以下是方法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 
    

    这是每种方法的代码:

    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')
    

1 个答案:

答案 0 :(得分:0)

可能的解决方案是deconvwr。我将首先从未失真的lena图像开始显示其性能。所以,我确切地知道高斯模糊功能。请注意,将estimated_nsr设置为零会因量化噪声而完全破坏性能。

 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, []);

我通过反复试验找到的最佳参数。

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

编辑:准确计算使用的模糊滤镜

如果您下载未失真的lena(I_original_fft),则可以按如下方式计算使用的模糊滤镜:

G_filter_fft = I_fft./I_original_fft