我谈一阶差分算子的演化史以及MATLAB实现

发布时间:2026/10/21 3:04:36
我谈一阶差分算子的演化史以及MATLAB实现 禹晶、肖创柏、廖庆敏《数字图像处理面向新工科的电工电子信息基础课程系列教材》P234一维离散序列的一阶差分的定义可以直接扩展到二维离散序列。前向差分 (Forward Difference)对于一个函数f ( x ) f(x)f(x)在点x xx处的一阶前向差分可以表示为f ′ ( x ) ≈ f ( x h ) − f ( x ) h f(x) \approx \frac{f(x h) - f(x)}{h}f′(x)≈hf(xh)−f(x)​其中h hh是一个小的正数表示步长。前向差分是最简单的差分形式它使用当前点和下一个点之间的差值来近似导数。这样的直接扩展二维是可分离的。中心差分 (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(xh)−f(x−h)​一维到二维。一维的计算MATLAB没有使用滤波直接计算差分。casecentraldifferenceifisrow(I)Gxgradient(I);ifnargout1Gyzeros(size(I),like,I);endelseifiscolumn(I)Gxzeros(size(I),like,I);ifnargout1Gygradient(I);endelse[Gx,Gy]gradient(I);endcaseintermediatedifferenceGxzeros(size(I),like,I);if(size(I,2)1)Gx(:,1:end-1)diff(I,1,2);endifnargout1Gyzeros(size(I),like,I);if(size(I,1)1)Gy(1:end-1,:)diff(I,1,1);endend二维与一维最本质的不同在于二维有对角方向。所以才有了Roberts、Prewitt和Sobel算子。Roberts、Prewitt和Sobel算子是考虑对角邻域的二维一阶差分模板。Roberts是第一个通过交叉考虑了一个对角邻域。Prewitt和Sobel算子均考虑了4个对角邻域。Prewitt考虑对角方向和四邻域方向的距离相同Sobel算子考虑街区距离对角方向的距离是2。欧氏距离有根号不用。Sobel的数学推导这三个MATLAB都是通过滤波计算。Roberts是在edge中考虑了归一化。Prewitt和Sobel算子是在imgradientxy函数中没有考虑归一化。elseifstrcmp(method,roberts)x_mask[10;0-1]/2;% Roberts approximation to diagonal derivativey_mask[01;-10]/2;% compute the gradient in x and y directionbximfilter(a,x_mask,replicate);byimfilter(a,y_mask,replicate);% compute the magnitudebkx*bx.*bxky*by.*by;casesobelh-fspecial(sobel);% Align mask correctly along the x- and y- axesGximfilter(I,h,replicate);ifnargout1Gyimfilter(I,h,replicate);endcaseprewitth-fspecial(prewitt);% Align mask correctly along the x- and y- axesGximfilter(I,h,replicate);ifnargout1Gyimfilter(I,h,replicate);end