对于每个像素 I ( x , y ) I(x, y) I(x,y),应用这些卷积核以获得水平和垂直方向上的梯度值:
G x ( x , y ) = ∑ i = − 1 1 ∑ j = − 1 1 I ( x + i , y + j ) ⋅ G x ( i + 1 , j + 1 ) G_x(x, y) = \sum_{i=-1}^{1} \sum_{j=-1}^{1} I(x+i, y+j) \cdot G_x(i+1, j+1) Gx(x,y)=i=−1∑1j=−1∑1I(x+i,y+j)⋅Gx(i+1,j+1)
G y ( x , y ) = ∑ i = − 1 1 ∑ j = − 1 1 I ( x + i , y + j ) ⋅ G y ( i + 1 , j + 1 ) G_y(x, y) = \sum_{i=-1}^{1} \sum_{j=-1}^{1} I(x+i, y+j) \cdot G_y(i+1, j+1) Gy(x,y)=i=−1∑1j=−1∑1I(x+i,y+j)⋅Gy(i+1,j+1)
梯度幅值
然后,计算梯度幅值(也称为梯度强度):
G = G x 2 + G y 2 G = \sqrt{G_x^2 + G_y^2} G=Gx2+Gy2
为了便于计算,也可以使用近似计算梯度幅值:
G ≈ ∣ G x ∣ + ∣ G y ∣ G \approx |G_x| + |G_y| G≈∣Gx∣+∣Gy∣
梯度方向
θ = arctan ( G y G x ) \theta = \arctan\left(\frac{G_y}{G_x}\right) θ=arctan(GxGy)
使用场景