首页 > 解决方案 > 在这个例子中如何使用 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']

如果有人可以告诉我出了什么问题并帮助我,我将不胜感激。

标签: pythonrobotics

解决方案


推荐阅读