Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b1252da

Browse files
committed
update ch19-2
1 parent 5fd1169 commit b1252da

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed
File renamed without changes.

‎ch19-Canny边缘检测/19.Canny.py‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,41 @@
11
# -*- coding: utf-8 -*-
22

3+
'''
4+
Canny 边缘检测是一种 常流 的 缘检测算法 是 John F.Canny 在
5+
1986 年提出的。它是一个有很多步构成的算法
6+
由于 缘检测很容易受到噪声影响 所以第一步是使用 5x5 的 斯滤波器 去 噪声
7+
对平滑后的图像使用 Sobel 算子 算水平方向和竖直方向的一 导数 图 像梯度 Gx 和 Gy
8+
梯度的方向一般总是与边界垂直。
9+
梯度方向 归为四类: 垂直 水平 和 两个对角线。
10+
非极大值抑制
11+
12+
滞后阈值
13+
现在 确定 些 界才是真正的边界。 时我们 置两个阈值 minVal 和 maxVal。
14+
当图像的灰度梯度 于 maxVal 时 为是真的边界
15+
那些低于 minVal 的 界会 抛弃。
16+
如果介于两者之间的 就 看这个点是否与某个被确定为真正的边界点相连
17+
如果是就认为它也是边界点 如果不是 就抛弃。
18+
19+
OpenCV 中的 Canny 边界检测
20+
在 OpenCV 中只 需要 一个函数 cv2.Canny() 就可以完成以上几步。
21+
我们看如何使用这个函数。
22+
第一个参数是输入图像。
23+
第二和第三 个分别是 minVal 和 maxVal。
24+
第三个参数 置用来计算图像梯度的 Sobel卷积核的大小 默认值为 3。
25+
最后一个参数是 L2gradient 它可以用来 设定 求梯度大小的方程。
26+
如果 为 True 就会使用我们上 提到 的方程 否则 使用方程 Edge−Gradient (G) = |G2x| + |G2y| 代替, 默认值为 False。
27+
'''
328
import cv2
429
import numpy as np
530
from matplotlib import pyplot as plt
631

7-
img = cv2.imread('../data/messi5.jpg',0)
8-
edges = cv2.Canny(img,100,200)
32+
img = cv2.imread('../data/messi5.jpg',0)
33+
edges = cv2.Canny(img,100,200)
934

10-
plt.subplot(121),plt.imshow(img,cmap='gray')
35+
plt.subplot(121),plt.imshow(img,cmap='gray')
1136
plt.title('Original Image'), plt.xticks([]), plt.yticks([])
1237

13-
plt.subplot(122),plt.imshow(edges,cmap='gray')
38+
plt.subplot(122),plt.imshow(edges,cmap='gray')
1439
plt.title('Edge Image'), plt.xticks([]), plt.yticks([])
1540

16-
plt.show()
41+
plt.show()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /