一幅幅图片中去识别和定位车道线的位置
车道线有一个与柏油马路相比很明显的特征,那就是它是白色的
#importing some useful packages
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import cv2
%matplotlib inline
#reading in an image
image = mpimg.imread('test_images/solidWhiteRight.jpg')
#printing out some stats and plotting
print('This image is:', type(image), 'with dimensions:', image.shape)
plt.imshow(image) # if you wanted to show a single color channel image called 'gray', for example, call as plt.imshow(gray, cmap='gray')
import math
def grayscale(img):
"""Applies the Grayscale transform
This will return an image with only one color channel
but NOTE: to see the returned image as grayscale
(assuming your grayscaled image is called 'gray')
you should call plt.imshow(gray, cmap='gray')"""
return cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
#使图像变成灰度图
# Or use BGR2GRAY if you read an image with cv2.imread()
# return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
def canny(img, low_threshold, high_threshold):
"""Applies the Canny transform"""
#这个是用于边缘检测的 需要处理的原图像,该图像必须为单通道的灰度图
#其中较大的阈值2用于检测图像中明显的边缘,但一般情况下检测的效果不会那么完美,
#边缘检测出来是断断续续的。
#所以这时候用较小的第一个阈值用于将这些间断的边缘连接起来。
return cv2.Canny(img, low_threshold, high_threshold)
def gaussian_blur(img, kernel_size):
#在某些情况下,需要对一个像素的周围的像素给予更多的重视。
#因此,可通过分配权重来重新计算这些周围点的值。
#这可通过高斯函数(钟形函数,即喇叭形数)的权重方案来解决。
"""Applies a Gaussian Noise kernel"""
return cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
def region_of_interest(img, vertices):
"""
Applies an image mask.
Only keeps the region of the image defined by the polygon
formed from `vertices`. The rest of the image is set to black.
`vertices` should be a numpy array of integer points.
"""
#defining a blank mask to start with
mask = np.zeros_like(img)
#defining a 3 channel or 1 channel color to fill the mask with depending on the input image
if len(img.shape) > 2:
channel_count = img.shape[2] # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,) * channel_count
else:
ignore_mask_color = 255
#filling pixels inside the polygon defined by "vertices" with
因篇幅问题不能全部显示,请点此查看更多更全内容