數字圖像處理之頻域圖像增強
by方陽
版權聲明:本文為博主原創文章,轉載請指明轉載地址
http://www.cnblogs.com/fydeblog/p/7069942.html
1. 前言
這篇博客主要講解頻域濾波增強的各類濾波器的實現,並分析不同的濾波器截止頻率對頻域濾波增強效果的影響。理論的知識還請看書和百度,這里不再復述!
2. 原理說明
(1) 圖像的增強可以通過頻域濾波來實現,頻域低通濾波器濾除高頻噪聲,頻域高通濾波器濾除低頻噪聲。
(2) 相同類型的濾波器的截止頻率不同,對圖像的濾除效果也會不同。
3. 實現內容
(1) 選擇任意一副圖像,對其進行傅里葉變換,在頻率域中實現二階butterworth低通濾波器的平滑作用,截止頻率任意設定。顯示原始圖像和濾波圖像。
(2) 選擇任意一副圖像,對其進行傅里葉變換,在頻率域中實現兩種不同半徑(截止頻率)的高斯高通濾波的銳化效果,顯示原始圖像和濾波圖像,及與原圖像疊加的高頻增強圖像。
4. 程序實現及實驗結果
(1)butterworth濾波器
參考代碼:
I=imread('fig620.jpg'); f=D3_To_D2(I); PQ=paddedsize(size(f)); [U,V]=dftuv(PQ(1),PQ(2)); D0=0.05*PQ(2); F=fft2(f,PQ(1),PQ(2)); H=1./(1+((U.^2+V.^2)/(D0^2)).^2); g=dftfilt(f,H); figure; subplot(1,3,1); imshow(f); title('原圖'); subplot(1,3,2); imshow(fftshift(H),[]); title('濾波器頻譜'); subplot(1,3,3); imshow(g,[]); title('濾波后的圖像');
D3_To_D2函數參考代碼:
function image_out=D3_To_D2(image_in) [m,n]=size(image_in); n=n/3;%由於我的灰度圖像是185x194x3的,所以除了3,你們如果是PxQ的,就不要加了 A=zeros(m,n);%構造矩陣 for i=1:m for j=1:n A(i,j)= image_in(i,j);%填充圖像到A end end image_out=uint8(A);
paddedsize函數參考代碼:
function PQ = paddedsize(AB,CD,~ ) %PADDEDSIZE Computes padded sizes useful for FFT-based filtering. % Detailed explanation goes here if nargin == 1 PQ = 2*AB; elseif nargin ==2 && ~ischar(CD) PQ = QB +CD -1; PQ = 2*ceil(PQ/2); elseif nargin == 2 m = max(AB);%maximum dimension %Find power-of-2 at least twice m. P = 2^nextpow(2*m); PQ = [P,P]; elseif nargin == 3 m = max([AB CD]);%maximum dimension P = 2^nextpow(2*m); PQ = [P,P]; else error('Wrong number of inputs'); end
dftuv函數參考代碼:
function [ U,V ] = dftuv( M, N ) %DFTUV 實現頻域濾波器的網格函數 % Detailed explanation goes here u = 0:(M - 1); v = 0:(N - 1); idx = find(u > M/2); %找大於M/2的數據 u(idx) = u(idx) - M; %將大於M/2的數據減去M idy = find(v > N/2); v(idy) = v(idy) - N; [V, U] = meshgrid(v, u); end
運行結果
(2)高通濾波器
參考代碼:
I1=imread('lena.bmp'); f1=D3_To_D2(I1); PQ1=paddedsize(size(f1)); D0_1=0.05*PQ(1); D0_2=0.1*PQ(1); H1=hpfilter('gaussian',PQ1(1),PQ1(2),D0_1); H2=hpfilter('gaussian',PQ1(1),PQ1(2),D0_2); g1=dftfilt(f1,H1); g2=dftfilt(f1,H2); H1=0.5+2*H1; H2=0.5+2*H2; g3=dftfilt(f1,H1); g4=dftfilt(f1,H2); g3=histeq(gscale(g3),256); g4=histeq(gscale(g4),256); figure; subplot(2,3,1); imshow(f1); title('原圖'); subplot(2,3,2); imshow(g1,[]); title('濾波后的圖像-系數0.05'); subplot(2,3,3); imshow(g2,[]); title('濾波后的圖像-系數0.1'); subplot(2,3,4); imshow(g3,[]); title('增強后的圖像-系數0.05'); subplot(2,3,5); imshow(g4,[]); title('增強后的圖像-系數0.1');
hpfilter函數參考代碼:
function H = hpfilter(type, M, N, D0, n) if nargin == 4 n = 1; end hlp = lpfilter(type, M, N, D0, n); H = 1 - hlp;
hpfilter中的lpfilter參考代碼:
function [ H, D ] = lpfilter( type,M,N,D0,n ) %LPFILTER creates the transfer function of a lowpass filter. % Detailed explanation goes here %use function dftuv to set up the meshgrid arrays needed for computing %the required distances. [U, V] = dftuv(M,N); %compute the distances D(U,V) D = sqrt(U.^2 + V.^2); %begin filter computations switch type case 'ideal' H = double(D <= D0); case 'btw' if nargin == 4 n = 1; end H = 1./(1+(D./D0).^(2*n)); case 'gaussian' H = exp(-(D.^2)./(2*(D0^2))); otherwise error('Unkown filter type'); end
gscale函數參考代碼:
function g = gscale(f, varargin) %GSCALE Scales the intensity of the input image. % G = GSCALE(F, 'full8') scales the intensities of F to the full % 8-bit intensity range [0, 255]. This is the default if there is % only one input argument. % % G = GSCALE(F, 'full16') scales the intensities of F to the full % 16-bit intensity range [0, 65535]. % % G = GSCALE(F, 'minmax', LOW, HIGH) scales the intensities of F to % the range [LOW, HIGH]. These values must be provided, and they % must be in the range [0, 1], independently of the class of the % input. GSCALE performs any necessary scaling. If the input is of % class double, and its values are not in the range [0, 1], then % GSCALE scales it to this range before processing. % % The class of the output is the same as the class of the input. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins % Digital Image Processing Using MATLAB, Prentice-Hall, 2004 % $Revision: 1.5 $ $Date: 2003/11/21 14:36:09 $ if length(varargin) == 0 % If only one argument it must be f. method = 'full8'; else method = varargin{1}; end if strcmp(class(f), 'double') & (max(f(:)) > 1 | min(f(:)) < 0) f = mat2gray(f); end % Perform the specified scaling. switch method case 'full8' g = im2uint8(mat2gray(double(f))); case 'full16' g = im2uint16(mat2gray(double(f))); case 'minmax' low = varargin{2}; high = varargin{3}; if low > 1 | low < 0 | high > 1 | high < 0 error('Parameters low and high must be in the range [0, 1].') end if strcmp(class(f), 'double') low_in = min(f(:)); high_in = max(f(:)); elseif strcmp(class(f), 'uint8') low_in = double(min(f(:)))./255; high_in = double(max(f(:)))./255; elseif strcmp(class(f), 'uint16') low_in = double(min(f(:)))./65535; high_in = double(max(f(:)))./65535; end % imadjust automatically matches the class of the input. g = imadjust(f, [low_in high_in], [low high]); otherwise error('Unknown method.') end
運行結果:
五.結果分析
(1)由第一個圖可以看出,圖像經過低通濾波器,圖像的高頻分量濾掉了,圖像變得平滑。
(2)由第二個圖可以看出,圖像不同的截止頻率,出來的圖像也不同,系數小的效果強。