索伯算子(Sobel operator)是图像处理中的算子之一,有时又称为索伯-费德曼算子或索贝滤波器,在影像处理及电脑视觉领域中常被用来做边缘检测。Sobel 算子是一个离散微分算子 (discrete differentiation operator)
如果以{\displaystyle \mathbf {A} }代表原始图像,{\displaystyle \mathbf {G_{x}} }
及{\displaystyle \mathbf {G_{y}} }
分别代表经横向及纵向边缘检测的图像,其公式如下:
{\displaystyle \mathbf {G_{x}} ={\begin{bmatrix}+1&0&-1\\+2&0&-2\\+1&0&-1\end{bmatrix}}*\mathbf {A} \quad {\mbox{and}}\quad \mathbf {G_{y}} ={\begin{bmatrix}+1&+2&+1\\0&0&0\\-1&-2&-1\end{bmatrix}}*\mathbf {A} }
图像的每一个像素的横向及纵向梯度近似值可用以下的公式结合,来计算梯度的大小。
{\displaystyle \mathbf {G} ={\sqrt {\mathbf {G_{x}} ^{2}+\mathbf {G_{y}} ^{2}}}}
然后可用以下公式计算梯度方向。
{\displaystyle \mathbf {\Theta } =\operatorname {arctan} \left({\mathbf {G_{y}} \over \mathbf {G_{x}} }\right)}
以纵向边缘为例,如果角度{\displaystyle \Theta }等于零,即代表图像的纵向边缘右方较亮;若为 {\displaystyle \pi }
,则左方较亮。
例子:
参考:
维基、百度