OpenCV C++ 03 - Save an Image to a File
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相关推荐
- c++版本opencv(02-第一个OpenCV程序 03.图像加载与保存)
c++版本opencv(02-第一个OpenCV程序) 一.02-第一个OpenCV程序 二,03.图像加载与保存 来自网易云课堂 一.02-第一个OpenCV程序 如果加载到了之后呢,我们就要对它进 ...
- 【opencv系列03】OpenCV4.X视频捕获与显示
点击上方"AI搞事情"关注我们 一. 视频获取与显示 VideoCapture类实现视频获取,可以从摄像头或者文件进行视频读取.首先,创建一个VideoCapture对象,参数可以 ...
- OpenCV:03图像的算数运算
文章目录 掩膜 图像的算数运算 加法运算 利用掩膜遮盖相加结果 减法运算 乘法运算 除法运算 图片的融合 OpenCV的逻辑运算(位运算) 与运算`&` 花图像与十字掩膜做`与运算` 或运算` ...
- 利用VS+MFC+Opencv显示图像和视频所需添加类(CvvImage.h和CvvImage.cpp的源码)。
CvvImage.h代码: #pragma once #ifndef CVVIMAGE_CLASS_DEF #define CVVIMAGE_CLASS_DEF #include "open ...
- OpenCV 2.2.0 CvvImage的使用
转自:http://blog.csdn.net/raocong2010/archive/2011/01/17/6146158.aspx# 前几日,OpenCV 2.2.0版本出现了...但是...以前 ...
- 使用OpenCV实现道路车辆计数
点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 今天,我们将一起探讨如何基于计算机视觉实现道路交通计数. 在本教程 ...
- python opencv imwrite()方法 cv :: ImwriteFlags (写入图片、保存图片)
from init.py: opencv版本4.1.0,不同版本也许doc稍有不同 def imwrite(filename, img, params=None): # real signature ...
- 【opencv系列08】OpenCV4.X图像融合操作
点击上方"AI搞事情"关注我们 图像叠加 add(src1, src2, dst=None, mask=None, dtype=None)函数或者numpy操作实现图像按位相加,若 ...
- 【opencv系列07】OpenCV4.X图像基本操作
点击上方"AI搞事情"关注我们 一.像素操作 可以通过行列坐标访问像素值,对于BGR图,返回一个蓝色.绿色.红色通道的数组值,对于灰度图,仅返回相应的强度值. 代码 import ...
最新文章
- 布尔(bool)值需注意事项
- python基础网易_十年Python大牛花了三天总结出来的python基础知识实例,超详细!...
- 安装Eclipse插件
- linux编辑文档windows,1.9vim编辑器linux内核的底层文本编辑器,跟windows系统上的文本文档类似,大部分用这个工具进行文本的编辑,这个工具的操作方式基本上用不到鼠标,多是...
- python堆栈与队列_python:用deque实现栈,队列和保存最后的N个元素
- linux 内核定时器精度_linux使用select实现精确定时器详解
- 改变人类社会的五位数学大家
- net.sf.ehcache.util.UpdateChecker
- 保持hlist_node内存的紧凑性连续性以提高遍历性能
- telnet登录交换机
- linux 如何添加用户,Linux 怎么添加用户(adduser)
- 淡出动画fadeOut
- TODA SMT上料防错系统
- 每天盯着桌面,送你几个4k、8k壁纸资源的网站,请收好
- 【MyBatis】 动态SQL——模糊查询 LIKE
- R安装nCov2019包报错:Failed to install 'unknown package' from GitHub: schannel: failed to receive handsh
- 计算机组装与维修2版,计算机组装与维修2版.doc
- 两种电子配线架的原理比较
- 三十五、jQuery(JavaScript 库)
- 面向新手的西门子博途软件常用功能介绍