首页 > 解决方案 > OpenCV 车道检测和着色

问题描述

我有一张道路图片https://imgur.com/a/whT90Yp并且我想检测车道并为两侧着色,就像这样https://i.stack.imgur.com/1NuD8.jpg,我做了一个代码在openCV中,但它不起作用,我不知道为什么:(

这是代码:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def lanesDetection(img):
im = cv.imread("highway4.jpg")
im = cv.cvtColor(im, cv.COLOR_BGR2RGB)

# print(img.shape)
height = img.shape[0]
width = img.shape[1]

region_of_interest_vertices = [
    (200, height), (width/2, height/1.37), (width-300, height)
]
gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
edge = cv.Canny(gray_img, 50, 100, apertureSize=3)
cropped_image = region_of_interest(
    edge, np.array([region_of_interest_vertices], np.int32))

lines = cv.HoughLinesP(cropped_image, rho=2, theta=np.pi/180,
                       threshold=50, lines=np.array([]), minLineLength=10, maxLineGap=30)
image_with_lines = draw_lines(img, lines)
# plt.imshow(image_with_lines)
# plt.show()
return image_with_lines


def region_of_interest(img, vertices):
mask = np.zeros_like(img)
# channel_count = img.shape[2]
match_mask_color = (255)
cv.fillPoly(mask, vertices, match_mask_color)
masked_image = cv.bitwise_and(img, mask)
return masked_image


def draw_lines(img, lines):
img = np.copy(img)
blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

for line in lines:
    for x1, y1, x2, y2 in line:
        cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0)
return img


cap.release()
cv.destroyAllWindows()

你能帮我修一下吗?

标签: pythonopencvobject-detection

解决方案


我拿走了你的代码并让它运行起来:我做的一些事情是:

  • 修复了文件中空格和制表符的混合使用
  • 更改函数以接受文件名(您已将其硬编码)
  • 在末尾添加了 7 行(您的代码没有调用函数或显示结果图像)
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt


def lanesDetection(imgname):
    img = cv.imread(imgname)
    img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

# print(img.shape)
    height = img.shape[0]
    width = img.shape[1]

    region_of_interest_vertices = [(200, height), (width/2, height/1.37), (width-300, height)]
    gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
    edge = cv.Canny(gray_img, 50, 100, apertureSize=3)
    cropped_image = region_of_interest(edge, np.array([region_of_interest_vertices], np.int32))

    lines = cv.HoughLinesP(cropped_image, rho=2, theta=np.pi/180,threshold=50, lines=np.array([]), minLineLength=10, maxLineGap=30)
    image_with_lines = draw_lines(img, lines)
# plt.imshow(image_with_lines)
# plt.show()
    return image_with_lines

def blah(imgname):
    blahb = cv.imread(imgname)
    return blahb

def region_of_interest(img, vertices):
    mask = np.zeros_like(img)
# channel_count = img.shape[2]
    match_mask_color = (255)
    cv.fillPoly(mask, vertices, match_mask_color)
    masked_image = cv.bitwise_and(img, mask)
    return masked_image


def draw_lines(img, lines):
    img = np.copy(img)
    blank_image = np.zeros((img.shape[0], img.shape[1], 3), np.uint8)

    for line in lines:
        for x1, y1, x2, y2 in line:
            cv.line(blank_image, (x1, y1), (x2, y2), (0, 255, 0), 2)

    img = cv.addWeighted(img, 0.8, blank_image, 1, 0.0)
    return img


#cap.release()
#cv.destroyAllWindows()

pimage = lanesDetection("highway4.jpg")

while 1:
    cv.imshow("Image",pimage)
    key = cv.waitKey(0) & 0xFF
    if key == ord("q"):
        break

opencv道路中心线检测后处理图像


推荐阅读