首页 > 解决方案 > 无法检测到车道中间

问题描述

我能够检测到车道,但我还必须检测到车道的中间/中心,即两条线的中心/中间。我尝试了 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()

截屏

标签: pythonopencvhough-transform

解决方案


我不知道,如果你能用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

推荐阅读