You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现LKAS时Hough Line Transform报错,寻求技术解决方案

解决OpenCV Hough变换调用时的Assertion错误(LKAS实现问题)

错误信息

cv2.error: OpenCV(4.6.0) /io/opencv/modules/highgui/src/precomp.hpp:155: error: (-215:Assertion failed) src_depth != CV_16F && src_depth != CV_32S in function 'convertToShow'

问题背景

尝试用OpenCV实现车道保持辅助系统(LKAS),调用Hough变换函数时触发上述错误,原代码如下:

import cv2
import numpy as np

def detect_line_segments(frame):
    # tuning min_threshold, minLineLength, maxLineGap is a trial and error process by hand
    rho = 1  # distance precision in pixel, i.e. 1 pixel
    angle = np.pi / 180  # angular precision in radian, i.e. 1 degree
    min_threshold = 2  # minimal of votes
    # frame = frame.astype(np.uint8)
    line_segments = cv2.HoughLinesP(frame, rho, angle, min_threshold, minLineLength=8, maxLineGap=4)
    return line_segments

def detectEdges(frame):

    rho = 1
    angle = np.pi / 180
    min_threshold = 10
    
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_blue = np.array([60, 40, 40])
    upper_blue = np.array([150, 255, 255])
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    mask = cv2.resize(mask, (960, 540))

    # cv2.imshow('Test', mask)
    # cv2.waitKey(0)

    edges = cv2.Canny(mask, 200, 400)
    #edgesUpdt = np.array(edges, dtype=np.uint8)
    # cv2.imshow('Test', edgesUpdt)
    # cv2.waitKey(0)
    return edges

def region_of_interest(edges):
    print(edges)
    height, width = edges.shape
    mask = np.zeros_like(edges)

    # only focus bottom half of the screen
    polygon = np.array([[
        (0, height * 1 / 2),
        (width, height * 1 / 2),
        (width, height),
        (0, height),
    ]], np.int32)

    cv2.fillPoly(mask, polygon, 255)
    cropped_edges = cv2.bitwise_and(edges, mask)
    cv2.imshow('Test', cropped_edges)
    cv2.waitKey(0)
    return cropped_edges

def detect_line_segments(cropped_edges):
    # cropped_edges = cropped_edges.astype(np.float32)
    cv2.imshow('Test', cropped_edges)
    cv2.waitKey(0)
    # tuning min_threshold, minLineLength, maxLineGap is a trial and error process by hand
    rho = 2  # distance precision in pixel, i.e. 1 pixel
    angle = np.pi / 60  # angular precision in radian, i.e. 1 degree
    min_threshold = 50  # minimal of votes
    line_segments = cv2.HoughLinesP(cropped_edges, rho, angle, min_threshold, 
                                    np.array([], dtype=np.uint8), minLineLength=40, maxLineGap=80)
    cv2.imshow('Test', line_segments)
    cv2.waitKey(0)
    return line_segments

def main():
    frame = cv2.imread(r'/home/a1ph4/Desktop/LKAS system/Media/image.jpg')
    edges = detectEdges(frame)
    # test1 = region_of_interest(edges)
    croppedEdges = detect_line_segments(edges)
    

if __name__ == '__main__':
    main()

错误原因

  1. 错误的图像显示操作:cv2.HoughLinesP返回的line_segments是存储线段坐标的int32类型数组,并非可直接显示的图像格式,直接用cv2.imshow()会触发类型断言错误。
  2. 重复定义函数:原代码中存在两个同名的detect_line_segments函数,会导致函数覆盖,逻辑混乱。
  3. 流程缺失:main函数跳过了region_of_interest裁剪步骤,不符合车道检测的正常流程。

修复后的代码

import cv2
import numpy as np

def detectEdges(frame):
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_blue = np.array([60, 40, 40])
    upper_blue = np.array([150, 255, 255])
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    mask = cv2.resize(mask, (960, 540))
    edges = cv2.Canny(mask, 200, 400)
    return edges

def region_of_interest(edges):
    height, width = edges.shape
    mask = np.zeros_like(edges)
    # 只关注屏幕下半部分的区域
    polygon = np.array([[
        (0, height * 1 / 2),
        (width, height * 1 / 2),
        (width, height),
        (0, height),
    ]], np.int32)
    cv2.fillPoly(mask, polygon, 255)
    cropped_edges = cv2.bitwise_and(edges, mask)
    return cropped_edges

def detect_line_segments(cropped_edges):
    rho = 2  # 像素距离精度
    angle = np.pi / 60  # 角度精度(弧度)
    min_threshold = 50  # 最小投票数
    line_segments = cv2.HoughLinesP(cropped_edges, rho, angle, min_threshold, 
                                    np.array([], dtype=np.uint8), minLineLength=40, maxLineGap=80)
    return line_segments

def draw_lanes(frame, line_segments):
    # 在原图像上绘制检测到的车道线
    if line_segments is not None:
        for segment in line_segments:
            x1, y1, x2, y2 = segment[0]
            cv2.line(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
    cv2.imshow('Detected Lanes', frame)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

def main():
    frame = cv2.imread(r'/home/a1ph4/Desktop/LKAS system/Media/image.jpg')
    edges = detectEdges(frame)
    cropped_edges = region_of_interest(edges)
    line_segments = detect_line_segments(cropped_edges)
    draw_lanes(frame, line_segments)

if __name__ == '__main__':
    main()

修复说明

  1. 删除了尝试显示线段数据的错误代码,改用draw_lanes函数在原图像上绘制车道线,正确展示检测结果。
  2. 移除重复定义的detect_line_segments函数,保证逻辑唯一。
  3. 修正main函数流程:边缘检测→感兴趣区域裁剪→线段检测→结果绘制,符合车道检测的标准流程。

内容的提问来源于stack exchange,提问作者Kamau Samuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 21:54:14