使用cv::imwrite
函数保存图像时,可选择不同的参数,保存不同格式的图像文件,也可保存压缩或无压缩的图像文件。以PNG图像为例,如何保存压缩或无压缩的图像文件呢?
在OpenCV的源代码中,有如下枚举:
//! 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. If JPEG_LIB_VERSION < 70, Not supported.
IMWRITE_JPEG_CHROMA_QUALITY = 6, //!< Separate chroma quality level, 0 - 100, default is -1 - don't use. If JPEG_LIB_VERSION < 70, Not supported.
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, //!< For PNG, One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE.
IMWRITE_PNG_BILEVEL = 18, //!< For PNG, Binary level PNG, 0 or 1, default is 0.
IMWRITE_PNG_FILTER = 19, //!< For PNG, One of cv::ImwritePNGFilterFlags, default is IMWRITE_PNG_FILTER_SUB.
IMWRITE_PNG_ZLIBBUFFER_SIZE = 20, //!< For PNG with libpng, sets the size of the internal zlib compression buffer in bytes, from 6 to 1048576(1024 KiB). Default is 8192(8 KiB). For normal use, 131072(128 KiB) or 262144(256 KiB) may be sufficient. If WITH_SPNG=ON, it is not supported.
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 ImwriteTiffResolutionUnitFlags. Default is IMWRITE_TIFF_RESOLUTION_UNIT_INCH.
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 cv::ImwriteTiffCompressionFlags. 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_TIFF_ROWSPERSTRIP = 278,//!< For TIFF, use to specify the number of rows per strip.
IMWRITE_TIFF_PREDICTOR = 317,//!< For TIFF, use to specify predictor. See cv::ImwriteTiffPredictorFlags. Default is IMWRITE_TIFF_PREDICTOR_HORIZONTAL .
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 10(fastest). Default is 9.
IMWRITE_JPEGXL_QUALITY = 640,//!< For JPEG XL, it can be a quality from 0 to 100 (the higher is the better). Default value is 95. If set, distance parameter is re-calicurated from quality level automatically. This parameter request libjxl v0.10 or later.
IMWRITE_JPEGXL_EFFORT = 641,//!< For JPEG XL, encoder effort/speed level without affecting decoding speed; it is between 1 (fastest) and 10 (slowest). Default is 7.
IMWRITE_JPEGXL_DISTANCE = 642,//!< For JPEG XL, distance level for lossy compression: target max butteraugli distance, lower = higher quality, 0 = lossless; range: 0 .. 25. Default is 1.
IMWRITE_JPEGXL_DECODING_SPEED = 643,//!< For JPEG XL, decoding speed tier for the provided options; minimum is 0 (slowest to decode, best quality/density), and maximum is 4 (fastest to decode, at the cost of some quality/density). Default is 0.
IMWRITE_GIF_LOOP = 1024, //!< Not functional since 4.12.0. Replaced by cv::Animation::loop_count.
IMWRITE_GIF_SPEED = 1025, //!< Not functional since 4.12.0. Replaced by cv::Animation::durations.
IMWRITE_GIF_QUALITY = 1026, //!< For GIF, it can be a quality from 1 to 8. Default is 2. See cv::ImwriteGifCompressionFlags.
IMWRITE_GIF_DITHER = 1027, //!< For GIF, it can be a quality from -1(most dither) to 3(no dither). Default is 0.
IMWRITE_GIF_TRANSPARENCY = 1028, //!< For GIF, the alpha channel lower than this will be set to transparent. Default is 1.
IMWRITE_GIF_COLORTABLE = 1029 //!< For GIF, 0 means global color table is used, 1 means local color table is used. Default is 0.
};
其中,cv::IMWRITE_PNG_COMPRESSION
是用于控制PNG图像压缩级别的参数,其作用是通过调整压缩策略来平衡文件大小与处理性能。具体特性如下:
- 参数范围
取值为0-9的整数,其中0表示无压缩(最大文件体积),9表示最高压缩率(最小文件体积)。 - 压缩原理
PNG采用无损压缩算法,该参数通过调整DEFLATE压缩算法的策略(如过滤模式、滑动窗口大小等)优化压缩效率,而非直接降低图像质量。 - 性能影响
更高的压缩级别会增加CPU处理时间,但能显著减小文件体积。 - 使用场景
- 需要保存无压缩的图像时,采用0值。
- 需要快速保存压缩图像时,设为低值(如1-3)。
- 需要保存压缩图像,且需最小化存储空间时,设为高值(如6-9)。
保存图像的代码示例如下:
cv::Mat image;
//todo:为image变量赋值
cv::imwrite("output.png", image, {cv::IMWRITE_PNG_COMPRESSION, 0});
//保存无压缩的图像
参考文献
- 【OpenCV】生成透明的PNG图像 - CSDN博客
- OpenCV学习笔记(七):opencv保存png图片的压缩编码问题 - CSDN博客
- OpenCV图像基本操作:读取、显示与保存 - CSDN博客
- 使用OpenCV保存图像-CSDN博客 - CSDN博客
- [C++] opencv - imwrite函数介绍和使用场景 - CSDN博客
- python opencv保存图像是LZW压缩的 opencv保存png图像 - 51CTO博客
- 【Opencv】图像的载入和显示 - CSDN博客
- 【笔记】OpenCV图像基本操作 - CSDN博客
- OpenCV官方教程节选上:图像处理入门(读取、显示、转换、拆分合并、保存)、基本操作、添加注释 - 神洛
- OpenCV中原始图像加载与保存压缩技巧 - CSDN博客
- OpenCV中原始图像加载与保存压缩技巧 - 搜狐
- OpenCV中原始图像加载与保存压缩技巧 - 腾讯云
- OpenCV这么简单为啥不学——2.1、imwrite逐帧保存图片 - CSDN下载
- 【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等 - CSDN博客
- opencv学习2——图像写入 - CSDN
- 贰[2],OpenCV函数解析 - CSDN博客
- OpenCV入门(二)—— 图片读写基本操作 - CSDN博客
- python opencv中的imwrite函数_OpenCV之imwrite - CSDN博客
- imwrite() opencv压缩问题 - CSDN博客
- imwrite用法(opencv存图像) - CSDN博客
- opencv-python无损保存png图片 - CSDN博客
- OpenCV探索之路(零):HighGUI——使用imwrite调整保存的图片质量 - CSDN博客
- OpenCV探索之路(十八):使用imwrite调整保存的图片质量 - CSDN博客
- 关于opencv无损保存图片的说明 - CSDN博客
- opencv imwrite 之后与imread 图片变小原因与总结 - CSDN博客
- opencv imwrite()函数保存png格式的图像 - CSDN博客
- 使用Opencv对图像进行压缩和解压缩 - CSDN博客
- OpenCV 3.1 imwrite()函数写入异常问题解决方法 - CSDN博客
- OpenCV 笔记(02)— 图像显示、保存、腐蚀、模糊、canny 边缘检测(imread、imshow、namedWindow、imwrite) - CSDN博客