import cv2
import matplotlib.pyplot as plt
import numpy as np# 读取图像
image = cv2.imread('./data/1.png')
if image is None:print("无法读取图像文件")
else:# 转换为灰度图像gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)# 使用Canny边缘检测edges = cv2.Canny(gray, 50, 150, apertureSize=3)# 使用概率霍夫变换检测线段lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi / 180, threshold=100, minLineLength=100, maxLineGap=10)# 绘制检测到的线段if lines is not None:for line in lines:x1, y1, x2, y2 = line[0]cv2.line(image, (x1, y1), (x2, y2), (0, 255, 0), 2)# 显示结果plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))plt.axis('off') # 不显示坐标轴plt.show()