找回密码
 立即注册
首页 业界区 业界 【AirSim】图像API的使用

【AirSim】图像API的使用

璋锌 2025-9-26 11:38:46
博客地址:https://www.cnblogs.com/zylyehuo/
参考链接: 【AirSim】
具体效果可以关注博主的小红书: 42891122102,上面有效果视频
一、基本信息与AirSim图像API的简单demo

Image API.py
  1. import airsim
  2. import numpy as np
  3. import cv2
  4. # 连接到AirSim模拟器
  5. client = airsim.MultirotorClient()
  6. client.confirmConnection()
  7. # 一次获取一张图片
  8. # response = client.simGetImage(camera_name, image_type, vehicle_name='')
  9. # 无人机摄像头编号及含义(camera_name)
  10. '''
  11. 摄像机0:无人机的前方视角
  12. 摄像机1:无人机的后方视角
  13. 摄像机2:无人机的底部视角,可以用于检测地面和障碍物
  14. 摄像机3:无人机的顶部视角,可以用于拍摄俯视图或进行目标跟踪
  15. 摄像机4:无人机的左侧视角
  16. 摄像机5:无人机的右侧视角
  17. '''
  18. # 使用图像API能够获取到的8种图像类型
  19. '''
  20. Scene:场景视图图片,即俯视图,可以看到整个场景的情况。                                   airsim.ImageType.Scene
  21. DepthPlanar:平面深度图片,可以获取场景中每个像素点到相机的距离。                          airsim.ImageType.DepthPlanar
  22. DepthPerspective:透视深度图片,可以获取场景中每个像素点到相机的距离。                     airsim.ImageType.DepthPerspective
  23. DepthVis:深度可视化图片,可以将深度图像转换为RGB图像,方便观察。                          airsim.ImageType.DepthVis
  24. DisparityNormalized:视差归一化图片,可以获取场景中每个像素点的视差值,用于计算深度信息。     airsim.ImageType.DisparityNormalized
  25. Segmentation:分割图片,可以将场景中的不同物体或区域分别标记出来,方便进行目标检测和分割。      airsim.ImageType.Segmentation
  26. SurfaceNormals:表面法线图片,可以获取场景中每个像素点的法线方向,用于计算光照和阴影效果。      airsim.ImageType.SurfaceNormals
  27. Infrared:红外线图片,可以获取场景中的红外线图像,用于热成像和红外线探测等应用。               airsim.ImageType.Infrared
  28. '''
  29. # 1.直接使用simGetImage获取PNG格式的彩色图,并保存成 .png 格式的图片文件。
  30. # response = client.simGetImage('0', airsim.ImageType.Scene, vehicle_name='Drone')
  31. # f = open('screen/scene.png', 'wb')
  32. # f.write(response)
  33. # f.close()
  34. # 2.使用 simGetImages 获取PNG格式的分割图,并保存成 .png 格式的图片文件。
  35. # responses = client.simGetImages(
  36. #     [airsim.ImageRequest(0, airsim.ImageType.Segmentation, pixels_as_float=False, compress=True)])
  37. # f = open('screen/seg.png', 'wb')
  38. # f.write(responses[0].image_data_uint8)
  39. # f.close()
  40. '''
  41. 图像类型           compress            pixels_as_float                   适合保存的图片类型
  42. PNG格式            True                   False                      彩色图、分割图、表面法线图、红外图
  43. Array格式          False                  False                      彩色图、分割图、表面法线图、红外图
  44. 浮点型格式          False                  True                              深度图
  45. '''
  46. # 3.使用 simGetImages 同时获取PNG格式的红外图和表面法线图,并保存成2个.png格式的图片文件。
  47. # responses = client.simGetImages(
  48. #     [airsim.ImageRequest(0, airsim.ImageType.Infrared, pixels_as_float=False, compress=True),
  49. #      airsim.ImageRequest(0, airsim.ImageType.SurfaceNormals, pixels_as_float=False, compress=True)])
  50. # # print(responses)
  51. # # 保存红外图
  52. # f = open('screen/infrared.png', 'wb')
  53. # f.write(responses[0].image_data_uint8)
  54. # f.close()
  55. # # 保存表面法线图
  56. # f = open('screen/surface.png', 'wb')
  57. # f.write(responses[1].image_data_uint8)
  58. # f.close()
  59. # 4.保存Array格式图像
  60. # # 读取图像数据,array格式
  61. # responses = client.simGetImages([
  62. #     airsim.ImageRequest(0, airsim.ImageType.Scene, pixels_as_float=False, compress=False)])
  63. # # 将bytes格式转换为 array格式 fromstring
  64. # img_1d = np.frombuffer(responses[0].image_data_uint8, dtype=np.uint8)
  65. # img_bgr = img_1d.reshape(responses[0].height, responses[0].width, 3)
  66. # # 保存为图片文件
  67. # cv2.imwrite('screen/scene2.png', img_bgr)  # 保存为.png格式的图像文件
  68. # cv2.imwrite('screen/scene2.jpg', img_bgr)  # 保存为.jpg格式的图像文件
  69. # cv2.imwrite('screen/scene2.tif', img_bgr)  # 保存为.tif格式的图像文件
  70. # cv2.imwrite('screen/scene2.bmp', img_bgr)  # 保存为.bmp格式的图像文件
  71. '''
  72. .jpg 格式:不带透明通道的有损压缩格式,广泛应用于互联网和数码相机领域;
  73. .png 格式:便携式网络图形,无损压缩的位图,有较高的压缩比;
  74. .tif 格式:非失真的压缩格式,占用空间较大,通常用于书籍和海报等教专业的领域;
  75. .bmp 格式:是Windows操作系统中的标准图像文件格式,通常不压缩,文件所占空间较大。
  76. '''
复制代码
二、随机设置无人机位姿并获取图像

random_pictures_get_and_save_pose.py
  1. import airsim
  2. import os
  3. import numpy as np
  4. import pandas as pd
  5. # 连接到AirSim模拟器
  6. client = airsim.MultirotorClient()
  7. client.confirmConnection()
  8. # 获取图像路径
  9. folder_path = "screen2"
  10. # 保存位姿信息的空DataFrame
  11. poses_df = pd.DataFrame(columns=['index', 'x', 'y', 'z', 'yaw', 'pitch', 'roll'])
  12. # 设置随机采样的范围和数量
  13. num_samples = 50  # 需要采样的数量
  14. x_min, x_max, y_min, y_max, z_min, z_max = -4, 4, -4, 4, -5, -2  # 位置范围
  15. yaw_min, yaw_max, pitch_min, pitch_max, roll_min, roll_max = -90, 90, -45, 45, -45, 45  # 姿态范围
  16. # 相机列表
  17. camera_list = ["0", "1", "2", "3", "4"]
  18. # 随机采样并保存图像和位姿信息
  19. poses_list = []
  20. for i in range(num_samples):
  21.     # 随机生成目标位置,并设置姿态朝向
  22.     x = np.random.uniform(x_min, x_max)
  23.     y = np.random.uniform(y_min, y_max)
  24.     z = np.random.uniform(z_min, z_max)
  25.     yaw = np.random.uniform(yaw_min, yaw_max)
  26.     pitch = np.random.uniform(pitch_min, pitch_max)
  27.     roll = np.random.uniform(roll_min, roll_max)
  28.     pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))
  29.     poses_list.append({'index': i, 'x': x, 'y': y, 'z': z, 'yaw': yaw, 'pitch': pitch, 'roll': roll})
  30.     # 移动到目标位置
  31.     client.simSetVehiclePose(pose, True)
  32.     # # 获取相机图像
  33.     # responses = client.simGetImages([airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)])
  34.     # img_raw = responses[0]
  35.     # 遍历相机列表,获取每个相机的图像
  36.     for j, camera_name in enumerate(camera_list):
  37.         # 获取相机图像
  38.         responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False, False)])
  39.         img_raw = responses[0]
  40.         # 将字节流转换为PIL的Image对象
  41.         img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
  42.         img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)
  43.         # 保存PNG格式的图像
  44.         img_filename = "pose_{0}_x_{1:.2f}_y_{2:.2f}_z_{3:.2f}_yaw_{4:.2f}_pitch_{5:.2f}_roll_{6:.2f}_camera_{4}.png".format(i, x, y, z, yaw, pitch, roll, j)
  45.         img_filepath = os.path.join(folder_path, img_filename)
  46.         airsim.write_png(os.path.normpath(img_filepath), img_rgb)
  47. print("全部图像和位姿信息均已保存到文件夹:", folder_path)
  48. # 将位姿信息保存到csv文件中
  49. poses_df = pd.DataFrame(poses_list)
  50. poses_df.to_csv(os.path.join(folder_path, 'poses.csv'), index=False)
  51. '''
  52. airsim.Vector3r函数用于创建一个三维向量,表示无人机在三个轴上的位置信息。
  53. airsim.to_quaternion函数则用于将欧拉角(即pitch、roll、yaw)转换为四元数,表示无人机的姿态信息。
  54. 四元数是一种数学工具,用于描述三维空间中的旋转。它是由一个实部和三个虚部组成的,通常表示为q = a + bi + cj + dk,
  55. 其中a是实部,b、c、d是虚部,i、j、$k$是虚数单位。四元数可以用来表示旋转的方向和角度,它比欧拉角更加稳定和准确,避免了万向锁等问题。
  56. 在机器人、计算机图形学和游戏开发等领域中,四元数被广泛应用。在AirSim中,四元数用于表示无人机的姿态信息。
  57. 万向锁是一种旋转表示中常见的问题,它发生在使用欧拉角进行旋转时,当旋转角度过大或旋转轴与旋转顺序不当时,就会出现万向锁问题。
  58. 万向锁的表现形式是旋转轴和旋转角度不能被唯一确定,即旋转自由度丧失。这会导致旋转结果不可预测,甚至无法进行旋转。
  59. 为了避免万向锁问题,可以使用四元数等其他旋转表示方法。在AirSim中,使用欧拉角进行旋转时,也可能会出现万向锁问题,因此建议使用四元数进行姿态表示。
  60. '''
复制代码
三、利用保存好的位姿csv文件截取图像

get_pictures_with_poses_from_csv.py
  1. import airsim
  2. import os
  3. import csv
  4. import numpy as np
  5. client = airsim.MultirotorClient()
  6. client.confirmConnection()
  7. # 设置相机和文件路径
  8. camera_list = ["0", "1", "2", "3", "4"]
  9. folder_path = "/home/yehuo/python_learning/AirSim_learning/screen3"
  10. # 读取位姿信息文件(csv格式)
  11. poses_csv_file = open("/home/yehuo/python_learning/AirSim_learning/screen2/poses.csv", "r")
  12. pos_reader = csv.DictReader(poses_csv_file)
  13. # 循环采样并保存图像和位姿信息
  14. for i, row in enumerate(pos_reader):
  15.     # 获取姿态信息
  16.     x, y, z = float(row['x']), float(row['y']), float(row['z'])
  17.     yaw, pitch, roll = float(row['yaw']), float(row['pitch']), float(row['roll'])
  18.     pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))
  19.     # 移动到目标位置
  20.     client.simSetVehiclePose(pose, True)
  21.     # 遍历相机列表,获取每个相机的图像
  22.     for j, camera_name in enumerate(camera_list):
  23.         responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False, False)])
  24.         img_raw = responses[0]
  25.         # 将字节流转换为PIL的Image对象
  26.         img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
  27.         img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)
  28.         # 保存PNG格式的图像
  29.         img_filename = "pose_{0}_x_{1:.2f}_y_{2:.2f}_z_{3:.2f}_yaw_{4:.2f}_pitch_{5:.2f}_roll_{6:.2f}_camera_{7}.png".format(i, x, y, z, yaw, pitch, roll, j)
  30.         img_filepath = os.path.join(folder_path, img_filename)
  31.         airsim.write_png(os.path.normpath(img_filepath), img_rgb)
  32. print("图像和位姿信息均已保存到文件夹:", folder_path)
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
前天 00:41

举报

感谢,下载保存了
您需要登录后才可以回帖 登录 | 立即注册