Code

/*
作者:郑大峰
时间:2019年09月20日
环境:OpenCV 4.1.1 + VS2017
内容:Save an Image to a File
*/#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>using namespace std;
using namespace cv;int main()
{Mat image = imread("claudia.png");if (image.empty()){cout << "Could not open or find the image" << endl;cin.get();return -1;}/*Make changes to the image as necessarye.g.1. Change brightness/contrast of the image2. Smooth/Blur image3. Crop the image4. Rotate the image5. Draw shapes on the image*///bool isSuccess = imwrite("claudia_changed.jpg", image); //write the image to a file as JPEG bool isSuccess = imwrite("claudia_changed.png", image); //write the image to a file as PNGif (isSuccess == false){cout << "Failed to save the image" << endl;cin.get();return -1;}cout << "Image is succusfully saved to a file" << endl;cin.get();return 0;
}

Result

Explanation

bool imwrite( const String& filename, InputArray img, const std::vector& params = std::vector())

@brief Saves an image to a specified file.

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see cv::imread for the list of extensions). In general, only 8-bit single-channel or 3-channel (with 'BGR' channel order) images can be saved using this function, with these exceptions:

  • 16-bit unsigned (CV_16U) images can be saved in the case of PNG, JPEG 2000, and TIFF formats
  • 32-bit float (CV_32F) images can be saved in PFM, TIFF, OpenEXR, and Radiance HDR formats; 3-channel (CV_32FC3) TIFF images will be saved using the LogLuv high dynamic range encoding (4 bytes per pixel)
  • PNG images with an alpha channel can be saved using this function. To do this, create 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below).

If the format, depth or channel order is different, use Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.

The sample below shows how to create a BGRA image and save it to a PNG file. It also demonstrates how to set custom compression parameters:
@include snippets/imgcodecs_imwrite.cpp
@param filename Name of the file.
@param img Image to be saved.
@param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags

转载于:https://www.cnblogs.com/zdfffg/p/11557920.html

OpenCV C++ 03 - Save an Image to a File相关推荐

  1. c++版本opencv(02-第一个OpenCV程序 03.图像加载与保存)

    c++版本opencv(02-第一个OpenCV程序) 一.02-第一个OpenCV程序 二,03.图像加载与保存 来自网易云课堂 一.02-第一个OpenCV程序 如果加载到了之后呢,我们就要对它进 ...

  2. 【opencv系列03】OpenCV4.X视频捕获与显示

    点击上方"AI搞事情"关注我们 一. 视频获取与显示 VideoCapture类实现视频获取,可以从摄像头或者文件进行视频读取.首先,创建一个VideoCapture对象,参数可以 ...

  3. OpenCV:03图像的算数运算

    文章目录 掩膜 图像的算数运算 加法运算 利用掩膜遮盖相加结果 减法运算 乘法运算 除法运算 图片的融合 OpenCV的逻辑运算(位运算) 与运算`&` 花图像与十字掩膜做`与运算` 或运算` ...

  4. 利用VS+MFC+Opencv显示图像和视频所需添加类(CvvImage.h和CvvImage.cpp的源码)。

    CvvImage.h代码: #pragma once #ifndef CVVIMAGE_CLASS_DEF #define CVVIMAGE_CLASS_DEF #include "open ...

  5. OpenCV 2.2.0 CvvImage的使用

    转自:http://blog.csdn.net/raocong2010/archive/2011/01/17/6146158.aspx# 前几日,OpenCV 2.2.0版本出现了...但是...以前 ...

  6. 使用OpenCV实现道路车辆计数

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 今天,我们将一起探讨如何基于计算机视觉实现道路交通计数. 在本教程 ...

  7. python opencv imwrite()方法 cv :: ImwriteFlags (写入图片、保存图片)

    from init.py: opencv版本4.1.0,不同版本也许doc稍有不同 def imwrite(filename, img, params=None): # real signature ...

  8. 【opencv系列08】OpenCV4.X图像融合操作

    点击上方"AI搞事情"关注我们 图像叠加 add(src1, src2, dst=None, mask=None, dtype=None)函数或者numpy操作实现图像按位相加,若 ...

  9. 【opencv系列07】OpenCV4.X图像基本操作

    点击上方"AI搞事情"关注我们 一.像素操作 可以通过行列坐标访问像素值,对于BGR图,返回一个蓝色.绿色.红色通道的数组值,对于灰度图,仅返回相应的强度值. 代码 import ...

最新文章

  1. 布尔(bool)值需注意事项
  2. python基础网易_十年Python大牛花了三天总结出来的python基础知识实例,超详细!...
  3. 安装Eclipse插件
  4. linux编辑文档windows,1.9vim编辑器linux内核的底层文本编辑器,跟windows系统上的文本文档类似,大部分用这个工具进行文本的编辑,这个工具的操作方式基本上用不到鼠标,多是...
  5. python堆栈与队列_python:用deque实现栈,队列和保存最后的N个元素
  6. linux 内核定时器精度_linux使用select实现精确定时器详解
  7. 改变人类社会的五位数学大家
  8. net.sf.ehcache.util.UpdateChecker
  9. 保持hlist_node内存的紧凑性连续性以提高遍历性能
  10. telnet登录交换机
  11. linux 如何添加用户,Linux 怎么添加用户(adduser)
  12. 淡出动画fadeOut
  13. TODA SMT上料防错系统
  14. 每天盯着桌面,送你几个4k、8k壁纸资源的网站,请收好
  15. 【MyBatis】 动态SQL——模糊查询 LIKE
  16. R安装nCov2019包报错:Failed to install 'unknown package' from GitHub: schannel: failed to receive handsh
  17. 计算机组装与维修2版,计算机组装与维修2版.doc
  18. 两种电子配线架的原理比较
  19. 三十五、jQuery(JavaScript 库)
  20. 面向新手的西门子博途软件常用功能介绍

热门文章

  1. 9月26日数聚云端·智驭未来「阿里云数据库创新上云峰会」邀你进入数智未来
  2. Nacos 2.0 升级前后性能对比压测
  3. 技术人生——解决问题的规律
  4. php静态函数作用,php – OOP中静态函数有什么用?
  5. 干货分享:插画家Anmi的创作技巧与练习方法
  6. “压倒性快感”是怎么来的?氪金抽卡,是利用认知BUG的最强发明
  7. 社交网站将推动手游发展
  8. Postman批量接口测试
  9. 九零后程序员心塞大哭:“我累的心脏疼,父母却说我工作很轻松”
  10. 全国 省市 自治区的数据库sql生成