为什么要边缘检测
- 通过边缘化后获取道路的边缘线更方便
效果展示
注:
- 游戏内可以按V键进行视角切换
- 建议游戏改成引擎盖视角,不然是驾驶室视角。流程:设置->显示->引擎盖视角打开
代码
def convert_To_gray(image):
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_img = cv2.Canny(gray_img, threshold1=100, threshold2=200)
return gray_img
# 注:输入原始图像,先将其转化为灰度图像,通过Canny()进行边缘检测,Canny()方法参数如下:
# Parameters
# image 8-bit input image.
# edges output edge map; single channels 8-bit image, which has the same size as image .
# threshold1 first threshold for the hysteresis procedure.
# threshold2 second threshold for the hysteresis procedure.
# apertureSize aperture size for the Sobel operator.
# L2gradient a flag, indicating whether a more accurate L2 norm =(dI/dx)2+(dI/dy)2−−−−−−−−−−−−−−−−√ should be used to calculate the # image gradient magnitude ( L2gradient=true ), or whether the default L1 norm =|dI/dx|+|dI/dy| is enough ( L2gradient=false ).
代码2:对边缘检测后的图片进行展示
def screen_record():
last_time = time.time()
while True:
# 800x600 windowed mode for GTA 5, at the top left position of your main screen.
# 40 px accounts for title bar.
printscreen = np.array(ImageGrab.grab(bbox=(0, 40, 800, 640)))
print('loop took {} seconds'.format(time.time() - last_time))
last_time = time.time()
gray_img = convert_To_gray(printscreen) # 修改了这里
cv2.imshow('window', gray_img) # 修改了这里
# cv2.imshow('window', cv2.cvtColor(printscreen, cv2.COLOR_BGR2RGB))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break