1

我是初学者,我正在尝试在游戏中进行一些线路检测。 这是我试图检测车道的照片 这是结果 HoughLinesP 代码:```lines = cv2.HoughLinesP(cropped_image, 2, np.pi / 180, 100, np.array([]), minLineLength=50,maxLineGap=5)

# The displaying function:
def displayLines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1, x2, y1, y2 = line.reshape(4)
            cv2.line(line_image, (x1, x2), (x2, y2), (0,255,0), 10)
    return line_image```
# Here is the cropping function: 
def region(image):
    height = image.shape[0]
    polygons = np.array([[
        (570, 640), (1600, 700), (863, 520)
    ]])
    mask = np.zeros_like(image)
    cv2.fillPoly(mask, polygons, 255)
    masked_image = cv2.bitwise_and(canny, mask)
    return masked_image
#As input I'm giving image with edges displayed. Function:
def canny(image):
    gray = cv2.cvtColor(lane_image, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5),0)
    canny = cv2.Canny(blur, 50, 150)
    return canny

我不知道是什么问题

4

1 回答 1

0

我建议您在尝试检测线条之前屏蔽掉噪音:

import cv2
import numpy as np

img = cv2.imread("driving_game.jpg")

img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([18, 120, 200])
upper = np.array([30, 255, 255])
mask = cv2.inRange(img_hsv, lower, upper)
img_masked = cv2.bitwise_and(img, img, mask=mask)

cv2.imshow("Mask", mask)
cv2.imshow("Masked Image", img_masked)
cv2.waitKey(0)

输出:

在此处输入图像描述

在此处输入图像描述

在哪里

lower = np.array([18, 120, 200])
upper = np.array([30, 255, 255])

是 HSV 颜色掩码的下限值和上限值。使用上面的掩码,您甚至不需要该cv2.HoughLinesP方法;您可以简单地检测非遮罩对象的轮廓并近似结果:

import cv2
import numpy as np

def process(img):
    img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    lower = np.array([18, 120, 200])
    upper = np.array([30, 255, 255])
    mask = cv2.inRange(img_hsv, lower, upper)
    mask_canny = cv2.Canny(mask, 50, 50)
    kernel = np.ones((2, 2))
    img_dilate = cv2.dilate(mask_canny, kernel, iterations=7)
    return cv2.erode(img_dilate, kernel, iterations=7)

def draw_lines(img):
    contours, _ = cv2.findContours(process(img), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    for cnt in contours:
        peri = cv2.arcLength(cnt, True)
        approx = cv2.approxPolyDP(cnt, 0.13 * peri, True)
        cv2.drawContours(img, [approx], -1, (0, 0, 255), 5)

img = cv2.imread("driving_game.jpg")
draw_lines(img)

cv2.imshow("Lines Detected", img)
cv2.waitKey(0)

输出:

在此处输入图像描述

于 2021-05-14T17:14:54.363 回答