OpenCV C++版本 车道检测 https://blog.youkuaiyun.com/francislucien2017/article/details/83443639
lane detection.py
import os
import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import QApplication
import GUI
# pre-define function
def grayscale(img):
return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def canny(img, low_threshold, high_threshold):
return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(img, kernel_size):
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
# crop the region of interest
def ROI(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_img = cv2.bitwise_and(img, mask)
return masked_img
def draw_lines(img, lines, color=[255,0,0], thickness=2):
for line in lines:
for x1, y1, x2, y2 in line:
cv2.line(img, (x1, y1), (x2, y2), color, thickness)
def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]),
minLineLength=min_line_len, maxLineGap=max_line_gap)
return lines
# angle filter
def bypass_angle_filter(lines, low_thres, hi_thres):
filtered_lines = []
for line in lines:
for x1, y1, x2, y2 in line:
angle = abs(np.arctan((y2-y1)/(x2-x1+