# 算法思想

midpoint = np.int(img.shape[1]/2)


h = [0,img.shape[0]]
w = [0,img.shape[1]]
for window in range(nwindows):
start = h[1] - int(h[0] + (h[1] - h[0]) * window / nwindows)
end = h[1] - int(h[0] + (h[1] - h[0]) * (window + 1) / nwindows)
histogram = np.sum(img[end:start,w[0]:w[1]], axis=0)


leftx_current = np.argmax(histogram[:midpoint]) if np.argmax(histogram[:midpoint]) > minLane else leftx_current
rightx_current = np.argmax(histogram[midpoint:]) + midpoint if np.argmax(histogram[midpoint:]) > minLane else rightx_current


 win_y_low = img.shape[0] - (window+1)*window_height
win_y_high = img.shape[0] - window*window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
cv2.rectangle(out_img,(win_xleft_low,win_y_low),(win_xleft_high,win_y_high),
(0,255,0), 2)
cv2.rectangle(out_img,(win_xright_low,win_y_low),(win_xright_high,win_y_high),
(0,255,0), 2)


good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xleft_low) &  (nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xright_low) &  (nonzerox < win_xright_high)).nonzero()[0]


left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)


def find_line_fit(img, name = "default" ,nwindows=9, margin=50, minpix=50 , minLane = 50):
# Find the peak of the left and right halves of the histogram
# These will be the starting point for the left and right lines
midpoint = np.int(img.shape[1]/2)
# Set height of windows
window_height = np.int(img.shape[0]/nwindows)
# Identify the x and y positions of all nonzero pixels in the image
nonzero = img.nonzero()
nonzeroy = np.array(nonzero[0])
nonzerox = np.array(nonzero[1])

# Create empty lists to receive left and right lane pixel indices
left_lane_inds = []
right_lane_inds = []
h = [0,img.shape[0]]
w = [0,img.shape[1]]
leftx_current = w[0]
rightx_current = w[1]
# Step through the windows one by one
for window in range(nwindows):
start = h[1] - int(h[0] + (h[1] - h[0]) * window / nwindows)
end = h[1] - int(h[0] + (h[1] - h[0]) * (window + 1) / nwindows)
histogram = np.sum(img[end:start,w[0]:w[1]], axis=0)

leftx_current = np.argmax(histogram[:midpoint]) if np.argmax(histogram[:midpoint]) > minLane else leftx_current
rightx_current = np.argmax(histogram[midpoint:]) + midpoint if np.argmax(histogram[midpoint:]) > minLane else rightx_current

# Identify window boundaries in x and y (and right and left)
win_y_low = img.shape[0] - (window+1)*window_height
win_y_high = img.shape[0] - window*window_height
win_xleft_low = leftx_current - margin
win_xleft_high = leftx_current + margin
win_xright_low = rightx_current - margin
win_xright_high = rightx_current + margin
# Draw the windows on the visualization image
# cv2.rectangle(out_img,(win_xleft_low,win_y_low),(win_xleft_high,win_y_high),
# (0,255,0), 2)
# cv2.rectangle(out_img,(win_xright_low,win_y_low),(win_xright_high,win_y_high),
# (0,255,0), 2)
#
# cv2.line(out_img,(leftx_current,0),(leftx_current,img.shape[1]),(255,0,0))
# cv2.line(out_img, (rightx_current, 0), (rightx_current, img.shape[1]), (255, 0, 0))
# cv2.line(out_img, (midpoint, 0), (midpoint, img.shape[1]), (255, 0, 0))
#
# cv2.imshow("rec",out_img)
# cv2.waitKey(0)

# Identify the nonzero pixels in x and y within the window
good_left_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xleft_low) &  (nonzerox < win_xleft_high)).nonzero()[0]
good_right_inds = ((nonzeroy >= win_y_low) & (nonzeroy < win_y_high) &
(nonzerox >= win_xright_low) &  (nonzerox < win_xright_high)).nonzero()[0]
# Append these indices to the lists
left_lane_inds.append(good_left_inds)
right_lane_inds.append(good_right_inds)
# If you found > minpix pixels, recenter next window on their mean position
if len(good_left_inds) > minpix:
leftx_current = np.int(np.mean(nonzerox[good_left_inds]))
if len(good_right_inds) > minpix:
rightx_current = np.int(np.mean(nonzerox[good_right_inds]))

# Concatenate the arrays of indices
left_lane_inds = np.concatenate(left_lane_inds)
right_lane_inds = np.concatenate(right_lane_inds)

# Extract left and right line pixel positions
leftx = nonzerox[left_lane_inds]
lefty = nonzeroy[left_lane_inds]
rightx = nonzerox[right_lane_inds]
righty = nonzeroy[right_lane_inds]

# Fit a second order polynomial to each
try:
left_fit = np.polyfit(lefty, leftx, 2)
right_fit = np.polyfit(righty, rightx, 2)
except:
print("error:" + name)
return [0,0,0],[0,0,0]

return left_fit, right_fit


cropped = img[78:282, 0:450]
gray = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
left, right = find_line_fit(gray)
bottom_y = int(cropped.shape[0]/2)
bottom_x_left = int(left[0] * (bottom_y ** 2) + left[1] * bottom_y + left[2])
bottom_x_right = int(right[0] * (bottom_y ** 2) + right[1] * bottom_y + right[2])

mid = 200
cv2.line(cropped, (mid, 0), (mid,cropped.shape[0]), (0, 0, 255), thickness=10)
cv2.line(cropped,(bottom_x_left,bottom_y),(bottom_x_right,bottom_y),(255,0,0), thickness=10)
cv2.line(cropped, (mid, bottom_y), (int(bottom_x_left / 2 + bottom_x_right / 2), bottom_y), (0, 255, 0),
thickness=10)
angle = int(bottom_x_left / 2 + bottom_x_right / 2) - mid


# 处理效果

• 红色是我假定的车视场中心
• 蓝色是当前车道的宽以及位置
• 绿色是当前车道中心偏移我的时场中心多少

# 代码控制欧卡2

import pyvjoy
import time
j = pyvjoy.VJoyDevice(1)

def control(angle):
x = angle /180 + 0.5
j.data.wAxisX = int(x * 32767)
j.data.wAxisY = int(0.4 * 32767) # 限制速度，0.4测试了只要你不按键盘的w加速，会定速巡航，按w加速后不再按w也会定速巡航
j.data.wAxisZ = 0
j.update()

while True:
for ang in range(0，180):
control(ang)
time.sleep(1)

for ang in range(-180，0):
control(ang)
time.sleep(1)


b站地址

# 优化

• 考虑连续帧的车道线的连续性
• 考虑优化图像做对半的分割，采用上次车道线的中心作为分割
• 考虑方向控制采用PD控制
• 采用深度学习的方法推理方向盘打角
• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

只会git clone的程序员

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
04-08 2767

04-09 981
01-28 1381
06-10 1444
03-20 18万+
07-14 1690
04-05 1157