【问题标题】:Opencv python lane detection draw line using cv2.line() change line color bug unsuccessfullyOpencv python车道检测绘制线使用cv2.line()更改线颜色错误未成功
【发布时间】:2020-07-01 10:11:54
【问题描述】:

我在这里呆了很长一段时间。我正在使用 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 值,我得到的是,无论我如何更改 R B G 值,它只绘制灰度颜色的线条。我试图通过简单地创建一个黑色图像然后用不同颜色绘制线条来使用另一个 .py 文件测试cv2.line() 函数,它工作得很好。 我希望有人能给我暗示哪里可能出错了。先谢谢了!

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)

【问题讨论】:

  • 如果您转换为灰色或创建黑白图像 (np.zeros_like(image)) 则无法绘制颜色 - 您必须将图像转换为 BGR (RGB)

标签: python numpy opencv


【解决方案1】:

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

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

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

    # ... code ... 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-28
    • 2019-09-17
    • 2018-01-09
    • 2011-08-31
    • 2020-06-09
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多