本文根据 udacity Self-Driving Car Engineer 课程内容整理
如何从一张照片,比如从车辆的前置摄像头的照片中识别出车道线?
比如这样的
 
 
相当于要把在照片中特定位置的那些黄线或者白线找出来,同时过滤掉一些干扰。
首先,我们要把黄色或者白色过滤出来。
可以把黄色或者白色分开处理。
现找黄颜色,再找白颜色。
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
# Read in the image and print out some stats
image = mpimg.imread('test.jpg')
print('This image is: ',type(image), 'with dimensions:', image.shape)
# Grab the x and y size and make a copy of the image
ysize = image.shape[0]
xsize = image.shape[1]
color_select = np.copy(image) # Note: always make a copy rather than simply using "="
注意这里读入了之后是 [y, x, 3] r_t, g_t, b_t = 220, 220, 220 mask = (image[:, :, 0]) < r_t | (image[:, :, 1] < g_t) | (image[:, :, 2] < b_t ) color_select[mask] = 0 plt.imshow(color_select) plt.show()
left_bottom = [0, 539]
right_bottom = [900, 300]
apex = [400, 0]
# Fit lines (y=Ax+B) to identify the  3 sided region of interest
# np.polyfit() returns the coefficients [A, B] of the fit
fit_left = np.polyfit((left_bottom[0], apex[0]), (left_bottom[1], apex[1]), 1)
fit_right = np.polyfit((right_bottom[0], apex[0]), (right_bottom[1], apex[1]), 1)
fit_bottom = np.polyfit((left_bottom[0], right_bottom[0]), (left_bottom[1], right_bottom[1]), 1)
# Find the region inside the lines
XX, YY = np.meshgrid(np.arange(0, xsize), np.arange(0, ysize))
region_thresholds = (YY > (XX*fit_left[0] + fit_left[1])) & \
                    (YY > (XX*fit_right[0] + fit_right[1])) & \
                    (YY < (XX*fit_bottom[0] + fit_bottom[1]))
# Color pixels red which are inside the region of interest
region_select[region_thresholds] = [255, 0, 0]
# Display the image
plt.imshow(region_select)
edges = cv2.Canny(gray, low_threshold, high_threshold)canny 只对单通道进行 梯度计算,所以对于一个RGB 的img 需要转成 gray
import cv2 #bringing in OpenCV libraries gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) #grayscale conversion plt.imshow(gray, cmap='gray')注意到这里有 low_threshold 和 high_threshold cv2 首先会计算梯度,然后把梯度高于 high_threshold 保留下来,同时略去 低于 low_threshold 的像素。 对于在两者之间的梯度,如果和 high_threshold 的像素相连,就保留,不相连,就删去。
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]), min_line_length, max_line_gap)参数解释:
  
# Do relevant imports
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
# Read in and grayscale the image
image = mpimg.imread('exit-ramp.jpg')
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
# Define a kernel size and apply Gaussian smoothing
kernel_size = 5
blur_gray = cv2.GaussianBlur(gray,(kernel_size, kernel_size),0)
# Define our parameters for Canny and apply
low_threshold = 50
high_threshold = 150
masked_edges = cv2.Canny(blur_gray, low_threshold, high_threshold)
# Define the Hough transform parameters
# Make a blank the same size as our image to draw on
rho = 1
theta = np.pi/180
threshold = 1
min_line_length = 10
max_line_gap = 1
line_image = np.copy(image)*0 #creating a blank to draw lines on
# Run Hough on edge detected image
lines = cv2.HoughLinesP(masked_edges, rho, theta, threshold, np.array([]),
                            min_line_length, max_line_gap)
# Iterate over the output "lines" and draw lines on the blank
for line in lines:
    for x1,y1,x2,y2 in line:
        cv2.line(line_image,(x1,y1),(x2,y2),(255,0,0),10)
# Create a "color" binary image to combine with line image
color_edges = np.dstack((masked_edges, masked_edges, masked_edges)) 
# Draw the lines on the edge image
combo = cv2.addWeighted(color_edges, 0.8, line_image, 1, 0) 
plt.imshow(combo)