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 necessary
    e.g.
     1. Change brightness/contrast of the image
     2. Smooth/Blur image
     3. Crop the image
     4. Rotate the image
     5. 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 PNG
    if (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

时间: 2024-11-10 11:04:41

OpenCV && C++ 03 - Save an Image to a File的相关文章

openCV 2.4.13 iOS background_segm.hpp &#39;list&#39; file not found

最近下载了最新版的OpenCV的Framework. 导入到项目里边,编译.然后就出现了问题 /opencv2.framework/Headers/video/background_segm.hpp:47:10: 'list' file not found #include 中的list没有..... google了一圈也没有找到好的解决办法. 然后好好的回忆了一下list好像是C++的一个标准库. 然后就简单的尝试了一下#ifdef __cplusplus #endif 然后编译就通过了.算了

Save output to a text file from Mac terminal

  Simply with output redirection: system_profiler > file.txt Basically, this will take the output of system_profiler and save it to the file file.txt. There are technically two different output "streams", standard output, and standard error.

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

一. 视频获取与显示 VideoCapture类实现视频获取,可以从摄像头或者文件进行视频读取.首先,创建一个VideoCapture对象,参数可以是设备索引(摄像头索引)或视频文件的名称.如果是设备索引,当只连接一台摄像机时,可以是0或-1:也可以通过传递1来选择第二个摄像头.然后,便可以通过该对象一帧一帧的获取视频图像帧.最后,释放VideoCapture对象. 显示视频图像帧,涉及到的opencv函数包括: 创建对象:cap = cv2.VideoCapture(0) 打开视频进行初始化:

How to save console output to a file in Eclipse

https://coderanch.com/t/278299/java/Writing-output-console-file-system File file = new File("test.txt");FileOutputStream fis = new FileOutputStream(file);PrintStream out = new PrintStream(fis);System.setOut(out);System.out.println("First Li

MFC中利用OpenCV实现视频播放

近期项目中在学习全景相机相关知识 当然首先要开始将基本的MFC学会才能封装SDK 首先建立MFC对话框工程 具体的详细知识就不说了,网上有很多的教程. 添加如下控件 中间显示部分是picture control控件 采用定时器更新picture 显示框 首先包含opencv相关的头文件,在CUpanoCameraDlg.h中添加以下变量 private: CRect rect; CStatic* pStc; //标识图像显示的Picture控件 CDC* pDC; //视频显示控件设备上下文 H

Save results to different files when executing multi SQL statements in DB Query Analyzer 7.01

1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainland. It has English version named 'DB Query Analyzer'and Simplified Chinese version named   . DB Query Analyzer is one of the few excellent Client Tools i

opencv源代码分析之二:cvhaartraining.cpp

我使用的是opencv2.4.9.安装后.我的cvboost..cpp文件的路径是........\opencv\sources\apps\haartraining\cvhaartraining.cpp,研究源代码那么多天,有非常多收获.opencv库真是非常强大.当中在这篇博文中我有部分凝视,其它的有关知识请參考我博客http://blog.csdn.net/ding977921830?viewmode=contents.详细内容例如以下: /*M///////////////////////

opencv源码分析:cvCreateTreeCascadeClassifier

我使用的是opencv2.4.9,cvCreateTreeCascadeClassifier的源码在......opencv\sources\apps\haartraining\cvhaartraining.cpp之中,这中间用到很多结构体,指针函数,宏等各方面的内容,请参考我博客中的文章opencv源码详细解读目录.如需转载请注明本博网址http://blog.csdn.net/ding977921830?viewmode=contents.具体内容如下: /* * cvCreateCasca

基于opencv的MFC多摄像机视场标定软件

一.项目需求 确定网络摄像机的监控视场在全景地图上的精确位置,并采用简单而高效的方法将其所在视场标定并投影到全景地图上,项目要求能够将多路网络摄像机的视场标定在全景地图上,并实现这样的功能:当调取当前摄像机视频播放时,其他摄像机处于暂停状态. 二.项目分析 1.如何将视频投影到全景地图上,实质上就是如何将一幅幅视频帧图像与全景图像进行配准,关于图像配准方面的研究已经有很多方法,本项目采用基于互信息的图像配准算法寻找四对精确特征匹配点对. 2.设计一个多摄像机视场标定的人机交互界面的软件系统,要求