import cv2
import numpy as np
img = cv2.imread("../chapter3/qipan.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 120)
min_line_lenghth = 200
max_line_gap = 30
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, min_line_lenghth, max_line_gap)
print(len(lines))
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("edges", edges)
cv2.imshow("lines", img)
cv2.waitKey()
cv2.destroyAllWindows()
- cv2.HoughLinesP()参数是二值图,r的精度(一般为1),theta的精度(一般为3.14/180), 阈值(越高检测到的直线越少), 最段直线长度, 共线直线的最小间隔
![在这里插入图片描述](https://img-blog.csdnimg.cn/49a80961023c45e0a1b10d1f52d6dfe4.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBAempMT1ZFY3lq,size_20,color_FFFFFF,t_70,g_se,x_16)