---
title: "Sobel operator"
author: "Perrin Yong"
author_profile: https://www.pystone.net/profile/
published_by: "Perrin Yong"
canonical: https://www.pystone.net/notes/sobel-operator/
type: note
content_role: unspecified
visibility: public
id_stability: rename-stable
source_path: "10-计算机、信息技术与工程/05-游戏图形与运行时/渲染基础/Sobel operator.md"
content_hash: 376133af7cae1a9e8858f23d93dd2ab9b05271bd4f4d0d77aeb0850c0fbca957
knowledge_version: 224c990773de.5fa8af6e39fa
site_commit: 224c990773de166d23a886306577dd90379529ce
notes_commit: 5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a
---
# Sobel operator

> 创建时间：2021/2/25 21:01

  * Sobel operator
    * 背景知识
      * 术语
      * 算子(operator)
      * 差分算子
      * 图像处理中的卷积
    * Sobel边缘检测
    * 代码
    * Ref

## 背景知识

索贝尔算子（Sobel operator）主要用作边缘检测，在技术上，它是一离散性差分算子，用来运算图像亮度函数的灰度之近似值。在图像的任何一点使用此算子，将会产生对应的灰度矢量或是其法矢量。

### 术语

  * 边缘：灰度或结构等信息的突变处，边缘是一个区域的结束，也是另一个区域的开始，利用该特征可以分割图像。

  * 边缘点：图像中具有坐标[x，y]，且处在强度显著变化的位置上的点。

  * 边缘段：对应于边缘点坐标[x，y]及其方位 ，边缘的方位可能是梯度角。

### 算子(operator)

算子（英语：Operator）是将一个元素在向量空间（或模）中转换为另一个元素的映射。

![Alt text](/media/dc7d836db46a7e2c4761.png)

摘录知乎的回答（<https://www.zhihu.com/question/24989360/answer/29702293>）：
算子含义为操作、运算等等。算子可以理解为，把一个函数变成另一个函数的东西。
函数是从数到数的映射。
泛函是从函数到数的映射。
算子是从函数到函数的映射。
当然，有的时候这几个词可以混用，比如可以可以把数当作常函数，那么普通的函数也可以看作泛函或算子；再比如考虑从算子到算子的映射，你仍然可以叫它算子。

![Alt text](/media/bfa3967d5c84ed72be30.png)

### 差分算子

百度百科：
差分算子是一种算子，对任一实函数f(x)，若记Δf(x)=f(x+1)-f(x)，则称Δ为向前差分算子，简称差分算子。差分是计算数学的基本概念之一，指离散函数在离散节点上的改变量。

维基百科：
差分，又名差分函数或差分运算，是数学中的一个概念。它将原函数
映射到
。差分运算，相应于微分运算，是微积分中重要的一个概念。

### 图像处理中的卷积

![Alt text](/media/c2d13b603268d834b7e6.png)

所谓两个函数的卷积，本质上就是先将一个函数翻转，然后进行滑动叠加。
在图像处理的中，卷积处理的结果，其实就是把每个像素周边的像素都考虑进来，进行加权求和。

![Alt text](/media/abb8d03709abfd168f5b.png)

![Alt text](/media/09e4b946c151ff7d3008.png)

![Alt text](/media/e20169a00a4ad6c2e2cd.png)

![Alt text](/media/d2352361e72077d73c67.png)

计算卷积时，就可以用 f 和 g’ 的内积：

![Alt text](/media/3dda0535799491690f09.png)

使用卷积，实质上是使用一个矩阵，表示像素点周围各个像素点的权值，对范围内的像素值做加权求平均。

## Sobel边缘检测

使用Sobel算子对传进来的图像像素做卷积。
卷积的实质是在求梯度值。
卷积核( convolution kernels)

![Alt text](/media/75b56b9e717744564c8a.png)

> 可以直观地看到，用这两个矩阵与像素及其周围的像素点求卷积之后，像素亮度在x或y轴方向上变化越大，得到的值的绝对值越大。

The kernels can be applied separately to the input image, to produce separate measurements of the gradient component in each orientation.

> A very common operator for doing this is a Sobel Operator, which is an approximation to a derivative of an image.

These can then be combined together to find the absolute magnitude of the gradient at each point and the orientation of that gradient.

![Alt text](/media/258688e0753703558f98.png)

Typically, an approximate magnitude is computed using:

![Alt text](/media/c0eecfd4bf6b6a844264.png)

The angle of orientation of the edge (relative to the pixel grid) giving rise to the spatial gradient is given by:

![Alt text](/media/353bca4e0ba95a9200ae.png)

![Alt text](/media/d42577c6cbc52c85e03e.png)

## 代码

```cpp
string s = String.Empty;

```

## Ref

<https://zh.wikipedia.org/zh-cn/%E7%AE%97%E5%AD%90>
<https://www.zhihu.com/question/24989360/answer/29702293>
<https://baike.baidu.com/item/%E5%B7%AE%E5%88%86%E7%AE%97%E5%AD%90>
<https://medium.datadriveninvestor.com/understanding-edge-detection-sobel-operator-2aada303b900>
<http://homepages.inf.ed.ac.uk/rbf/HIPR2/sobel.htm>
<http://recreationstudios.blogspot.com/search/label/Compute%20Shader>
