渐变和渐变有什么区别?

What is the difference between gradient and imgradient?

我想计算图像 I 的梯度。我有两个选项

[Gx, Gy] = gradient(I);
g = sqrt(Gx.^2+Gy.^2);

[g,~] = imgradient(I, 'sobel');

我的问题是

  1. 第一个选项中使用的梯度法是什么?

  2. 使用sobel方法求梯度有什么好处?

谢谢大家

这是我试过的

I=zeros([128 128]);
I(50:100,50:100)=100;
[Gx, Gy] = gradient(I);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I, 'sobel');
subplot(121);imshow(g1,[]);title('First option')
subplot(122);imshow(g2,[]);title('Second option')

图像加噪后,差异会更明显

I=zeros([128 128]);
I(50:100,50:100)=100;
%% Add noise image;
noiseStd=5;
I_noise = I + (noiseStd * randn([128, 128]));
[Gx, Gy] = gradient(I_noise);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I_noise, 'sobel');
subplot(121);imshow(g1,[]);title('First option')
subplot(122);imshow(g2,[]);title('Second option')

作为可视化,第二个选项提供了更多的亮度值

区别确实是 'Sobel' 运算符。 sobel 算子是一个矩阵,它与图像进行卷积以计算其方向梯度。

sobel 运算符定义为(来自维基百科):

gradient所做的只是方向差异。你可以将其解释为渐变做

Gx=[ 0 0 0;                Gx=[ 0 -1 0;
    -1 0 1;         and         0  0 0;
     0 0 0]                     0  1 0];

这个区别是因为当一个人有一个图像时,就知道它是一个连续域的离散化。 Sobel 运算符有助于考虑给定像素 "around" 发生的事情。

gradient exclusively uses central differences and imgradient 给你一个选择,例如'central' 以及默认值 'sobel'。使用第一个选项 imgradient 看起来与 gradient:

相同
I=zeros([128 128]);
I(50:100,50:100)=100;
%% Add noise image;
noiseStd=5;
I_noise = I + (noiseStd * randn([128, 128]));
[Gx, Gy] = gradient(I_noise);
g1 = sqrt(Gx.^2+Gy.^2);
[g2,~] = imgradient(I_noise, 'sobel');
[g3,~] = imgradient(I_noise, 'central');
subplot(131);imshow(g1,[]);title('gradient')
subplot(132);imshow(g2,[]);title('imgradient sobel')
subplot(133);imshow(g3,[]);title('imgradient central')


对于imgradient,有五个选项可用:

  • 'sobel' Sobel 梯度算子(默认)
  • 'prewitt' Prewitt梯度算子
  • 'central'中心差分梯度:dI/dx = (I(x+1) - I(x-1))/2
  • 'intermediate'中间差分梯度:dI/dx = I(x+1) - I(x)
  • ‘roberts’罗伯茨梯度算子

虽然文档解释了:

The algorithmic approach taken in imgradient for each of the listed gradient methods is to first compute directional gradients, Gx and Gy, with respect to the x-axis and y-axis. The x-axis is defined along the columns going right and the y-axis is defined along the rows going down. The gradient magnitude and direction are then computed from their orthogonal components Gx and Gy.