import cv2
import numpy as np
cv2.namedWindow("line", cv2.WINDOW_NORMAL)
cv2.namedWindow("edges", cv2.WINDOW_NORMAL)
cv2.resizeWindow("line", 640, 480)
cv2.resizeWindow("edges", 640, 480)
img = cv2.imread("D:/shijue/cl.jpg")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#变为灰度图
gray = cv2.GaussianBlur(gray, (3,3), 0)#高斯滤波
edges = cv2.Canny(gray, 30, 200)#Canny边缘检测
min =5
max = 100
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, min, max)#小于最小的被消除,大于最大的被视为两条线段
#print(lines)
for i in range(0, 8):
for x1, y1, x2, y2 in lines[i]:
cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)#绘制直线
cv2.imshow("edges", edges)
cv2.imshow("line", img)
cv2.waitKey()
cv2.destroyAllWindows()