更復雜些的濾波算子通常是先利用高斯濾波來平滑,而後計算其1階和2階微分。因爲它們濾除高頻和低頻,所以稱爲帶通濾波器(band-pass filters)。html
在介紹具體的帶通濾波器前,先介紹必備的圖像微分知識。node
對於離散狀況(圖像),其導數必須用差分方差來近似,有函數
,前向差分 forward differencing (1.2)性能
,中心差分 central differencing (1.3)ui
1)前向差分的Matlab實現spa
1 function dimg = mipforwarddiff(img,direction) 2 % MIPFORWARDDIFF Finite difference calculations 3 % 4 % DIMG = MIPFORWARDDIFF(IMG,DIRECTION) 5 % 6 % Calculates the forward-difference for a given direction 7 % IMG : input image 8 % DIRECTION : 'dx' or 'dy' 9 % DIMG : resultant image 10 % 11 % See also MIPCENTRALDIFF MIPBACKWARDDIFF MIPSECONDDERIV 12 % MIPSECONDPARTIALDERIV 13 14 % Omer Demirkaya, Musa Asyali, Prasana Shaoo, ... 9/1/06 15 % Medical Image Processing Toolbox 16 17 imgPad = padarray(img,[1 1],'symmetric','both');%將原圖像的邊界擴展 18 [row,col] = size(imgPad); 19 dimg = zeros(row,col); 20 switch (direction) 21 case 'dx', 22 dimg(:,1:col-1) = imgPad(:,2:col)-imgPad(:,1:col-1);%x方向差分計算, 23 case 'dy', 24 dimg(1:row-1,:) = imgPad(2:row,:)-imgPad(1:row-1,:); 25 otherwise, disp('Direction is unknown'); 26 end; 27 dimg = dimg(2:end-1,2:end-1);
2)中心差分的Matlab實現3d
1 function dimg = mipcentraldiff(img,direction) 2 % MIPCENTRALDIFF Finite difference calculations 3 % 4 % DIMG = MIPCENTRALDIFF(IMG,DIRECTION) 5 % 6 % Calculates the central-difference for a given direction 7 % IMG : input image 8 % DIRECTION : 'dx' or 'dy' 9 % DIMG : resultant image 10 % 11 % See also MIPFORWARDDIFF MIPBACKWARDDIFF MIPSECONDDERIV 12 % MIPSECONDPARTIALDERIV 13 14 % Omer Demirkaya, Musa Asyali, Prasana Shaoo, ... 9/1/06 15 % Medical Image Processing Toolbox 16 17 img = padarray(img,[1 1],'symmetric','both'); 18 [row,col] = size(img); 19 dimg = zeros(row,col); 20 switch (direction) 21 case 'dx', 22 dimg(:,2:col-1) = (img(:,3:col)-img(:,1:col-2))/2; 23 case 'dy', 24 dimg(2:row-1,:) = (img(3:row,:)-img(1:row-2,:))/2; 25 otherwise, 26 disp('Direction is unknown'); 27 end 28 dimg = dimg(2:end-1,2:end-1);
實例:技術圖像x方向導數code
1 I = imread('coins.png'); figure; imshow(I); 2 Id = mipforwarddiff(I,'dx'); figure, imshow(Id);
原圖像 x方向1階導數orm
圖像I的梯度定義爲 ,其幅值爲
。出於計算性能考慮,幅值也可用
來近似。
Matlab函數
1)gradient:梯度計算
2)quiver:以箭頭形狀繪製梯度。注意放大下面最右側圖可看到箭頭,因爲這裏計算橫豎兩個方向的梯度,所以箭頭方向都是水平或垂直的。
實例:仍採用上面的原始圖像
1 I = double(imread('coins.png')); 2 [dx,dy]=gradient(I); 3 magnitudeI=sqrt(dx.^2+dy.^2); 4 figure;imagesc(magnitudeI);colormap(gray);%梯度幅值 5 hold on;quiver(dx,dy);%疊加梯度方向
梯度幅值 梯度幅值+梯度方向
拉普拉斯算子是n維歐式空間的一個二階微分算子。它定義爲兩個梯度向量算子的內積
其在二維空間上的公式爲: (3.3)
對於1維離散狀況,其二階導數變爲二階差分
2)所以,二階差分爲
對於2維離散狀況(圖像),拉普拉斯算子是2個維上二階差分的和(見式3.3),其公式爲:
上式對應的卷積核爲
經常使用的拉普拉斯核有:
拉普拉斯算子會突出像素值快速變化的區域,所以經常使用於邊緣檢測。
Matlab裏有兩個函數
1)del2
2)fspecial:圖像處理中通常利用Matlab函數fspecial
h = fspecial('laplacian', alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator.
The parameter alpha controls the shape of the Laplacian and must be in the range 0.0 to 1.0. The default value for alpha is 0.2.
http://fourier.eng.hmc.edu/e161/lectures/gradient/node8.html (很是清晰的Laplacian Operator介紹,本文的主要參考)