yolov5的letterbox函数实现 c++
时间: 2024-06-14 15:07:17 浏览: 17
以下是yolov5的letterbox函数的C++实现示例:
```cpp
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include <cmath>
cv::Mat letterbox(const cv::Mat& image, int targetWidth, int targetHeight)
{
cv::Mat resizedImage;
int imageWidth = image.cols;
int imageHeight = image.rows;
float scale = std::min((float)targetWidth / imageWidth, (float)targetHeight / imageHeight);
int resizedWidth = round(imageWidth * scale);
int resizedHeight = round(imageHeight * scale);
cv::resize(image, resizedImage, cv::Size(resizedWidth, resizedHeight));
cv::Mat paddedImage(targetHeight, targetWidth, CV_8UC3, cv::Scalar(128, 128, 128));
int offsetX = (targetWidth - resizedWidth) / 2;
int offsetY = (targetHeight - resizedHeight) / 2;
resizedImage.copyTo(paddedImage(cv::Rect(offsetX, offsetY, resizedWidth, resizedHeight)));
return paddedImage;
}
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)