网站服务器类型,python安装wordpress,全球互联网企业排名,可以做免费广告的网站有哪些此文章主要介绍carla前后左右摄像头画面拼接到pygame上 文章目录 前言一、要点分析二、完整代码三、拼接效果四、总结 前言
1、使用carla做仿真测试或者开发时#xff0c;如果能够将车辆周边的画面拼接并渲染#xff0c;可以直观地查看周围地环境#xff0c;便于调试。本文… 此文章主要介绍carla前后左右摄像头画面拼接到pygame上 文章目录 前言一、要点分析二、完整代码三、拼接效果四、总结 前言
1、使用carla做仿真测试或者开发时如果能够将车辆周边的画面拼接并渲染可以直观地查看周围地环境便于调试。本文将介绍使用carla中的camera传感器监控自车周边的画面并通过pygame可视化。
一、要点分析
1、如果摄像头的横向分辨率image_x如果不为192的倍数可能会导致内存对齐问题拼接效果则会出现闪屏调试了很久才发现这个规律知道具体原因的大佬可以交流下猜测是显卡解析相关导致。即Image_x 192 * N (N为正整数)下图为分辨率不为192倍数的拼接情况。 2、camera.listen(lambda data : callback(data))listen方法是carla中用于设置摄像头传感器的回调函数的。当摄像头传感器捕获到新的图像数据image时它会调用这个回调函数并将图像数据image作为参数传递给它。下图中的cala.SensorData就是carla.Image具体更多内容可以到carla官网查看。 3、使用np.concatenate拼接不同摄像头的数据。 1numpy.concatenate是一个用于将多个数组沿指定轴连接在一起的函数。它的基本语法如下
numpy.concatenate((a1, a2, ...), axis0)2其中a1, a2, …是你想要连接的数组axis参数指定了连接的轴。默认情况下axis是0这意味着数组将在垂直方向上即行方向连接当axis1时数组将在水平方向上即列方向连接。
import numpy as npa np.array([1, 2])
b np.array([3, 4])
c np.concatenate((a, b), axis0)print(c) # 输出[1 2 3 4]a1 np.array([[1, 2], [3, 4]])
a2 np.array([[5, 6], [7, 8]])c np.concatenate((a1, a2), axis1)print(c)
#输出
[[1 2 5 6][3 4 7 8]]二、完整代码
import carla
import random
import pygame
import numpy as np# 渲染对象来保持和传递 PyGame 表面
class RenderObject(object):def __init__(self, width, height):init_image np.random.randint(0, 255, (height, width, 3), dtypeuint8)self.surface pygame.surfarray.make_surface(init_image.swapaxes(0, 1))# 相机传感器回调将相机的原始数据重塑为 2D RGB并应用于 PyGame 表面
def pygame_callback(image, side):img np.reshape(np.copy(image.raw_data), (image.height, image.width, 4))img img[:, :, :3]img img[:, :, ::-1]if side Front:global FrontFront imgelif side Rear:global RearRear imgelif side Left:global LeftLeft imgelif side Right:global RightRight imgif (Front in globals() and Rear in globals()and Left in globals()and Right in globals()):# 横向拼接前后左右摄像头的画面img_combined_front np.concatenate((Front, Rear), axis1)img_combined_rear np.concatenate((Left, Right), axis1)# 纵向拼接前后左右摄像头的画面img_combined np.concatenate((img_combined_front, img_combined_rear), axis0)renderObject.surface pygame.surfarray.make_surface(img_combined.swapaxes(0, 1))class cameraManage():def __init__(self, world, ego_vehicle, pygame_size):self.world worldself.cameras {}self.ego_vehicle ego_vehicleself.image_size_x int(pygame_size.get(image_x) / 2) # 横向放置两个摄像头的画面self.image_size_y int(pygame_size.get(image_y) / 2) # 纵向放置两个摄像头的画面def camaraGenarate(self):cameras_transform [(carla.Transform(carla.Location(x2.0, y0.0, z1.3), # 前侧摄像头安装位置carla.Rotation(pitch0, yaw0, roll0)), Front),(carla.Transform(carla.Location(x-2.0, y0.0, z1.3), # 后侧摄像头安装位置carla.Rotation(pitch0, yaw180, roll0)), Rear),(carla.Transform(carla.Location(x0.0, y2.0, z1.3), # 左侧摄像头安装位置carla.Rotation(pitch0, yaw90, roll0)), Left),(carla.Transform(carla.Location(x0.0, y-2.0, z1.3), # 右侧的摄像头安装位置carla.Rotation(pitch0, yaw-90, roll0)), Right)]# 查找RGB相机蓝图camera_bp self.world.get_blueprint_library().find(sensor.camera.rgb)# 设置摄像头的fov为90°camera_bp.set_attribute(fov, 90)# 设置摄像头的分辨率camera_bp.set_attribute(image_size_x, str(self.image_size_x))camera_bp.set_attribute(image_size_y, str(self.image_size_y))# 生成摄像头for index, (camera_ts, camera_sd) in enumerate(cameras_transform):camera self.world.spawn_actor(camera_bp, camera_ts, attach_toself.ego_vehicle)self.cameras[camera_sd] camerareturn self.camerasif __name__ __main__:# 连接到客户端并检索世界对象client carla.Client(localhost, 2000)world client.get_world()# 获取地图的刷出点spawn_point random.choice(world.get_map().get_spawn_points())# 生成车辆并设置自动驾驶vehicle_bp world.get_blueprint_library().filter(*vehicle*).filter(vehicle.tesla.*)[0]ego_vehicle world.spawn_actor(vehicle_bp, spawn_point)# ego_vehicle.set_autopilot(True)#设置pygame窗口size,image_x为192的整数倍用其他分辨率会闪屏可能是显卡解析原因导致。pygame_size {image_x: 1152,image_y: 600}#调用cameraManage类生成摄像头cameras cameraManage(world, ego_vehicle, pygame_size).camaraGenarate()#采集carla世界中camera的图像cameras.get(Front).listen(lambda image: pygame_callback(image, Front))cameras.get(Rear).listen(lambda image: pygame_callback(image, Rear))cameras.get(Left).listen(lambda image: pygame_callback(image, Left))cameras.get(Right).listen(lambda image: pygame_callback(image, Right))# 为渲染实例化对象renderObject RenderObject(pygame_size.get(image_x), pygame_size.get(image_y))# 初始化pygame显示pygame.init()gameDisplay pygame.display.set_mode((pygame_size.get(image_x), pygame_size.get(image_y)),pygame.HWSURFACE | pygame.DOUBLEBUF)# 循环执行crashed Falsewhile not crashed:# 等待同步world.tick()# 按帧更新渲染的 Camera 画面gameDisplay.blit(renderObject.surface, (0, 0))pygame.display.flip()# 获取 pygame 事件for event in pygame.event.get():# If the window is closed, break the while loopif event.type pygame.QUIT:crashed True# 结束ego_vehicle.destroy()camera cameras.values()for cam in camera:cam.stoppygame.quit()三、拼接效果 四、总结
欢迎各位大佬来交流特别是为什么只支持192倍数的分辨率设置还是说我的方法不严谨欢迎交流。