我谈一阶差分算子的演化
禹晶、肖创柏、廖庆敏《数字图像处理(面向新工科的电工电子信息基础课程系列教材)》P234
二维与一维最本质的不同在于,二维有对角方向。所以才有了Roberts、Prewitt和Sobel算子。
一维离散序列的一阶差分的定义可以直接扩展到二维离散序列。
前向差分 (Forward Difference)
对于一个函数 f ( x ) f(x) f(x),在点 x x x 处的一阶前向差分可以表示为:
f ′ ( x ) ≈ f ( x + h ) − f ( x ) h f'(x) \approx \frac{f(x + h) - f(x)}{h} f′(x)≈hf(x+h)−f(x)
其中, h h h 是一个小的正数,表示步长。前向差分是最简单的差分形式,它使用当前点和下一个点之间的差值来近似导数。
这样的直接扩展,二维是可分离的。
中心差分 (Central Difference)
中心差分通常提供更高的精度,因为它使用当前点两侧的点来近似导数:
f ′ ( x ) ≈ f ( x + h ) − f ( x − h ) 2 h f'(x) \approx \frac{f(x + h) - f(x - h)}{2h} f′(x)≈2hf(x+h)−f(x−h)
一维到二维。
一维的计算MATLAB没有使用滤波,直接计算差分。
case 'centraldifference' if isrow(I) Gx = gradient(I);if nargout > 1Gy = zeros(size(I),'like',I);end elseif iscolumn(I) Gx = zeros(size(I),'like',I);if nargout > 1Gy = gradient(I);end else [Gx, Gy] = gradient(I);endcase 'intermediatedifference' Gx = zeros(size(I),'like',I);if (size(I,2) > 1) Gx(:,1:end-1) = diff(I,1,2);endif nargout > 1Gy = zeros(size(I),'like',I);if (size(I,1) > 1)Gy(1:end-1,:) = diff(I,1,1);endend
Roberts、Prewitt和Sobel算子是考虑对角邻域的二维一阶差分模板。
Roberts是第一个,通过交叉,考虑了一个对角邻域。
Prewitt和Sobel算子均考虑了4个对角邻域。Sobel的数学推导
这三个MATLAB都是通过滤波计算。Roberts是在edge中,考虑了归一化。Prewitt和Sobel算子是在imgradientxy函数中,没有考虑归一化。
elseif strcmp(method, 'roberts')x_mask = [1 0; 0 -1]/2; % Roberts approximation to diagonal derivativey_mask = [0 1;-1 0]/2;% compute the gradient in x and y directionbx = imfilter(a,x_mask,'replicate');by = imfilter(a,y_mask,'replicate');% compute the magnitudeb = kx*bx.*bx + ky*by.*by;
case 'sobel'h = -fspecial('sobel'); % Align mask correctly along the x- and y- axesGx = imfilter(I,h','replicate');if nargout > 1Gy = imfilter(I,h,'replicate');endcase 'prewitt'h = -fspecial('prewitt'); % Align mask correctly along the x- and y- axesGx = imfilter(I,h','replicate');if nargout > 1Gy = imfilter(I,h,'replicate');end