首页 > 解决方案 > Opencv python车道检测绘制线使用cv2.line()更改线颜色错误未成功

问题描述

我在这里呆了很长一段时间。我正在使用 opencv python 进行车道检测,该程序可以检测线条并将其绘制在黑色蒙版图像上。但是,只有白色。根据文档,如果我理解正确,那么如果是蓝线,它应该是cv2.line(mask_image, firstpoint_line, seconpoint_line, (255, 0, 0), 5). 然而,我的程序将以白色而不是蓝色绘制正确的线条。然后,如果我尝试使用 更改为红线cv2.line(mask_image, (x1, y1), (x2, y2), (0, 0, 255), 5),它不会画任何线,我猜它确实画了线,但是颜色为黑色,黑色蒙版图像上的黑线则看不到任何线。我玩过 RGB 值,我得到的是,无论我如何更改 RBG 值,它只绘制灰度颜色的线条。我试图测试cv2.line()通过简单地创建一个黑色图像然后绘制不同颜色的线条来使用另一个 .py 文件,它工作得很好。我希望有人能给我一个提示,说明哪里可能出错。提前谢谢!

import cv2
import numpy as np
import matplotlib.pyplot as plt

'''
Canny edge detection
'''
def canny(image):
    # RGB to Gray
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    # apply a 5 by 5 gussian blur kernel on grayscale image
    blur = cv2.GaussianBlur(gray,(5, 5), 0)
    # apply canny operator for edge detection
    canny = cv2.Canny(blur, 50, 150)
    return canny
'''
Create mask on the canny image to only show the region of inteste (lane to be tracked)
'''
def region_to_track(image):
    # get the height of the image
    height = image.shape[0]
    # create a region with coordinates as a numpy array
    # in this case we only need one region (one list)
    region = np.array([
        [(200, height), (1200, height), (550, 250)]
        ])
    # create mask as a full black image (all zeros) with the same size of our image
    mask = np.zeros_like(image)
    # full mask with region(full white). Be aware that fillPoly() takes more than one regions
    cv2.fillPoly(mask, region, 255)
    # implement bitwise-& operation using mask on the edge image, ultimately masking the edge image only show the lane region 
    masked_image = cv2.bitwise_and(image, mask)
    return masked_image
'''
draw the detected line on the image
'''
def display_lines(image, lines):
    # black image surface
    line_image = np.zeros_like(image)
    # if line detected
    if lines is not None:
        # loop through the lines
        for line in lines:
            # return of HoughLinesP() is a 2D array (n row and 1 column). each row is a line determined by 2 points
            # unpack the 2D array
            x1, y1, x2, y2 = line.reshape(4)
            # draw line on black image surface
            # usage: cv2.line(image, firstpoint, secondpoint, color of line, line thickness)
            cv2.line(line_image, (x1, y1), (x2, y2), (255, 0, 0), 5)
    return line_image

# load image as a numpy array object 
image = cv2.imread('test_image.jpg')
# set new vaiable for image processing
lane_image = np.copy(image)
canny = canny(lane_image)
# get masked image with only lane region
lane_region = region_to_track(canny)
# find line using hough transformation
# HoughLinesP() taks two arguments about the resolution of the grid of Hough space (rho in pixels, theta in radian). Here, 1 pixels and 1 degree resolution. 
lines = cv2.HoughLinesP(lane_region, 1, np.pi/180, 120, np.array([]), minLineLength=30, maxLineGap=5)
detected_lines_image = display_lines(lane_region, lines)
# show image
cv2.imshow("output_image", detected_lines_image)
# show the opened window till keyboard input detected
cv2.waitKey(0)

标签: pythonnumpyopencv

解决方案


如果您转换为GRAY或创建了B&W图像 ( np.zeros_like(image)),那么您将无法绘制颜色 - 首先您必须将图像转换为BGR( RGB)

def display_lines(image, lines):
    # black image surface
    line_image = np.zeros_like(image)

    line_image = cv2.cvtColor(line_image, cv2.COLOR_GRAY2BGR)

    # ... code ... 

推荐阅读