更复杂些的滤波算子通常是先利用高斯滤波来平滑,而后计算其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介绍,本文的主要参考)