python - 在这个例子中如何使用 OpenCV?
问题描述
我一直在尝试使用我的 Robobo(机器人)进行线路检测,但我被卡住了。这是我正在使用的代码(它也是在教程(https://github.com/mintforpeople/robobo-programming/wiki/Lane-detection-library)上发布的相同代码帽:
import cv2
import numpy as np
from robobo_video.robobo_video import RoboboVideo
from robobopy.Robobo import Robobo
def draw_line_pro(coeffs1, coeffs2, M, frame):
# Get height and width to the frame
try:
height, width, _ = frame.shape
# Mask for draw the lines
mask = np.zeros_like(frame)
# Create an Array with numbers to 0 at height -1
plot_y = np.linspace(0, height - 1, height)
# Arrays evaluating the coefficients
left_x = coeffs1['a'] * plot_y 2 \
+ coeffs1['b'] * plot_y \
+ coeffs1['c']
right_x = coeffs2['a'] * plot_y 2 + \
coeffs2['b'] * plot_y + \
coeffs2['c']
# Draw the lanes (blue for left lane and red for right lane)
cv2.polylines(mask, [np.int32(np.stack((left_x, plot_y), axis=1))], False, (255, 0, 0), 20)
cv2.polylines(mask, [np.int32(np.stack((right_x, plot_y), axis=1))], False, (0, 0, 255), 20)
# Warp the perspective
mask = cv2.warpPerspective(mask, np.float32(M), (width, height))
# Add the lane to the original frame
img = cv2.addWeighted(frame, 1., mask, 0.5, 0)
except ValueError:
pass
return img
IP = '...'
rob = Robobo(IP)
rob.connect()
rob.moveTiltTo(120, 50)
video = RoboboVideo(IP)
video.connect()
rob.setLaneColorInversion(False)
rob.startLaneDetection()
while True:
frame = video.getImage()
# obtein the coefficients to the left and right lane and the reverse transformation matrix
obj = rob.readLanePro()
frame = draw_line_pro(obj.coeffs1, obj.coeffs2, obj.minv, frame)
# show the frame draw
cv2.imshow('smartphone camera', frame)
# if you press the key 'q', the script ends
if cv2.waitKey(1) & 0xFF == ord('q'):
video.disconnect()
cv2.destroyAllWindows()
break
rob.stopLaneDetection()
当我运行它时,线条一直在飞来飞去,我不知道出了什么问题,但我认为这行代码有问题:
left_x = coeffs1['a'] * plot_y 2 \
+ coeffs1['b'] * plot_y \
+ coeffs1['c']
right_x = coeffs2['a'] * plot_y 2 + \
coeffs2['b'] * plot_y + \
coeffs2['c']
如果有人可以告诉我出了什么问题并帮助我,我将不胜感激。
解决方案
推荐阅读
- javascript - 在条件问答表单中添加下一步按钮
- javascript - 如何在本地运行?
- core-data - SwiftUI 核心数据预览不能跨平台工作
- java - 如何在 Spring Batch 中为 JSON 文件创建阅读器
- reactjs - ReactJS + Firestore - 使用文档分页似乎不起作用
- google-bigquery - BigQuery 存储过程以什么用户身份运行?
- twitter-bootstrap - 只有其他所有链接在 Bootstrap 5 弹出窗口中都能正常工作
- html - 在部分内随机排序/排序项目
- python - 一列文本之间的空间余弦相似度
- javascript - 所以我正在尝试使用 Math.cbrt 并且我的代码没有按预期工作