用cpp写一个根据矩形锚框坐标切割深度图并输出切割后深度图尺寸的函数
时间: 2024-05-07 15:19:12 浏览: 9
下面是一个根据矩形锚框坐标切割深度图并输出切割后深度图尺寸的函数的示例代码:
```cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void cropDepthImage(Mat& depth_img, vector<int>& anchor_box, int& cropped_width, int& cropped_height) {
int x1 = anchor_box[0]; // 左上角点的x坐标
int y1 = anchor_box[1]; // 左上角点的y坐标
int x2 = anchor_box[2]; // 右下角点的x坐标
int y2 = anchor_box[3]; // 右下角点的y坐标
// 计算切割后深度图的尺寸
cropped_width = x2 - x1 + 1;
cropped_height = y2 - y1 + 1;
// 切割深度图
Mat cropped_depth_img = depth_img(Rect(x1, y1, cropped_width, cropped_height));
// 将切割后的深度图保存到文件
imwrite("cropped_depth_img.png", cropped_depth_img);
}
int main() {
// 加载深度图
Mat depth_img = imread("depth_img.png", IMREAD_UNCHANGED);
// 定义矩形锚框坐标
vector<int> anchor_box = {100, 100, 200, 200};
// 切割深度图并输出切割后深度图尺寸
int cropped_width, cropped_height;
cropDepthImage(depth_img, anchor_box, cropped_width, cropped_height);
cout << "Cropped Depth Image Size: " << cropped_width << " x " << cropped_height << endl;
return 0;
}
```
该函数使用OpenCV库的Rect函数来指定矩形区域,然后使用该区域来切割深度图。最后,将切割后的深度图保存到文件,并输出切割后深度图的尺寸。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)