帮我写一段python代码识别图像中的道路
时间: 2023-03-13 12:26:52 浏览: 111
简单路标识别代码
我可以为你提供一段Python代码,它可以识别图像中的道路:import cv2
import numpy as np # 读取图像
img = cv2.imread('road.jpg') # 创建灰度图像
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 创建滤波器
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0) # 创建Canny边缘检测滤波器
low_threshold = 50
high_threshold = 150
edges = cv2.Canny(blur_gray, low_threshold, high_threshold) # 运行Hough变换
rho = 1
theta = np.pi/180
threshold = 15
min_line_length = 50
max_line_gap = 20
line_image = np.copy(img)*0 # 创建一个与原图大小相同的全黑图像 # 在Hough空间中检测直线
lines = cv2.HoughLinesP(edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap) # 在绘制直线的图像上绘制直线
for line in lines:
for x1,y1,x2,y2 in line:
cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),5) # 将检测到的直线覆盖到原图上
lines_edges = cv2.addWeighted(img, 0.8, line_image, 1, 0) # 显示结果
cv2.imshow('Result Image', lines_edges)
cv2.waitKey(0)
阅读全文