1. 程式人生 > >第五章 影象復原

第五章 影象復原

%% Deblurring Images Using the Blind Deconvolution Algorithm 
%%盲反捲積演算法復原影象
% The Blind Deconvolution Algorithm can be used effectively when no
% information about the distortion (blurring and noise) is known. The
% algorithm restores the image and the point-spread function (PSF)
% simultaneously. The accelerated, damped Richardson-Lucy algorithm is used
% in each iteration. Additional optical system (e.g. camera)
% characteristics can be used as input parameters that could help to
% improve the quality of the image restoration. PSF constraints can be
% passed in through a user-specified function
%在不知道影象失真資訊(模糊和噪聲)資訊情況下,盲反捲積演算法可以有效地加以利用。該演算法
%對影象和點擴充套件函式(PSF)的同時進行復原。每次迭代都使用加速收斂Richardson-Lucy 
%演算法。額外的光學系統(如照相機)的特性可作為輸入引數,幫助改善影象復原質量。可以通
%過使用者指定的函式對PSF進行限制
% Copyright 2004-2005 The MathWorks, Inc.
 
%% Step 1: Read Image
%%第一步:讀取影象
% The example reads in an intensity image. The |deconvblind| function can
% handle arrays of any dimension.
%該示例讀取一個灰度影象。| deconvblind |函式可以處理任何維陣列。
I = imread('lena.jpg');
I=rgb2gray(I);
figure;imshow(I);title('Original Image');
%text(size(I,2),size(I,1)+15, ...
%    'Image courtesy of Massachusetts Institute of Technology', ...
%'FontSize',7,'HorizontalAlignment','right');  
   
 
 
%% Step 2: Simulate a Blur
%%第二步:模擬一個模糊
% Simulate a real-life image that could be blurred (e.g., due to camera
% motion or lack of focus). The example simulates the blur by convolving a
% Gaussian filter with the true image (using |imfilter|). The Gaussian filter
% then represents a point-spread function, |PSF|.
 %模擬一個現實中存在的模糊影象(例如,由於相機抖動或對焦不足)。這個例子通過對真實
%影象進行高斯濾波器模擬影象模糊(使用|imfilter|)。高斯濾波器是一個點擴充套件函式,
%|PSF|。
PSF=fspecial('gaussian',7,10);
Blurred=imfilter(I,PSF,'symmetric','conv');  %對影象I進行濾波處理;
figure;imshow(Blurred);title('Blurred Image');  
 
   
 
 
%% Step 3: Restore the Blurred Image Using PSFs of Various Sizes
%%第三步:使用不同的點擴充套件函式復原模糊影象
% To illustrate the importance of knowing the size of the true PSF, this
% example performs three restorations. Each time the PSF reconstruction
% starts from a uniform array--an array of ones.
%為了說明知道真實PSF的大小的重要性,這個例子執行三個修復。PSF函式重建每次都是從統一
%的全一陣列開始。
%%
% The first restoration, |J1| and |P1|, uses an undersized array, |UNDERPSF|, for
% an initial guess of the PSF. The size of the UNDERPSF array is 4 pixels
% shorter in each dimension than the true PSF. 
%第一次復原,|J1|和|P1|,使用一個較小陣列,| UNDERPSF |,來對PSF的初步猜測。該
%UNDERPSF陣列每維比真實PSF少4個元素。
UNDERPSF = ones(size(PSF)-4);
[J1 P1] = deconvblind(Blurred,UNDERPSF);
figure;imshow(J1);title('Deblurring with Undersized PSF'); 
 
   
 
%%
% The second restoration, |J2| and |P2|, uses an array of ones, |OVERPSF|, for an
% initial PSF that is 4 pixels longer in each dimension than the true PSF.
%第二次復原,|J2|和|P2|,使用一個元素全為1的陣列,| OVERPSF|,初始PSF每維比真
%實PSF多4個元素。
OVERPSF = padarray(UNDERPSF,[4 4],'replicate','both');
[J2 P2] = deconvblind(Blurred,OVERPSF);
figure;imshow(J2);title('Deblurring with Oversized PSF');  
   
 
 
%%
% The third restoration, |J3| and |P3|, uses an array of ones, |INITPSF|, for an
% initial PSF that is exactly of the same size as the true PSF.
%第三次復原,|J3|和|P3|,使用一個全為一的陣列| INITPSF |作為初次PSF,每維與真正
%的PSF相同。
INITPSF = padarray(UNDERPSF,[2 2],'replicate','both');
[J3 P3] = deconvblind(Blurred,INITPSF);
figure;imshow(J3);title('Deblurring with INITPSF');  
 
   
 
 
%% Step 4: Analyzing the Restored PSF
%%第四步:分析復原函式PSF
% All three restorations also produce a PSF. The following pictures show
% how the analysis of the reconstructed PSF might help in guessing the
% right size for the initial PSF. In the true PSF, a Gaussian filter, the
% maximum values are at the center (white) and diminish at the borders (black).
%所有這三個復原也產生PSF。以下圖片顯示對PSF重建分析的如何可能有助於猜測最初PSF的大
%小。在真正的PSF中,高斯濾波器的最高值在中心(白),到邊界消失(黑)。
figure;
subplot(221);imshow(PSF,[],'InitialMagnification','fit');
title('True PSF');
subplot(222);imshow(P1,[],'InitialMagnification','fit');
title('Reconstructed Undersized PSF');
subplot(223);imshow(P2,[],'InitialMagnification','fit');
title('Reconstructed Oversized PSF');
subplot(224);imshow(P3,[],'InitialMagnification','fit');
title('Reconstructed true PSF');  
 
   
 
 
%% 
% The PSF reconstructed in the first restoration, |P1|, obviously does not
% fit into the constrained size. It has a strong signal variation at the
% borders. The corresponding image, |J1|, does not show any improved clarity
% vs. the blurred image,.
 %第一次復原的PSF,|P1|,顯然不適合大小的限制。它在邊界有一個強烈的變化訊號。
%相應的圖片|J1|,與模糊影象|Blurred|比沒有表現出清晰度提高。
%%
% The PSF reconstructed in the second restoration, |P2|, becomes very smooth
% at the edges. This implies that the restoration can handle a PSF of a
% smaller size. The corresponding image, |J2|, shows some deblurring but it
% is strongly corrupted by the ringing.
 %第二次復原的PSF,|P2|,邊緣變得非常平滑。這意味著復原可以處理一個更細緻的
%PSF。相應的圖片|J2|,顯得清晰了,但被一些“振鈴”強烈破壞。
%%
% Finally, the PSF reconstructed in the third restoration, |P3|, is somewhat
% intermediate between |P1| and |P2|. The array, |P3|, resembles the true PSF
% very well. The corresponding image, |J3|, shows significant improvement;
% however it is still corrupted by the ringing.
 %最後,第三次復原的PSF,|P3|,介於|P1|和|P2|之間。該陣列|P3|,非常接近真
%正的PSF。相應的圖片,|J3|,顯示了顯著改善,但它仍然被一些“振鈴”破壞。
 
 
%% Step 5: Improving the Restoration
%%第五步:改善影象復原
% The ringing in the restored image, |J3|, occurs along the areas of sharp
% intensity contrast in the image and along the image borders. This example
% shows how to reduce the ringing effect by specifying a weighting
% function. The algorithm weights each pixel according to the |WEIGHT| array
% while restoring the image and the PSF. In our example, we start by
% finding the "sharp" pixels using the edge function. By trial and error,
% we determine that a desirable threshold level is 0.3.
%在復原影象|J3|內部灰度對比鮮明的地方和影象邊界都出現了“振鈴”。這個例子說明了如何
%通過定義一個加權函式來減少影象中的“振鈴”。該演算法是在對影象和PSF進行復原時,對每個
%像元根據|WEIGHT|陣列進行加權計算。在我們的例子,我們從用邊緣函式查詢“鮮明”像元
%開始。通過反覆試驗,我們確定理想的閾值為0.3。
 
%WEIGHT = edge(I,'sobel',.3);  
 WEIGHT = edge(Blurred,'sobel',.3); 
%%
% To widen the area, we use |imdilate| and pass in a structuring element, |se|.
%為了拓寬領域,我們使用|imdilate|並傳遞一個結構元素|se|。
se = strel('disk',2);
WEIGHT = 1-double(imdilate(WEIGHT,se));  
 
%%
% The pixels close to the borders are also assigned the value 0.
%在邊界附近畫素的值也被分配為0。
WEIGHT([1:3 end-[0:2]],:) = 0;
WEIGHT(:,[1:3 end-[0:2]]) = 0;
figure;imshow(WEIGHT);title('Weight array');  
 
   
 
 %%
% The image is restored by calling deconvblind with the |WEIGHT| array and an
% increased number of iterations (30). Almost all the ringing is suppressed.
%該影象通過|WEIGHT|陣列和增加重複次數(30)呼叫deconvblind函式來複原。幾乎所
%有的“振鈴”被抑制。
[J P] = deconvblind(Blurred,INITPSF,30,[],WEIGHT);
figure;imshow(J);title('Deblurred Image');  
   
 
 
%% Step 6: Using Additional Constraints on the PSF Restoration
%第六步:使用附加約束對PSF復原
% The example shows how you can specify additional constraints on the PSF.
%這個例子說明了如何在PSF上指定額外的限制。
% The function, |FUN|, below returns a modified PSF array which deconvblind
% uses for the next iteration. 
%函式|FUN|返還一個修改了的PSF陣列,用作deconvblind函式的下一次重複。
% In this example, |FUN| modifies the PSF by cropping it by |P1| and |P2| number
% of pixels in each dimension, and then padding the array back to its
% original size with zeros. This operation does not change the values in
% the center of the PSF, but effectively reduces the PSF size by |2*P1| and
% |2*P2| pixels. 
%在這個例子中,通過對PSF陣列各維數剪下|P1|和|P2|個值實現對PSF的修改,對陣列填充
%回零。此操作不會改變在PSF中心的值,而且有效地在各維減少了|2*P1|和| 2*P2|元
%素。
 
P1 = 2;
P2 = 2;
FUN = @(PSF) padarray(PSF(P1+1:end-P1,P2+1:end-P2),[P1 P2]);  
 
%%
% The anonymous function, |FUN|, is passed into |deconvblind| last.
%該匿名函式|FUN|,最後傳遞給| deconvblind |。
%%
% In this example, the size of the initial PSF, |OVERPSF|, is 4 pixels larger
% than the true PSF. Setting P1=2 and P2=2 as parameters in |FUN|
% effectively makes the valuable space in |OVERPSF| the same size as the true
% PSF. Therefore, the outcome, |JF| and |PF|, is similar to the result of
% deconvolution with the right sized PSF and no |FUN| call, |J| and |P|, from
% step 4.
%在這個例子中,初始PSF,|OVERPSF|,每維比真正的PSF多4個畫素,。設定P1=2和P2=2作
%為|FUN|的引數,可有效地使|OVERPSF|與真正的PSF的大小相同。因此,得到的結果|JF|
%和|PF|,與第四步不使用|FUN|而僅用正確尺寸PSF盲反捲積得到的結果|J|和|P|類似。
[JF PF] = deconvblind(Blurred,OVERPSF,30,[],WEIGHT,FUN);%OVERPSF預估psf,30迭代次數,WEIGHT用來遮蔽壞畫素,fun表示噪聲矩陣
figure;imshow(JF);title('Deblurred Image');  
 
   
 
 
%%
% If we had used the oversized initial PSF, |OVERPSF|, without the
% constraining function, |FUN|, the resulting image would be similar to the
% unsatisfactory result, |J2|, achieved in Step 3.
%
% Note, that any unspecified parameters before |FUN| can be omitted, such as
% |DAMPAR| and |READOUT| in this example, without requiring a place holder,
% ([]).
 %如果我們使用了沒有約束的函式|FUN|的較大的初始PSF,| OVERPSF |,所得影象將類
%似第3步得到的效果並不理想的|J2|。 
%注意,任何在|FUN|之前未指定引數都可以省略,如|DAMPAR|和|READOUT|在這個例子中,而不需要指示他們的位置,([])。
 
displayEndOfDemoMessage(mfilename)