簡體   English   中英

Opencv python 車道檢測畫線使用 cv2.line() 更改線顏色錯誤不成功

[英]Opencv python lane detection draw line using cv2.line() change line color bug unsuccessfully

我在這里呆了很長一段時間。 我正在使用 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 BG 值,它只繪制灰度顏色的線條。 我試圖通過簡單地創建一個黑色圖像然后用不同的 colors 繪制線條來使用 another.py 文件測試cv2.line() function,它工作得很好。 我希望有人能給我一個提示,說明哪里可能出錯。 提前謝謝!

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)

如果您轉換為GRAY或創建B&W圖像( np.zeros_like(image) ),那么您無法繪制 colors - 首先您必須將圖像轉換為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 ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM