【问题标题】:Unable to detect the middle of the lane无法检测到车道中间
【发布时间】:2021-09-22 22:44:47
【问题描述】:

我能够检测到车道,但我还必须检测到车道的中间/中心,即两条线的中心/中间。我尝试了 x1 和 x2 的中间,但它没有用。我想要第三条线,即中间线。我还附上了截图

  def houghLines(cropped_canny):
    return cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100, 
        np.array([]), minLineLength=70, maxLineGap=5)

def addWeighted(frame, line_image):
    return cv2.addWeighted(frame, 0.8, line_image, 1, 1)
 
def display_lines(img,lines):
    line_image = np.zeros_like(img)
    if lines is not None:
        print(len(lines))
        for line in lines:
            for x1, y1, x2, y2 in line:
                #print(len(lines))
                cv2.line(line_image,(x1,y1),(x2,y2),(0,0,255),5)
    return line_image



cap = cv2.VideoCapture("videoplayback (online-video-cutter.com) (1).mp4")
while(cap.isOpened()):
    _, frame = cap.read()
    canny_image = canny(frame)
    cropped_canny = region_of_interest(canny_image)
    #cv2.imshow("cropped_canny",cropped_canny)

    lines = houghLines(cropped_canny)
    #averaged_lines = average_slope_intercept(frame, lines)
    line_image = display_lines(frame, lines)
    combo_image = addWeighted(frame, line_image)
    
    
    cv2.imshow("result", combo_image)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

  • 你用什么等式从两条红线中得到中间线?您应该从端点获取线的方程并组合方程(组合斜率和组合截距)以获得中间线。

标签: python opencv hough-transform


【解决方案1】:

我不知道,如果你能用 opencv 以某种方式检测到它,但你可以通过平均每条线的 (x1, y1) 和 (x2, y2) 坐标来计算它。

例如:

def display_midline(img, lines):
    line_image = np.zero_like(img)
    x1_avg, y1_avg = (0, 0)
    x2_avg, y2_avg = (0, 0)
    if lines is not None:
        for line in lines:
            for x1, y1, x2, y2 in line:
                x1_avg += x1
                y1_avg += y1
                x2_avg += x2
                y2_avg += y2
        x1_avg = int(x1_avg / len(lines))
        y1_avg = int(y1_avg / len(lines))
        x2_avg = int(x2_avg / len(lines))
        y2_avg = int(y2_avg / len(lines))
        cv2.line(line_image, (x1_avg, y1_avg), (x2_avg, y2_avg), (0, 0, 255), 5)
    return line_image

【讨论】:

    猜你喜欢
    • 2016-04-07
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2016-08-04
    相关资源
    最近更新 更多