小白學岡薩雷斯數字圖像處理——第四章:頻域處理

4.1 二維離散傅里葉變換

定義與一維DFT相同,此處不再贅述。

在計算二維傅里葉變換之前,可將f(x,y)*(-1)^(x+y)次,可將原點的變換值移到頻譜中心。
在這裏插入圖片描述

4.2 在MATLAB中計算並可視化二維DFT

下列函數返回M*N的傅里葉變換;數據原點在左上角,而兩個四分之一週期交匯於頻率矩形的中心。

F = fft2(f)

若需要進行0填充:

F = fft2(f,P,Q)

生成的矩陣大小爲P*Q,多餘的用0填充。

我們可以使用fftshift函數將變換的原點移到頻率矩形的中心,也可用ifftshift將其復原:

Fc = fftshift(F)
F = ifftshift(Fc)

我們可以使用對數變換來使頻譜的視覺增強:

S2 = log(1+abs(Fc));

在這裏插入圖片描述
可用如下函數計算頻率矩形的中心點可不必考慮M,N的奇偶性:

[floor(M/2) + 1 , floor(N/2) + 1]

由於浮點計算的舍入誤差,傅里葉逆變換的輸出會有很小的虛數分量,因此需要計算後取實部:

f = real(ifft2(F));

4.3 頻域濾波

4.3.1 基本概念

空間域和頻域線性濾波的基礎均爲卷積定理

頻域濾波的目的是選擇一個濾波器傳遞函數,以便按照指定的方式修改F(u,v)。例如,當乘一個低通濾波器時,高頻分量會被衰減,而低頻分量保持不變,此結果會出現模糊現象(平滑)。

注意:當我們使用DFT進行濾波時,圖像及其變換都被看作是具有週期性的。若週期關於函數的非零部分的持續時間很近,則對周期函數執行卷積運算會導致相鄰週期之間的干擾,稱爲摺疊誤差的干擾。我們可以通過使用0填充函數的方法來避免。
若函數f和h的大小分別爲AB和CD,通過選擇P>=A+C-1,Q>=B+D-1即可避免摺疊誤差。

paddedsize函數可計算滿足P,Q的最小偶數值(加快FFT速度),還可填充輸入圖像,使其等於最接近的2的整數次冪。

function PQ = paddedsize(AB, CD, nargin)
% 計算填充尺寸以供基於FFT的濾波器
% nargin = 1   PQ = PADDEDSIZE(AB),AB = [A B], PQ = 2 * AB
%
% nargin = 2   PQ = PADDEDSIZE(AB, 'PWR2'), PQ(1= PQ(2= 2 ^ nextpow2(2 * m), m =
% MAX(AB).
% 
% nargin = 3   PQ = PADDEDSIZE(AB, CD),AB = [A B], CD = [C D]
%
% nargin = 其它 PQ = PADDEDSIZE(AB, CD, 'PWR2'), PQ(1= PQ(2= 2 ^ nextpow2(2 * m), m =
% MAX([AB CD]).

if nargin == 1
    PQ = 2 * AB;
else if nargin == 2 & ~ischar(CD)
    PQ = AB + CD -1;
    PQ = 2 * ceil(PQ / 2);  % ceil(N)返回比N大的最小整數,避免出現奇數
else if nargin == 2
    m = max(AB);
    P = 2 ^ nextpow2(2 * m);  % nextpow2(N)返回第一個P,使得2. ^ P> = abs(N)。 
    % 對於FFT運算,找到最接近兩個序列長度的2 的冪次方通常很有用。
    PQ = [P, P];
else if nargin == 3
    m = max([AB CD]);
    P = 2 ^ nextpow2(2 * m);
    PQ = [P, P];
else
    error('Wrong number of input')
end

填充濾波與非填充濾波對比:
在這裏插入圖片描述

4.2 DFT濾波的基本步驟

(1)使用函數paddedsize獲取填充參數

PQ = paddedsize(size(f));

(2)得到使用填充的傅里葉變換

F = fft2(f,PQ(1),PQ(2));

(3)生成濾波函數H

濾波函數大小爲PQ(1)*PQ(2) 格式必須如下圖b所示
在這裏插入圖片描述
若是a格式,需用fftshift進行處理。

(4)將變換乘濾波函數

G = H.*F;

(5)獲得G的傅里葉逆變換的實部

g = real(ifft2(G));

(6)將左上部的矩陣修剪爲原始大小

g = g(1:size(f,1), 1:size(f,2));

4.3 頻域濾波

dftfilt函數

g = real(ifft(H.*F));
g = g(1:size(f,1),1:size(f,2));

4.4 從空間濾波器獲得頻域濾波器

一般,當濾波器較小時,空間濾波比頻域濾波更有效。

給定一個空間濾波器h,生成一個頻域濾波器H的方法:

H = fft2(h,PQ(1),PQ(2));

但是,這種方法獲得的頻域濾波器與空間濾波器不等價(使用imfilter的空間濾波,imfilter使用了相關,且濾波原點在中心處)

H = freqz2(h,R,C);

此函數用於計算FIR濾波器的頻率響應,結果是我們所希望的頻域濾波器。其中,h是一個二維空間濾波器,R爲h的行數(R=PQ(1)),C爲列數(C=PQ(2))。

4.5 在頻域中直接生成濾波器

4.5.1建立用於實現頻域濾波器的網格數組

dftuv函數:

function [U, V] = dftuv(M, N)
% DFTUV Computes meshgrid frequency matrices.
% [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and V. U and
% V are useful for computing frequency-domain filter functions that can be
% used with DFTFILT. U and V are both M-by-N.
% more details to see the textbook Page 93
%
% [U,V] = DFTUV(M,N)計算網格頻率矩陣U和V。 U和V對於計算可與DFTFILT一起使用的
% 頻域濾波器函數很有用。 U和V都是M-by-N。更多細節見岡薩雷斯教材93% Set up range of variables.
% 設置變量範圍
u = 0 : (M - 1);
v = 0 : (N - 1);

% Compute the indices for use in meshgrid.
% 計算網格的索引,即將網絡的原點轉移到左上角,因爲FFT計算時變換的原點在左上角。
idx = find(u > M / 2);
u(idx) = u(idx) - M;
idy = find(v - N / 2);
v(idy) = v(idy) - N;

% Compute the meshgrid arrays.
% 計算網格矩陣
[V, U] = meshgrid(v, u);


4.5.2 低通頻域濾波器

H(u,v)= 1 (若D(u,v)<= D0)
H(u,v)= 0 (若D(u,v)> D0)

D0爲指定的非負數,D(u,v)爲點(u,v)到濾波器中心的距離。D(u,v)=D0的點的軌跡爲一個圓。若濾波器H乘一幅圖像的傅里葉變換,則理想濾波器切斷了圓外F所有的分量,圓內和圓上的值不變。

在這裏插入圖片描述
高斯低通濾波器:

PQ = paddedsize(size(f));
[U , V] = dftuv(PQ(1),PQ(2));
D0 = 0.05*PQ(2);
F = fft2(f,PQ(1),PQ(2));
H = exp(-(U.^2+V.^2)/(2*D0^2));
g = dftfilt(f,H);
#以圖像形式查看該濾波器
figure(),imshow(fftshift(h),[])
#將頻譜顯示爲一幅圖像
figure(),imshow(log(1+abs(fftshift(F))),[])
#輸出圖像
figure(),imshow(g,[]);

高斯低通濾波後的圖像與原圖像相比要模糊一些。

生成濾波器函數lpfilter():

function [H, D] = lpfilter(type, M, N, D0, n)
% LPFILTER Computes frequency domain lowpass filters
%   H = LPFILTER(TYPE, M, N, D0, n) creates the transfer function of a
%   lowpass filter, H, of the specified TYPE and size (M-by-N). To view the
%   filter as an image or mesh plot, it should be centered using H =
%   fftshift(H)
%   Valid value for TYPE, D0, and n are:
%   'ideal' Ideal lowpass filter with cutoff frequency D0. n need not be
%           supplied. D0 must be positive.
%   'btw'   Butterworth lowpass filter of order n, and cutoff D0. The
%           default value for n is 1.0. D0 must be positive.
%   'gaussian'Gaussian lowpass filter with cutoff (standard deviation) D0.
%           n need not be supplied. D0 must be positive.
%
% 得到指定類型的低通濾波器


[U, V] = dftuv(M, N);
% 計算距離 D(U, V)
D = sqrt(U.^2 + V.^2);
% 濾波計算
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

4.5.3 線框圖與表面圖

對於二維函數,使用函數mesh可以繪製線框圖

mesh(H(1:k:end,1:k:end))

#默認顏色爲彩色
colormap([0 0 0]) %設置爲黑色
colormap(gray) %設置爲灰色

#開啓/關閉 網格/座標軸
grid on grid off 
axis on axis off

#調節方位角/仰角
view(az,el)

#將函數繪製成表面圖來代替線框圖
surf(H)

#插值,平滑小面描影並刪除網格線
shading interp

4.6 銳化頻域濾波器

高通濾波會使圖像變得更清晰(銳化)。

4.6.1 基本的高通濾波器

hpfilter()

function H = hpfilter(type, M, N, D0, n)
% LPFILTER Computes frequency domain highpass filters
%   H = HPFILTER(TYPE, M, N, D0, n) creates the transfer function of a
%   highpass filter, H, of the specified TYPE and size (M-by-N). To view the
%   filter as an image or mesh plot, it should be centered using H =
%   fftshift(H)
%   Valid value for TYPE, D0, and n are:
%   'ideal' Ideal highpass filter with cutoff frequency D0. n need not be
%           supplied. D0 must be positive.
%   'btw'   Butterworth highpass filter of order n, and cutoff D0. The
%           default value for n is 1.0. D0 must be positive.
%   'gaussian'Gaussian highpass filter with cutoff (standard deviation) D0.
%           n need not be supplied. D0 must be positive.
% The transfer function Hhp of highpass filter is 1 - Hlp, where Hlp is the
% transfer function of the corresponding lowpass filter. Thus, we can use
% function lpfilter to generate highpass filters.
%
% 計算給定類型(理想、巴特沃斯、高斯)的頻域高通濾波器

% Use function dftuv to set up the meshgrid arrays needed for computing the
% required distances.
if nargin == 4
    n = 1;
end
Hlp = lpfilter(type, M, N, D0, n);
H = 1 - Hlp;      

高通濾波:

PQ = paddedsize(size(f));
D0 = 0.05*PQ(1);
H = hpfilter('gaussian',PQ(1),PQ(2),D0);
g = dftfilt(f,H);
figure,imshow(g,[])

在這裏插入圖片描述

4.6.2 高頻強調濾波

普通的高通濾波器偏離直流項,從而把圖像的平均值降到了0。一種補償方法是給高通濾波器加上一個偏移量,若同時將濾波器乘一個大於1的常數,則稱爲高頻強調濾波,因爲該常數突出了高頻部分。這個乘數也增加了低頻部分的幅度,但是隻要偏離量與乘數相比較小,低頻增強的部分就弱於高頻增強的影響。
傳遞函數爲:
在這裏插入圖片描述
應用:X光片的增強。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章