python opencv3 直线检测

git:https://github.com/linyi0604/Computer-Vision

 1 # coding:utf8
 2 
 3 import cv2
 4 import numpy as np
 5 
 6 
 7 # 读入图像
 8 img = cv2.imread("../data/line1.png")
 9 # 转为灰度图像
10 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
11 # Canny边缘检测
12 edges = cv2.Canny(gray, 50, 100)
13 """
14 canny边缘检测:
15 有五个步骤:
16         1 高斯滤波器降噪
17         2 计算梯度
18         3 边缘上使用非最大抑制 nms
19         4 边缘上使用双阈值去除假阳性
20         5 分析所有边缘连接 消除不明显的边缘
21 """
22 
23 minLineLength = 20
24 maxLineGap = 5
25 lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)
26 """
27 cv2.HoughLinesP
28     作用:标准霍夫线变换, 找到图像中的所有直线
29     参数:
30         1 二值图
31         2 半径精度
32         3 角度精度
33         4 最短检测长度
34         5 允许的最大缺口
35     返回:
36         一个列表,每一项是一个四元组,分别是直线两个端点的坐标
37 """
38 for line in lines:
39     for x1, y1, x2, y2 in line:
40         # 在图片上画直线
41         cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
42 
43 cv2.imshow("edges", edges)
44 cv2.imshow("lines", img)
45 cv2.waitKey()
46 cv2.destroyAllWindows()

原文地址:https://www.cnblogs.com/Lin-Yi/p/9413173.html