关于python:毕业设计-车道线检测自动驾驶-机器视觉

33次阅读

共计 3621 个字符,预计需要花费 10 分钟才能阅读完成。

mask = np.zeros_like(edges)

ignore_mask_color = 255 
# 获取图片尺寸
imshape = img.shape
# 定义 mask 顶点
vertices = np.array([[(0,imshape[0]),(450, 290), (490, 290), (imshape[1],imshape[0])]], dtype=np.int32)
# 应用 fillpoly 来绘制 mask
cv2.fillPoly(mask, vertices, ignore_mask_color)
masked_edges = cv2.bitwise_and(edges, mask)
# 定义 Hough 变换的参数
rho = 1 
theta = np.pi/180
threshold = 2
min_line_length = 4 # 组成一条线的最小像素数
max_line_gap = 5    # 可连接线段之间的最大像素间距
# 创立一个用于绘制车道线的图片
line_image = np.copy(img)*0 
# 对于 canny 边缘检测后果利用 Hough 变换
# 输入“线”是一个数组,其中蕴含检测到的线段的端点
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                            min_line_length, max_line_gap)
# 遍历“线”的数组来在 line_image 上绘制
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
color_edges = np.dstack((edges, edges, edges)) 

import math
import cv2
import numpy as np
“””
Gray Scale
Gaussian Smoothing
Canny Edge Detection
Region Masking
Hough Transform
Draw Lines [Mark Lane Lines with different Color]
“””
class SimpleLaneLineDetector(object):

def __init__(self):
    pass
def detect(self,img):
    # 图像灰度解决
    gray_img = self.grayscale(img)
    print(gray_img)
    #图像高斯平滑解决
    smoothed_img = self.gaussian_blur(img = gray_img, kernel_size = 5)
    #canny 边缘检测
    canny_img = self.canny(img = smoothed_img, low_threshold = 180, high_threshold = 240)
    #区域 Mask
    masked_img = self.region_of_interest(img = canny_img, vertices = self.get_vertices(img))
    #霍夫变换
    houghed_lines = self.hough_lines(img = masked_img, rho = 1, theta = np.pi/180, threshold = 20, min_line_len = 20, max_line_gap = 180)
    # 绘制车道线
    output = self.weighted_img(img = houghed_lines, initial_img = img, alpha=0.8, beta=1., gamma=0.)
    return output
def grayscale(self,img):
    return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
def canny(self,img, low_threshold, high_threshold):
    return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(self,img, kernel_size):
    return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(self,img, vertices):
    mask = np.zeros_like(img)   
    if len(img.shape) > 2:
        channel_count = img.shape[2]  
        ignore_mask_color = (255,) * channel_count
    else:
        ignore_mask_color = 255
    cv2.fillPoly(mask, vertices, ignore_mask_color)
    masked_image = cv2.bitwise_and(img, mask)
    return masked_image
def draw_lines(self,img, lines, color=[255, 0, 0], thickness=10):
    for line in lines:
        for x1,y1,x2,y2 in line:
            cv2.line(img, (x1, y1), (x2, y2), color, thickness)
def slope_lines(self,image,lines):
    img = image.copy()
    poly_vertices = []
    order = [0,1,3,2]
    left_lines = [] 
    right_lines = [] 
    for line in lines:
        for x1,y1,x2,y2 in line:
            if x1 == x2:
                pass 
            else:
                m = (y2 - y1) / (x2 - x1)
                c = y1 - m * x1
                if m < 0:
                    left_lines.append((m,c))
                elif m >= 0:
                    right_lines.append((m,c))
    left_line = np.mean(left_lines, axis=0)
    right_line = np.mean(right_lines, axis=0)
    for slope, intercept in [left_line, right_line]:
        rows, cols = image.shape[:2]
        y1= int(rows) 
        y2= int(rows*0.6)
        x1=int((y1-intercept)/slope)
        x2=int((y2-intercept)/slope)
        poly_vertices.append((x1, y1))
        poly_vertices.append((x2, y2))
        self.draw_lines(img, np.array([[[x1,y1,x2,y2]]]))
    poly_vertices = [poly_vertices[i] for i in order]
    cv2.fillPoly(img, pts = np.array([poly_vertices],'int32'), color = (0,255,0))
    return cv2.addWeighted(image,0.7,img,0.4,0.)
def hough_lines(self,img, rho, theta, threshold, min_line_len, max_line_gap):
    lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, [Skrill 下载](https://www.gendan5.com/wallet/Skrill.html)maxLineGap=max_line_gap)
    line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    line_img = self.slope_lines(line_img,lines)
    return line_img
def weighted_img(self,img, initial_img, alpha=0.1, beta=1., gamma=0.):
    lines_edges = cv2.addWeighted(initial_img, alpha, img, beta, gamma)
    return lines_edges
def get_vertices(self,image):
    rows, cols = image.shape[:2]
    bottom_left  = [cols*0.15, rows]
    top_left     = [cols*0.45, rows*0.6]
    bottom_right = [cols*0.95, rows]
    top_right    = [cols*0.55, rows*0.6] 
    ver = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32)
    return ver

正文完
 0