import cv2
import os
import numpy as np
image_path="自己的文件路径/,图像后缀"
depth_image_ = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
depth_image = np.copy(depth_image_)
edges = cv2.Canny(depth_image, 50, 100, apertureSize=3)
lines = cv2.HoughLines(edges, 1, np.pi / 180, threshold=55)
# 筛选斜率较大的直线,并在原始图像上绘制
if lines is not None:
for line in lines:
rho, theta = line[0]
a = np.cos(theta)
b = np.sin(theta)
# 计算斜率,避免除零错误
if b != 0:
slope = -a / b
# 筛选斜率大于某个阈值的直线
if slope >- 0.1 and slope<0: # 修改斜率阈值为0.5或其他适当值
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
#在(x1,y1) 到(x2,y2)之间画一条直线
cv2.line(depth_image_, (x1, y1), (x2, y2), 100000, 2)
# 显示结果
cv2.imshow('Hough Lines',depth_image_)
cv2.waitKey(0)
cv2.destroyAllWindows()
对图片进行边缘检测+霍夫直线检测
于 2023-11-09 17:02:55 首次发布