OpenCV 实时灰度赛道识别: 方向判断、中心线绘制和偏转角计算
以下是基于 OpenCV 实现实时灰度处理和赛道识别的 Python 代码:
import cv2
import numpy as np
# 设置灰色区域的阈值
lower_gray = np.array([50, 50, 50])
upper_gray = np.array([200, 200, 200])
# 设置摄像头偏移角度的阈值
angle_threshold = 10
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取摄像头的帧
ret, frame = cap.read()
# 将帧转换为灰度图像
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 进行灰度处理
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 将灰度图像转换为二值图像
_, binary = cv2.threshold(gray, 128, 255, cv2.THRESH_BINARY)
# 对图像进行腐蚀和膨胀操作,以去除噪声
kernel = np.ones((5,5), np.uint8)
binary = cv2.erode(binary, kernel, iterations=1)
binary = cv2.dilate(binary, kernel, iterations=1)
# 找出灰色赛道的轮廓
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 初始化赛道中心位置和偏转角度
center_x = 0
deviation_angle = 0
if len(contours) > 0:
# 找到最大的轮廓
max_contour = max(contours, key=cv2.contourArea)
# 计算最大轮廓的凸包
hull = cv2.convexHull(max_contour)
# 找到凸包的最左和最右点
leftmost = tuple(hull[hull[:,:,0].argmin()][0])
rightmost = tuple(hull[hull[:,:,0].argmax()][0])
# 计算赛道中心位置
center_x = (leftmost[0] + rightmost[0]) // 2
# 计算摄像头偏离赛道中心线的偏转角度
deviation_angle = np.arctan((center_x - frame.shape[1]//2) / (frame.shape[0]//2)) * 180 / np.pi
# 绘制赛道中心线
cv2.line(frame, (center_x, 0), (center_x, frame.shape[0]), (0, 255, 0), 2)
# 判断偏转角度并显示在屏幕上
if np.abs(deviation_angle) < angle_threshold:
direction = "Straight"
elif deviation_angle < 0:
direction = "Turn Left"
else:
direction = "Turn Right"
cv2.putText(frame, "Deviation Angle: {:.2f}".format(deviation_angle), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
cv2.putText(frame, "Direction: {}".format(direction), (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
# 分屏显示灰度处理下的图像和原始图像
cv2.imshow("Gray", gray)
cv2.imshow("Original", frame)
# 按下"q"键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头和关闭窗口
cap.release()
cv2.destroyAllWindows()
代码首先通过 cap = cv2.VideoCapture(0) 打开摄像头,然后通过 cap.read() 读取摄像头的帧。接下来,将帧转换为灰度图像,并进行灰度处理。然后,将灰度图像转换为二值图像,并进行腐蚀和膨胀操作,以去除噪声。之后,使用 cv2.findContours() 函数找出灰色赛道的轮廓,并找到最大轮廓的凸包。根据凸包的最左和最右点,计算赛道的中心位置,并根据中心位置和图像尺寸计算摄像头偏离赛道中心线的偏转角度。最后,根据偏转角度判断赛车的行驶方向,并将偏转角度和行驶方向显示在屏幕上。同时,使用 cv2.putText() 函数在图像上绘制赛道中心线。
在循环中还使用了 cv2.imshow() 函数分屏显示灰度处理下的图像和原始图像。按下"q"键可以退出循环。
注意:在运行代码之前,请确保已经安装了 OpenCV 库,并且已经连接了摄像头。
原文地址: https://www.cveoy.top/t/topic/o8po 著作权归作者所有。请勿转载和采集!