imwrite 怎么用
时间: 2023-07-29 16:06:38 浏览: 51
imwrite 函数是OpenCV库中用于将图像保存到文件的函数。它的语法如下:
```cpp
bool imwrite(const string& filename, InputArray img, const vector<int>& params = vector<int>());
```
其中,`filename` 是保存的文件路径和文件名,`img` 是要保存的图像,`params` 是可选参数,用于指定保存图像的格式和压缩参数。
以下是一个示例代码,演示如何使用 imwrite 函数将图像保存为 JPG 格式:
```cpp
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
Mat image = imread("input.jpg", IMREAD_COLOR); // 读取图像
if (image.empty())
{
printf("Could not open or find the image\n");
return -1;
}
bool success = imwrite("output.jpg", image); // 保存图像
if (!success)
{
printf("Failed to save the image\n");
return -1;
}
return 0;
}
```
在上述代码中,首先使用 `imread` 函数读取名为 "input.jpg" 的图像,并将其存储在 `image` 变量中。然后,使用 `imwrite` 函数将 `image` 变量中的图像保存为名为 "output.jpg" 的 JPG 格式文件。
注意:在使用该函数之前,请确保已经成功加载了 OpenCV 库,并且编译器能够找到相关的头文件和库文件。