当前位置: 首页 > article >正文

opencv读取和保存图像

【欢迎关注编码小哥,学习更多实用的编程方法和技巧】 

OpenCv源码地址:https://github.com/opencv/opencv

imread函数原型
@param filename Name of file to be loaded.
@param flags Flag that can take values of cv::ImreadModes

//CV_EXPORTS_W Mat imread(const String& filename, int flags = IMREAD_COLOR);

imwrite函数原型

@param filename Name of the file.
@param img(Mat or vector of Mat) Image or Images to be saved.
@param params Format - specific parameters encoded as pairs(paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags

CV_EXPORTS_W bool imwrite(const String& filename, InputArray img,const std::vector<int>& params = std::vector<int>());

//imread 第二个参数flags取值范围以及作用
enum ImreadModes {
    IMREAD_UNCHANGED = -1, //!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). Ignore EXIF orientation.
    IMREAD_GRAYSCALE = 0,  //!< If set, always convert image to the single channel grayscale image (codec internal conversion).
    IMREAD_COLOR = 1,  //!< If set, always convert image to the 3 channel BGR color image.
    IMREAD_ANYDEPTH = 2,  //!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    IMREAD_ANYCOLOR = 4,  //!< If set, the image is read in any possible color format.
    IMREAD_LOAD_GDAL = 8,  //!< If set, use the gdal driver for loading the image.
    IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
    IMREAD_REDUCED_COLOR_2 = 17, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
    IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
    IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
    IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
    IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
    IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
};

//imwrite 函数第二个参数flags的取值范围以及作用
enum ImwriteFlags {
    IMWRITE_JPEG_QUALITY = 1,  //!< For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95.
    IMWRITE_JPEG_PROGRESSIVE = 2,  //!< Enable JPEG features, 0 or 1, default is False.
    IMWRITE_JPEG_OPTIMIZE = 3,  //!< Enable JPEG features, 0 or 1, default is False.
    IMWRITE_JPEG_RST_INTERVAL = 4,  //!< JPEG restart interval, 0 - 65535, default is 0 - no restart.
    IMWRITE_JPEG_LUMA_QUALITY = 5,  //!< Separate luma quality level, 0 - 100, default is -1 - don't use.
    IMWRITE_JPEG_CHROMA_QUALITY = 6,  //!< Separate chroma quality level, 0 - 100, default is -1 - don't use.
    IMWRITE_JPEG_SAMPLING_FACTOR = 7, //!< For JPEG, set sampling factor. See cv::ImwriteJPEGSamplingFactorParams.
    IMWRITE_PNG_COMPRESSION = 16, //!< For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting).
    IMWRITE_PNG_STRATEGY = 17, //!< One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.
    IMWRITE_PNG_BILEVEL = 18, //!< Binary level PNG, 0 or 1, default is 0.
    IMWRITE_PXM_BINARY = 32, //!< For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1.
    IMWRITE_EXR_TYPE = (3 << 4) + 0, /* 48 */ //!< override EXR storage type (FLOAT (FP32) is default)
    IMWRITE_EXR_COMPRESSION = (3 << 4) + 1, /* 49 */ //!< override EXR compression type (ZIP_COMPRESSION = 3 is default)
    IMWRITE_EXR_DWA_COMPRESSION_LEVEL = (3 << 4) + 2, /* 50 */ //!< override EXR DWA compression level (45 is default)
    IMWRITE_WEBP_QUALITY = 64, //!< For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used.
    IMWRITE_HDR_COMPRESSION = (5 << 4) + 0, /* 80 */ //!< specify HDR compression
    IMWRITE_PAM_TUPLETYPE = 128,//!< For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format
    IMWRITE_TIFF_RESUNIT = 256,//!< For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values
    IMWRITE_TIFF_XDPI = 257,//!< For TIFF, use to specify the X direction DPI
    IMWRITE_TIFF_YDPI = 258,//!< For TIFF, use to specify the Y direction DPI
    IMWRITE_TIFF_COMPRESSION = 259,//!< For TIFF, use to specify the image compression scheme. See libtiff for integer constants corresponding to compression formats. Note, for images whose depth is CV_32F, only libtiff's SGILOG compression scheme is used. For other supported depths, the compression scheme can be specified by this flag; LZW compression is the default.
    IMWRITE_JPEG2000_COMPRESSION_X1000 = 272,//!< For JPEG2000, use to specify the target compression rate (multiplied by 1000). The value can be from 0 to 1000. Default is 1000.
    IMWRITE_AVIF_QUALITY = 512,//!< For AVIF, it can be a quality between 0 and 100 (the higher the better). Default is 95.
    IMWRITE_AVIF_DEPTH = 513,//!< For AVIF, it can be 8, 10 or 12. If >8, it is stored/read as CV_32F. Default is 8.
    IMWRITE_AVIF_SPEED = 514 //!< For AVIF, it is between 0 (slowest) and (fastest). Default is 9.
};

//两种方法读取图像返回图像数据,默认情况下,通过imread读取的图像是BGR三通道彩色图像。

//方法一:
#if 0
#include<opencv2/opencv.hpp>

int main(int argc,char **argv)
{
	printf("CV_VERSION:%s", CV_VERSION);  //打印OpenCv版本
	cv::Mat img = cv::imread("D:/opencv/images/lena.jpg");//读取图像并返回图像数据,返回的图像数据是Mat对象。
	if (img.empty())
	{
		printf("could not load image");
		return -1;
	}

	cv::imshow("test 1",img); //显示图像,第一个参数是窗口名称,第二个参数是图像数据。
	cv::waitKey(1000);        //参为0时,表示显示的图像窗口一直存在,当用户按任意键结束;参数>0时,表示图像窗口显示的时间(ms)。
	cv::destroyAllWindows();  //销毁创建的窗口。

	return 0;
}
#endif


//方法二:
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
	string img_path = "D:/opencv/images/";

	Mat img = imread(img_path+"lena.jpg");
	//namedWindow("test 2", WINDOW_AUTOSIZE);

	imshow("test 2",img);
	waitKey(0);
	destroyAllWindows();

	return 0;
}
#endif

//设置属性读取图像
//设置灰度属性
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
	string img_path = "D:/opencv/images/";

	Mat img = imread(img_path + "lena.jpg",IMREAD_GRAYSCALE);
	//namedWindow("input img", WINDOW_AUTOSIZE);

	imshow("test 3", img);
	waitKey(0);
	destroyAllWindows();

	return 0;
}
#endif

//设置不改变属性
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
	string img_path = "D:/opencv/images/";

	Mat img = imread(img_path + "lena.jpg", IMREAD_UNCHANGED);
	//namedWindow("input img", WINDOW_AUTOSIZE);

	imshow("test 4", img);
	waitKey(0);
	destroyAllWindows();

	return 0;
}
#endif

//设置属性将图像变为原图像的1/2大小
#if 0
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
	string img_path = "D:/opencv/images/";

	Mat img = imread(img_path + "lena.jpg", IMREAD_REDUCED_COLOR_2);
	//namedWindow("input img", WINDOW_AUTOSIZE);

	imshow("test 5", img);
	waitKey(0);
	destroyAllWindows();

	return 0;
}
#endif

#if 1
#include<opencv2/opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
{
	string img_path = "D:/opencv/images/";

	Mat img = imread(img_path + "lena.jpg", IMREAD_COLOR);
	//vector<int> set;

	//保存为png格式

	imwrite("E:/result/lena.png", img);
	//imwrite("E:/result/lena.png", img,set);

	return 0;
}
#endif


http://www.kler.cn/a/442323.html

相关文章:

  • 深入理解 D3.js 力导向图:原理、调参与应用
  • 利用EXCEL进行XXE攻击
  • SurfaceFlinger代码笔记
  • Android SystemUI——车载CarSystemUI加载(八)
  • RK3568平台(音频篇)lineout无声调试
  • RPC 简介
  • mcu+cpld 联合编程(概念及流程)
  • 【Python知识】python基础-关于异常处理
  • Ollydbg 编写脚本的一些语法及例子(OD脚本)
  • 分布式开发学习
  • 基于java的springboot和vue ui的简单网站
  • 【Java】Java8的Stream流入门使用
  • 搭建分布式Spark集群
  • Golang学习笔记_13——数组
  • MR30分布式IO模块:驱动物流传输机高效升级
  • 鸿蒙Next条件渲染用法总结
  • GPT 时代,精进编程思维 + 熟练 Prompt 是否是新的编程范式?
  • HTMLCSS:3D动态卡车
  • ChatGPT生成测试用例的最佳实践(三)
  • 设计模式——单例模式(饿汉式,懒汉式等)
  • 用户输入 %%%% , MYSQL中数据全被查询出来的Bug(GORM)
  • Java程序员如何高效学习Spring Cloud Alibaba?
  • 【ETCD】ETCD 架构揭秘:内部各组件概览
  • 基于 SSM 和 Vue 架构的新锐台球厅管理系统:创新设计引领高效实现
  • RabbitMQ中的Publish-Subscribe模式
  • 单片机:实现教学上下课的自动打玲(附带源码)