记录unreal4使用airsim保存图片等数据,matlab处理得到所要的图片等数据

提前安装好EPIC unreal4 版本4.22
1.unreal4_airsim_getphoto.cpp

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <iostream>
#include "common/common_utils/StrictMode.hpp"
STRICT_MODE_OFF
#ifndef RPCLIB_MSGPACK
#define RPCLIB_MSGPACK clmdep_msgpack
#endif // !RPCLIB_MSGPACK
#include "rpc/rpc_error.h"
STRICT_MODE_ON

#include "vehicles/car/api/CarRpcLibClient.hpp"
#include "common/common_utils/FileSystem.hpp"
#include <chrono>
#include <fstream>
#include <Windows.h>
#include<string.h>
#include <iomanip>
#include<tchar.h>
#include <direct.h>
#include <io.h>

int main()
{
	using namespace msr::airlib;

	std::cout << "Make sure settings.json has \"SimMode\"=\"Car\" at root. Press Enter to continue." << std::endl; std::cin.get();

	// This assumes you are running DroneServer already on the same machine.
	// DroneServer must be running first.
	msr::airlib::CarRpcLibClient client;
	typedef ImageCaptureBase::ImageRequest ImageRequest;
	typedef ImageCaptureBase::ImageResponse ImageResponse;
	typedef ImageCaptureBase::ImageType ImageType;
	typedef common_utils::FileSystem FileSystem;

	//创建img文件夹
	string folderPath = "C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\img";
	string command;
	command = "mkdir -p " + folderPath;
	system(command.c_str());

	std::ofstream outfile;
	outfile.open("C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\Groundtruth.txt");
	outfile << std::setiosflags(std::ios::left) << std::setw(20) << "Time_Stamp" << std::setw(9) << "CAMERA_X" << std::setw(9) << "CAMERA_Y" << std::setw(9) << "CAMERA_Z";
	outfile << std::setiosflags(std::ios::left) << std::setw(9) << "CAMERA_X" << std::setw(9) << "CAMERA_Y" << std::setw(9) << "CAMERA_Z";
	outfile << std::setiosflags(std::ios::left) << std::setw(9) << "Q_W" << std::setw(9) << "Q_X" << std::setw(9) << "Q_Y" << std::setw(9) << "Q_Z" << std::endl;

	client.confirmConnection();

	std::ofstream out;
	out.open("C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\ObjectsPose.txt");

	std::ofstream outtimestamp;
	outtimestamp.open("C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\timestamp.txt");


	out << std::setiosflags(std::ios::left) << std::setw(9) << "OBJ_X" << std::setw(9) << "OBJ_Y" << std::setw(9) << "OBJ_Z";
	out << std::setiosflags(std::ios::left) << std::setw(9) << "Q_W" << std::setw(9) << "Q_X" << std::setw(9) << "Q_Y" << std::setw(9) << "Q_Z" << std::endl;
	for (int i = 1; i < 22; i++) {//获取车辆的位置

		std::string str = std::to_string(i);
		Pose z = client.simGetObjectPose(str);
		out << std::setiosflags(std::ios::left) << std::setprecision(6) << z.position[0] << "  " << std::setprecision(6) << z.position[1] << "  " << std::setprecision(6) << z.position[2] << "  ";
		out << std::setiosflags(std::ios::left) << std::setprecision(6) << z.orientation.coeffs().transpose()[0] << "  ";
		out << std::setiosflags(std::ios::left) << std::setprecision(6) << z.orientation.coeffs().transpose()[1] << "  ";
		out << std::setiosflags(std::ios::left) << std::setprecision(6) << z.orientation.coeffs().transpose()[2] << "  ";
		out << std::setiosflags(std::ios::left) << std::setprecision(6) << z.orientation.coeffs().transpose()[3] << "  " << std::endl;
	}

	std::cout << "Press Enter to get image" << std::endl; std::cin.get();
	//int i = 1;
	while (1)
	{
		std::cout << "111" << std::endl;
		vector<ImageRequest> request = { ImageRequest("0",ImageType::Scene)  , ImageRequest("0", ImageType::DepthVis) };
		const vector<ImageResponse>& response = client.simGetImages(request);
		//std::cout << "# of images recieved: " << response.size() << std::endl;

		if (response.size() > 0) {
			//std::cout << "Enter path with ending separator to save images (leave empty for no save)" << std::endl;
			std::string path = "C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\img";

			//std::getline(std::cin, path);
			string stp;
			for (const ImageResponse& image_info : response) {
				std::cout << "Image uint8 size: " << image_info.image_data_uint8.size() << std::endl;
				std::cout << "Image float size: " << image_info.image_data_float.size() << std::endl;
				//该机制防止相机死机导致 录制数据终止
				if (image_info.image_data_uint8.size() == 1) {
					std::cout << "camera game over dead! " << std::endl;
					break;
				}

				std::string str_sum = std::to_string(image_info.time_stamp).substr(0, 16);

				//outtimestamp << str_sum << std::endl;

				std::string str_name = str_sum.substr(0, 10) + "." + str_sum.substr(10, 16);
				outtimestamp << str_name << std::endl;

				std::string file_path1 = FileSystem::combine(path, str_name);
				//std::string file_path1 = FileSystem::combine(path, std::to_string(image_info.time_stamp).substr(0,16));
				//std::string file_path2 = FileSystem::combine(file_path1, ".");
				//std::string file_path3 = FileSystem::combine(file_path2, std::to_string(image_info.time_stamp).substr(10, 16));

				std::ofstream file(file_path1 + ".png", std::ios::binary);

				file.write(reinterpret_cast<const char*>(image_info.image_data_uint8.data()), image_info.image_data_uint8.size());
				file.close();
				stp = std::to_string(image_info.time_stamp);

			}

			CameraInfo c = client.simGetCameraInfo("0");//修改
			Pose x = c.pose;
			//std::cout << x.position << std::endl;
			//std::cout << x.orientation.coeffs() << std::endl;
			outfile << std::setiosflags(std::ios::left) << stp.substr(0, 16) << "  " << std::setprecision(6) << x.position[0] << "  " << std::setprecision(6) << x.position[1] << "  " << std::setprecision(6) << x.position[2] << "  ";
			//outfile << std::setiosflags(std::ios::left) <<  std::setprecision(6) << x.position[0] << "  " << std::setprecision(6) << x.position[1] << "  " << std::setprecision(6) << x.position[2] << "  ";
			outfile << std::setiosflags(std::ios::left) << std::setprecision(6) << x.orientation.coeffs().transpose()[0] << "  ";
			outfile << std::setiosflags(std::ios::left) << std::setprecision(6) << x.orientation.coeffs().transpose()[1] << "  ";
			outfile << std::setiosflags(std::ios::left) << std::setprecision(6) << x.orientation.coeffs().transpose()[2] << "  ";
			outfile << std::setiosflags(std::ios::left) << std::setprecision(6) << x.orientation.coeffs().transpose()[3] << "  " << std::endl;

		}
	}

	outfile.close();
	out.close();
	outtimestamp.close();

	return 0;
}

1.2 settings.json

{
  "SeeDocsAt": "https://github.com/Microsoft/AirSim/blob/master/docs/settings.md",
  "SettingsVersion": 1.2,

  "SimMode": "Car",
  "Recording": {
    "RecordOnMove": false,
    "RecordInterval": 0.033,
    "Cameras": [
      {
        "CameraName": "0",
        "ImageType": 0,
        "PixelsAsFloat": false,
        "Compress": true
      },
      {
        "CameraName": "0",
        "ImageType": 3,
        "PixelsAsFloat": false,
        "Compress": true
      }
    ]
  },
  "CameraDefaults": {
    "CaptureSettings": [

      {
        "ImageType": 0,
        "Width": 1080,
        "Height": 800,
        "TargetGamma": 2.0
      },
      {
        "ImageType": 3,
        "Width": 1080,
        "Height": 800,
        "TargetGamma": 2.0
      }

    ]
  }
}

1.3Airport_test.unproject

{
	"FileVersion": 3,
	"EngineAssociation": "4.22",
	"Category": "",
	"Description": "",
	"Modules": [
		{
			"Name": "Airport_test",
			"Type": "Runtime",
			"LoadingPhase": "Default",
			"AdditionalDependencies": [
				"AirSim"
			]
		}
	],
	"TargetPlatforms": [
		"MacNoEditor",
		"WindowsNoEditor"
	],
	"Plugins": [
		{
			"Name": "AirSim",
			"Enabled": true
		}
	]
}

2.matlab_do_unrealphotodata

Path = 'C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\img\\';   % 设置数据存放的文件夹路径
new_depth='C:\Users\Sugar\Desktop\8_10data\8_10_1data\depth';
mkdir(new_depth);
new_rgb='C:\Users\Sugar\Desktop\8_10data\8_10_1data\rgb';
mkdir(new_rgb);

outrgb ='C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\rgb\\';%设置输出rgb图片路径
outdepth ='C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\depth\\';%设置输出depth图片路径
File = dir(fullfile(Path,'*.png'));  % 显示文件夹下所有符合后缀名为.txt文件的完整信息
%File = dir(Path);
FileNames = {File.name};            % 提取符合后缀名为.txt的所有文件的文件名,转换为n行1列
img_num=length(File);

rgbfid=fopen('C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\rgb.txt','wt');
depthfid=fopen('C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\depth.txt','wt');
associatefid=fopen('C:\\Users\\Sugar\\Desktop\\8_10data\\8_10_1data\\associate.txt','wt');

 %读取rgb图片  %读取depth图片  ,并保存其txt
for i=1:2:img_num
     %读取rgb图片,并保存到rgb文件夹下
    ImageName_rgb =fullfile(Path,File(i).name);
    imgdata_rgb=imread(ImageName_rgb);
    imshow(imgdata_rgb);
    samename_rgb=fullfile(outrgb,File(i).name);
    imwrite(imgdata_rgb,samename_rgb);
    %读取rgb图片的名字,将其时间戳和名字保存到rgb.txt文件(图片名字就是时间戳)
    str_rgb = FileNames{i};
    sub_rgb = strsplit(str_rgb,'.png');
    s_rgb=sub_rgb{1};
    sum_rgb=strcat(s_rgb,' rgb/',str_rgb);
    fprintf(rgbfid,'%s\n',sum_rgb);
    
     %读取depth图片,并保存到depth文件夹下
    ImageName_depth =fullfile(Path,File(i+1).name);
    imgdata_depth=imread(ImageName_depth);
    %imshow(imgdata_depth);
    samename_depth=fullfile(outdepth,File(i+1).name);
    imwrite(imgdata_depth,samename_depth);
    
    %读取depth图片的名字,将其时间戳和名字保存到depth.txt文件(图片名字就是时间戳)
    str_depth = FileNames{i+1};
    sub_depth = strsplit(str_depth,'.png');
    s_depth=sub_depth{1};
    sum_depth=strcat(s_depth,' depth/',str_depth);
    fprintf(depthfid,'%s\n',sum_depth);
    
    %将rgb.tx和depth.txt合并成一个txt
    fprintf(associatefid,'%s',sum_rgb);
    fprintf(associatefid,'%s',' ');
    fprintf(associatefid,'%s\n',sum_depth);
    
end
fclose(rgbfid);
fclose(depthfid);
fclose(associatefid);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值