opencv 在图片上显示保留两位小数的数字
时间: 2023-12-11 11:05:10 浏览: 93
可以使用 OpenCV 的 `putText` 函数在图片上绘制文本,下面是一个示例代码:
```c++
#include <opencv2/opencv.hpp>
#include <sstream>
#include <iomanip>
int main() {
cv::Mat img = cv::imread("test.jpg");
float num = 3.14159;
std::ostringstream stream;
stream << std::fixed << std::setprecision(2) << num;
std::string str_num = stream.str();
cv::putText(img, str_num, cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 0, 255), 2);
cv::imshow("Image", img);
cv::waitKey(0);
return 0;
}
```
这里使用了 OpenCV 的 `putText` 函数,将转换后的字符串绘制在图片的 (50, 50) 位置上,使用了红色的字体,字体大小为 1,线宽为 2。可以根据需要调整这些参数。
相关问题
float保留两位小数并用opencv显示在图片上 C++
可以使用 OpenCV 中的 `putText` 函数将浮点数保留两位小数并显示在图片上。具体实现如下:
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
#include <iomanip>
int main() {
float f = 3.1415926f;
cv::Mat img(200, 200, CV_8UC3, cv::Scalar::all(255));
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << f;
std::string str = stream.str();
cv::putText(img, str, cv::Point(50, 100), cv::FONT_HERSHEY_SIMPLEX, 1.0, cv::Scalar(0, 0, 0), 2);
cv::imshow("Image", img);
cv::waitKey(0);
return 0;
}
```
运行结果会显示一个白色背景的图片,并在图片中心显示浮点数`3.14`。这里的`std::fixed`和`std::setprecision(2)`的作用与前面的问题一样。
`cv::putText` 函数用于在图片上绘制文本,其中的参数含义依次为:要绘制的文本内容、文本在图片上的坐标位置、字体、字体大小、字体颜色、文本线宽度等。
scalar 保留两位小数
Scalar类是一个用来表示颜色的类,它包含四个分量,分别是R、G、B和透明度(或H、S、V和透明度)。可以通过整数下标来访问Scalar类对象的元素。如果你想保留Scalar类中的数值到小数点后两位,你可以使用相关的函数来实现。在OpenCV中,可以使用cv::format函数将Scalar类对象转换为字符串,并指定保留小数点后两位的格式。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)