首页 > 编程语言 > 详细

C++Airsim无人机采集图片

时间:2021-05-24 22:34:26      阅读:43      评论:0      收藏:0      [点我收藏+]

1、编写函数:无人机的飞行路径,保存到points.xml中

 1 void collectpoint() {
 2     std::vector<Point3f> point;
 3     float x;
 4     float increase = 1;    // 间隔1米
 5     for (float i = 0; i <= 100; i++) {
 6         Point3f temp;
 7         //输入x,y,z的值
 8         x = i * increase;
 9         temp.x = x;
10         temp.y = 0;
11         temp.z = -10;
12         //存入point
13         point.push_back(temp);
14     }
15     cout << point << endl;
16     FileStorage fs;
17     fs.open("points.xml", FileStorage::WRITE);
18     //写入文件
19     fs << "point" << point;
20     return;
21 }    

2、主函数中读取xml中的point3d,调用(控制无人机飞行、无人机拍照、保存图片的函数)

 1 int main(int argc, char** argv)
 2 {
 3     MultirotorRpcLibClient client;
 4     client.enableApiControl(true);
 5     client.armDisarm(true);
 6     //IP地址
 7     cam = new fhy::CameraInfo("127.0.0.1");  
 8     //图片地址
 9     std::string work_space = "collect_data/test";       
10     int times = 0;
11     string pointxml = "points.xml";
12     //调用函数生成飞行路径
13     collectpoint();
14     //读取point3d
15     cv::FileStorage fs("points.xml", cv::FileStorage::READ); 
16     std::vector<cv::Point3d> allPoint;                         //文件中的点全部存放到allpoint
17     fs["point"] >> allPoint;                                 
18     std::cout << allPoint.size() << std::endl;
19     for (size_t i = 0; i < allPoint.size(); i++)        
20     {
21         allPoint[i].y -= 0.125;                            // 无人机的右相机与无人机坐标差y-0.125m
22         cv::Point3d orginPoint(allPoint[i].x, allPoint[i].y, allPoint[i].z + 1);
23         //控制无人机飞行
24         cam->cvModeControl(allPoint.at(i));
25      //采集图片
26         cam->cvModeCollect();
27      //保存图片
28         cam->cvModesave(work_space, cameraFOV, times++);
29     }
30     delete cam;
31     return 0;
32 }

3、控制无人机飞行函数:

1 void fhy::CameraInfo::cvModeControl1(const cv::Point3d positionPoint)
2 {
3 //设置无人机的位置
4     client->simSetVehiclePose(Pose(Vector3r(positionPoint.x, positionPoint.y, positionPoint.z), VectorMath::toQuaternion(0, 0, 0)), TRUE);
5     std::cout << "run to simSetVehiclePose " << std::endl;
6 }

4、采集图片的函数:

 1 void fhy::CameraInfo::cvModeCollect()
 2 {
 3     std::vector<ImageRequest> request = {
 4         ImageRequest("3", ImageType::Scene),
 5         ImageRequest("3", ImageType::DepthVis),
 6         ImageRequest("3", ImageType::Segmentation)
 7     };
 8     const std::vector<ImageResponse> responses = client->simGetImages(request);
 9     if (responses.size() > 0)
10     {
11         if (request[0].pixels_as_float)
12         {
13             front_rgb_image = cv::imdecode(responses[0].image_data_float, cv::IMREAD_COLOR);
14         }
15         else
16         {
17             front_rgb_image = cv::imdecode(responses[0].image_data_uint8, cv::IMREAD_COLOR);
18         }
19 
20         if (request[1].pixels_as_float)
21         {
22             front_depth_image = cv::imdecode(responses[1].image_data_float, cv::IMREAD_UNCHANGED);
23         }
24         else
25         {
26             front_depth_image = cv::imdecode(responses[1].image_data_uint8, cv::IMREAD_UNCHANGED);
27         }
28 
29         if (request[2].pixels_as_float)
30         {
31             front_seg_image = cv::imdecode(responses[2].image_data_float, cv::IMREAD_COLOR);
32         }
33         else
34         {
35             front_seg_image = cv::imdecode(responses[2].image_data_uint8, cv::IMREAD_COLOR);
36         }
37 
38         if (!(front_rgb_image.empty() && front_depth_image.empty() && front_seg_image.empty()))
39         {
40         cv::waitKey(1);
41         }
42     }
43 }

5、保存图片的函数:

 1 void fhy::CameraInfo::cvModesave(const std::string work_space, const double cameraFOV, const int times)
 2 {
 3     char strNum[64];
 4     sprintf_s(strNum, 64, "%05d", times);
 5     //字符数组转换为字符串
 6     string str = strNum;
 7     std::string front_img_path = work_space + "/rgb" + "/" + str + ".jpg";
 8     std::cout << "front_img_path = " << front_img_path << std::endl;
 9     cv::imwrite(front_img_path, front_rgb_image);
10     std::cout << "无人机拍摄照片写入成功.." << std::endl;
11 }

6、多无人机的设置:在settings.json中设置多无人机

"Vehicles": {
        "Drone1": {
          "VehicleType": "SimpleFlight"
        },
        "Drone2": {
          "VehicleType": "SimpleFlight"
        }
},

多无人机的控制

//!!!!!!!!!!!!!!!!!可以使用C++开辟线程 多无人机飞行、采集、保存!!!!!!!!!!!!!!!!!!
//
1、指定无人机接收控制 MultirotorRpcLibClient client; client.enableApiControl(true, "Drone1"); client.armDisarm(true, "Drone1"); //2、控制指定的无人机飞行 client->simSetVehiclePose(Pose(Vector3r(positionPoint.x, positionPoint.y, positionPoint.z), VectorMath::toQuaternion(0, 0, 0)), TRUE, "Drone1"); //3、控制指定的无人机采集 const std::vector<ImageResponse> responses = client->simGetImages(request,"Drone1"); //4、获取指定无人机的位置 auto pose = client->getMultirotorState("Drone1").getPosition();

 

C++Airsim无人机采集图片

原文:https://www.cnblogs.com/zxyzy/p/14806322.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!