【AirSim+Python】image API和无人机获取图像

没错!这个还是b站【皮卡丘上大学啦】 up主学习的代码。我就是懒!今天下午敲得每一行代码都不能白敲,放在这方便我以后复制!up主原代码分享链接:在这!!!

1.image API获取相机图像

使用的时候根据自己需求进行注释:

import airsim
import numpy as np
import cv2

# 与airsim建立连接
client = airsim.MultirotorClient()
client.confirmConnection()

# 1.直接使用simGetImage获取PNG格式的彩色图,并保存成.png格式
# response = client .simGetImage('0', airsim .ImageType .Scene, vehicle_name='Drone')
# f = open('D:/Revive_Python/Airsim python/screen/scene.png', 'wb')
# f.write(response)
# f.close()

# 2.使用simGetImages 获取PNG格式分割图,并保存成.png格式的图片文件
# responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Segmentation,
#                                                          pixels_as_float= False, compress= True)])
# f = open('D:/Revive_Python/Airsim python/screen/seg.png', 'wb')
# f.write(responses[0].image_data_uint8 )
# f.close()

# 3.使用simGetImages 获取PNG格式红外图和表面法向图,并保存成2个.png格式的图片文件
# responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Infrared,
#                                                           pixels_as_float= False, compress= True),
#                                   airsim .ImageRequest(0, airsim .ImageType .SurfaceNormals,
#                                                        pixels_as_float= False, compress= True)])
# print(responses)
# 保存红外图
# f = open('D:/Revive_Python/Airsim python/screen/infrared.png', 'wb')
# f.write(responses[0].image_data_uint8)
# f.close()
# # 保存表面法线图
# f = open('D:/Revive_Python/Airsim python/screen/surface.png', 'wb')
# f.write(responses[1].image_data_uint8)
# f.close()

# 4.保存图像Array格式图像

# 读取图像数据,Array格式
responses = client .simGetImages([airsim .ImageRequest(0, airsim .ImageType .Scene,
                                                         pixels_as_float= False, compress= False)])
# 将bytes格式转化为 array格式,fromstring→frombuffer
img_ld = np.frombuffer(responses[0].image_data_uint8, dtype=np.uint8)
img_bgr = img_ld.reshape(responses[0].height, responses[0].width, 3)  # 3表示3通道

# 保存为图片文件
cv2 .imwrite('scene.png', img_bgr)  # 保存为.png格式的图像文件
cv2 .imwrite('scene.jpg', img_bgr)  # 保存为.jpg格式的图像文件
cv2 .imwrite('scene.tif', img_bgr)  # 保存为.tif格式的图像文件
cv2 .imwrite('scene.bmp', img_bgr)  # 保存为.bmp格式的图像文件

2.无人机随机位置和获取照片并保存位置

import airsim
import os
import numpy as np
import pandas as pd

# 连接到AirSim模拟器
client = airsim.MultirotorClient()
client.confirmConnection()

# 获取图像路径
folder_path = "screen"

# 保存位姿信息的空DataFrame
poses_df = pd.DataFrame(columns=['index', 'x', 'y', 'z', 'yaw', 'pitch', 'roll'])

# 设置随机采样的范围和数量
num_samples = 50  # 需要采样的数量
x_min, x_max, y_min, y_max, z_min, z_max = -4, 4, -4, 4, -5, -2  # 位置范围
yaw_min, yaw_max, pitch_min, pitch_max, roll_min, roll_max = -90, 90, -45, 45, -45, 45  # 姿态范围

# 相机列表
camera_list = ["0", "1", "2", "3", "4"]

# 随机采样并保存图像和位姿信息
poses_list = []
for i in range(num_samples):
   #  \随机生成目标位置,并设置姿态朝向
   x = np.random.uniform(x_min, x_max)
   y = np.random.uniform(y_min, y_max)
   z = np.random.uniform(z_min, z_max)
   yaw = np.random.uniform(yaw_min, yaw_max)
   pitch = np.random.uniform(pitch_min, pitch_max)
   roll = np.random.uniform(roll_min, roll_max)
   pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))
   poses_list.append({'index': i, 'x': x, 'y': y, 'z': z, 'yaw': yaw, 'pitch': pitch, 'roll': roll})
   # 移动到目标位置
   client.simSetVehiclePose(pose, True)

   #  获取相机图像
   # responses = client.simGetImages([airsim.ImageRequest("1", airsim.ImageType.Scene, False, False)])
   # img_raw = responses[0]

   # 遍历相机列表,获取每个相机的图像
   for j, camera_name in enumerate(camera_list):
       # 获取相机图像
       responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False,
                                                            False)])
       img_raw = responses[0]

       # 将字节流转换为PIL的Image对象
       img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
       img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)

       # 保存PNG格式的图像
       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)
       img_filepath = os.path.join(folder_path, img_filename)
       airsim.write_png(os.path.normpath(img_filepath), img_rgb)

print("全部图像和位姿信息均已保存到文件夹:", folder_path)

# 将位姿信息保存到csv文件中
poses_df = pd.DataFrame(poses_list)
poses_df.to_csv(os.path.join(folder_path, 'poses.csv'), index=False)

3.根据2中获取的无人机位置进行拍照

import airsim
import os
import csv
import numpy as np


client = airsim.MultirotorClient()
client.confirmConnection()

# 设置相机和文件路径
camera_list = ["0", "1", "2", "3", "4"]
folder_path = "D:/Revive_Python/Airsim python/square"

# 读取位姿信息文件(csv格式)
poses_csv_file = open("D:/Revive_Python/Airsim python/screen/poses.csv", "r")
pos_reader = csv.DictReader(poses_csv_file)

# 循环采样并保存图像和位姿信息
for i, row in enumerate(pos_reader):
         x, y, z = float(row['x']), float(row['y']), float(row['z'])
         yaw, pitch, roll = float(row['yaw']), float(row['pitch']), float(row['roll'])
         pose = airsim.Pose(airsim.Vector3r(x, y, z), airsim.to_quaternion(pitch, roll, yaw))

         # 移动到目标位置
         client.simSetVehiclePose(pose, True)

         # 遍历相机列表,获取每个相机的图像
         for j, camera_name in enumerate(camera_list):
                    responses = client.simGetImages([airsim.ImageRequest(camera_name, airsim.ImageType.Scene, False, False)])
                    img_raw = responses[0]

                    # 将字节流转换为PIL的Image对象
                    img1d = np.frombuffer(img_raw.image_data_uint8, dtype=np.uint8)
                    img_rgb = img1d.reshape(img_raw.height, img_raw.width, 3)

                    # 保存PNG格式的图像
                    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)
                    img_filepath = os.path.join(folder_path, img_filename)
                    airsim.write_png(os.path.normpath(img_filepath), img_rgb)

print("图像和位姿信息均已保存到文件夹:", folder_path)

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
下面是使用OpenCV和Python计算图像的高低频信息的代码: ```python import cv2 import numpy as np # 读取图像 img = cv2.imread("image.jpg", 0) # 对图像进行傅里叶变换 f = np.fft.fft2(img) fshift = np.fft.fftshift(f) # 计算图像的频谱 magnitude_spectrum = 20 * np.log(np.abs(fshift)) # 将频谱分为高频和低频 rows, cols = img.shape crow, ccol = rows // 2, cols // 2 fshift[crow - 30: crow + 30, ccol - 30: ccol + 30] = 0 f_ishift = np.fft.ifftshift(fshift) img_back = np.fft.ifft2(f_ishift) # 获取低频信息图像 low_freq_img = np.abs(img_back) # 高频信息图像是原始图像减去低频信息 high_freq_img = img - low_freq_img # 显示图像 cv2.imshow("Original Image", img) cv2.imshow("Low Frequency Image", low_freq_img.astype(np.uint8)) cv2.imshow("High Frequency Image", high_freq_img.astype(np.uint8)) cv2.waitKey(0) cv2.destroyAllWindows() ``` 在代码中,首先读取了一张图像,并将其转换为灰度图像(单通道图像)。然后,对图像进行傅里叶变换,计算图像的频谱,并将频谱进行中心化。 接下来,将频谱分为高频和低频。在代码中,我们使用一个方形掩膜将中心区域置零,然后将掩膜应用于频谱。将调整后的频谱进行反变换,并获取低频信息图像。 最后,通过将原始图像减去低频信息图像,可以获得高频信息图像。最后,使用OpenCV的`imshow`函数显示原始图像、低频信息图像和高频信息图像。 请将"image.jpg"替换为你自己的图像路径。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值