使用OpenCV保存压缩或无压缩的图像

使用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图像压缩级别的参数,其作用是通过调整压缩策略来平衡文件大小与处理性能。具体特性如下:

  1. 参数范围
    取值为0-9的整数,其中0表示无压缩(最大文件体积),9表示最高压缩率(最小文件体积)。
  2. 压缩原理
    PNG采用无损压缩算法,该参数通过调整DEFLATE压缩算法的策略(如过滤模式、滑动窗口大小等)优化压缩效率,而非直接降低图像质量。
  3. 性能影响
    更高的压缩级别会增加CPU处理时间,但能显著减小文件体积。
  4. 使用场景
    • 需要保存无压缩的图像时,采用0值。
    • 需要快速保存压缩图像时,设为低值(如1-3)。
    • 需要保存压缩图像,且需最小化存储空间时,设为高值(如6-9)。

保存图像的代码示例如下:

cv::Mat image;
//todo:为image变量赋值
cv::imwrite("output.png", image, {cv::IMWRITE_PNG_COMPRESSION, 0});  
//保存无压缩的图像

参考文献

  1. 【OpenCV】生成透明的PNG图像 - CSDN博客
  2. OpenCV学习笔记(七):opencv保存png图片的压缩编码问题 - CSDN博客
  3. OpenCV图像基本操作:读取、显示与保存 - CSDN博客
  4. 使用OpenCV保存图像-CSDN博客 - CSDN博客
  5. [C++] opencv - imwrite函数介绍和使用场景 - CSDN博客
  6. python opencv保存图像是LZW压缩的 opencv保存png图像 - 51CTO博客
  7. 【Opencv】图像的载入和显示 - CSDN博客
  8. 【笔记】OpenCV图像基本操作 - CSDN博客
  9. OpenCV官方教程节选上:图像处理入门(读取、显示、转换、拆分合并、保存)、基本操作、添加注释 - 神洛
  10. OpenCV中原始图像加载与保存压缩技巧 - CSDN博客
  11. OpenCV中原始图像加载与保存压缩技巧 - 搜狐
  12. OpenCV中原始图像加载与保存压缩技巧 - 腾讯云
  13. OpenCV这么简单为啥不学——2.1、imwrite逐帧保存图片 - CSDN下载
  14. 【OpenCV开发】imread和imwrite的类型以及第三个参数关于图片压缩质量等 - CSDN博客
  15. opencv学习2——图像写入 - CSDN
  16. 贰[2],OpenCV函数解析 - CSDN博客
  17. OpenCV入门(二)—— 图片读写基本操作 - CSDN博客
  18. python opencv中的imwrite函数_OpenCV之imwrite - CSDN博客
  19. imwrite() opencv压缩问题 - CSDN博客
  20. imwrite用法(opencv存图像) - CSDN博客
  21. opencv-python无损保存png图片 - CSDN博客
  22. OpenCV探索之路(零):HighGUI——使用imwrite调整保存的图片质量 - CSDN博客
  23. OpenCV探索之路(十八):使用imwrite调整保存的图片质量 - CSDN博客
  24. 关于opencv无损保存图片的说明 - CSDN博客
  25. opencv imwrite 之后与imread 图片变小原因与总结 - CSDN博客
  26. opencv imwrite()函数保存png格式的图像 - CSDN博客
  27. 使用Opencv对图像进行压缩和解压缩 - CSDN博客
  28. OpenCV 3.1 imwrite()函数写入异常问题解决方法 - CSDN博客
  29. OpenCV 笔记(02)— 图像显示、保存、腐蚀、模糊、canny 边缘检测(imread、imshow、namedWindow、imwrite) - CSDN博客
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值