opencv保存图片imwrite
时间: 2023-11-04 15:18:56 浏览: 111
OpenCV中的imwrite函数用于将图像保存到文件中。它的原型为retval=cv.imwrite(filename, img[, params])。其中,filename是保存图像的文件名,img是要保存的图像对象,params是保存图像时的参数(可选)。
以下是几个示例代码展示了如何使用imwrite函数保存图像:
示例代码1(C++):
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat srcImage;
// 加载图像
srcImage = cv::imread("image.jpg", 1);
if (srcImage.empty()) {
std::cout << "图像加载失败!" << std::endl;
return -1;
}
// 保存图像到当前项目
cv::imwrite("save.jpg", srcImage);
return 0;
}
```
示例代码2(C++):
```cpp
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat src_image = cv::imread("F:/material/images/2022/2022-10/bxf_small.jpg");
// 保存图像到指定路径
cv::imwrite("F:/img_output/bxf_small_output.jpg", src_image);
return 0;
}
```
示例代码3(C):
```c
#include <opencv2/opencv.hpp>
#include <iostream>
int main() {
cv::Mat src_image = cv::imread("F:/material/images/2022/2022-10/bxf_small.jpg");
// 保存图像到指定路径
cv::imwrite("F:/img_output/bxf_small_output.jpg", src_image);
return 0;
}
```
这些示例代码展示了如何使用imwrite函数将图像保存到指定的文件路径中。你可以根据自己的需求修改文件名和路径。注意,保存图像时需要确保文件名和路径的正确性。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [利用OpenCV的函数imwrite()保存图像到硬盘](https://blog.csdn.net/wenhao_ir/article/details/127528900)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [OpenCV:imwrite函数保存图片](https://blog.csdn.net/mars_xiaolei/article/details/62233900)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文