无人驾驶(二)| lane detection | Udacity | Gradient到底是个啥?(通过线性代数矩阵进行分析)

Gradient曾经是个让我迷惑的词,现在通过Matrix来看看

先来看看opencv的代码

gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0)
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1)

可以看到Sobel函数后面有两个参数0,1。其中(1,0)表示 S o b e l x Sobel_x Sobelx,(0,1)表示 S o b e l y Sobel_y Sobely
在这里插入图片描述
g r a d i e n t = ∑ ( r e g i o n ∗ S x ) gradient=∑(region∗S_x) gradient=(regionSx)

同时我们得到了 G r a d i e n t x Gradient_x Gradientx G r a d i e n t y Gradient_y Gradienty

G = G x 2 + G y 2 \mathbf {G} ={\sqrt {{\mathbf {G} _{x}}^{2}+{\mathbf {G} _{y}}^{2}}} G=Gx2+Gy2

与此同时我们也可以得到

Θ = atan ⁡ ( G y G x ) {\displaystyle \mathbf {\Theta } =\operatorname {atan} \left({\mathbf {G} _{y} \over \mathbf {G} _{x}}\right)} Θ=atan(GxGy)

接下来我们就可以通过threshold来控制gradient和direction,上代码!

# Apply each of the thresholding functions
# You should write these functions on your own
gradx = abs_sobel_thresh(image, orient='x', sobel_kernel=ksize, thresh=(0, 255))
grady = abs_sobel_thresh(image, orient='y', sobel_kernel=ksize, thresh=(0, 255))
mag_binary = mag_thresh(image, sobel_kernel=ksize, mag_thresh=(0, 255))
dir_binary = dir_threshold(image, sobel_kernel=ksize, thresh=(0, np.pi/2))
combined = np.zeros_like(dir_binary)
combined[((gradx == 1) & (grady == 1)) | ((mag_binary == 1) & (dir_binary == 1))] = 1

你可能感兴趣的:(无人驾驶)