# 1.2 边缘检测示例（Edge detection example）

对于CV问题，神经网络由浅层到深层，分别可以检测出图片的边缘特征 、局部特征（例如眼睛、鼻子等）、整体面部轮廓

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/a4b8429a41f31afb14adaa9204f98c66.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/a4b8429a41f31afb14adaa9204f98c66.png)

## 图片的边缘检测

最常检测的图片边缘有两类：一是**垂直边缘（vertical edges）**，二是**水平边缘（horizontal edges）**

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/47c14f666d56e509a6863e826502bda2.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/47c14f666d56e509a6863e826502bda2.png)

图片的边缘检测可以通过与相应滤波器进行卷积来实现。以垂直边缘检测为例，原始图片尺寸为6x6，滤波器filter尺寸为3x3，卷积后的图片尺寸为4x4，得到结果如下：

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/5f9c10d0986f003e5bd6fa87a9ffe04b.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/5f9c10d0986f003e5bd6fa87a9ffe04b.png)

∗表示卷积操作。python中，卷积用conv\_forward()表示；tensorflow中，卷积用tf.nn.conv2d()表示；keras中，卷积用Conv2D()表示

垂直边缘是一个3×3的区域，左边是明亮的像素，中间的并不需要考虑，右边是深色像素。在这个6×6图像的中间部分，明亮的像素在左边，深色的像素在右边，就被视为一个垂直边缘

[![](https://github.com/fengdu78/deeplearning_ai_books/raw/master/images/0c8b5b8441557b671431d515aefa1e8a.png)](https://github.com/fengdu78/deeplearning_ai_books/blob/master/images/0c8b5b8441557b671431d515aefa1e8a.png)
