目录
问题描述
保存.tiff图像格式时如何保证与原图像属性相同?
解决方案
与图像格式无关,在调用OpenCV库imwrite()函数时,可以在第3位可缺省标志位设置输出图像的参数。函数imwrite()的官方注释如下:
def imwrite(filename, img, params=None): # real signature unknown; restored from __doc__
"""
imwrite(filename, img[, params]) -> retval
. @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).
. - Multiple images (vector of Mat) can be saved in TIFF format (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, how to set custom compression parameters and save it to a PNG file.
. It also demonstrates how to save multiple images in a TIFF file:
. @include snippets/imgcodecs_imwrite.cpp
. @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
"""
根据官方解释,此函数按照所给参数保存图像img到相应位置filename。需要注意的一点是输出图像的格式是基于所给文件的扩展名。可以保存8bit单通道图像,3通道图像以及16bit,32bit,3通道.tiff图像。最后的标志位针对不同图像类型可以有很多选择,见OpenCV官方注释: imwirte()注释。
在笔者项目中需要保存张7168x7168的单通道.tiff图像,图像原始属性如下:
有关保存.tiff图像的标志位可选如下:
IMWRITE_TIFF_RESUNIT Python: cv.IMWRITE_TIFF_RESUNIT | For TIFF, use to specify which DPI resolution unit to set; see libtiff documentation for valid values. |
IMWRITE_TIFF_XDPI Python: cv.IMWRITE_TIFF_XDPI | For TIFF, use to specify the X direction DPI. |
IMWRITE_TIFF_YDPI Python: cv.IMWRITE_TIFF_YDPI | For TIFF, use to specify the Y direction DPI. |
IMWRITE_TIFF_COMPRESSION Python: cv.IMWRITE_TIFF_COMPRESSION | 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. |
实现代码如下:
cv.imwrite("D:/Project/images/test.tiff", test_img, ((int(cv.IMWRITE_TIFF_RESUNIT), 2,
int(cv.IMWRITE_TIFF_COMPRESSION), 1,
int(cv.IMWRITE_TIFF_XDPI), 100,
int(cv.IMWRITE_TIFF_YDPI), 100)))
这里要注意,如何实现标志位多参数,这里将参数名称与参数值使用“()”放在一起作为第3位参数,中间使用“,”隔开。IMWRITE_TIFF_RESUNIT表示分辨率单位,这里赋值为“2”表示使用单位“英寸”;IMWRITE_TIFF_COMPRESSION表示图像压缩,参照..\OpenCV4.4.0\opencv\sources\3rdparty\libtiff\tiff.h的定义,此参数有如下预定义值可供选择:
#define COMPRESSION_NONE 1 /* dump mode */
#define COMPRESSION_CCITTRLE 2 /* CCITT modified Huffman RLE */
#define COMPRESSION_CCITTFAX3 3 /* CCITT Group 3 fax encoding */
#define COMPRESSION_CCITT_T4 3 /* CCITT T.4 (TIFF 6 name) */
#define COMPRESSION_CCITTFAX4 4 /* CCITT Group 4 fax encoding */
#define COMPRESSION_CCITT_T6 4 /* CCITT T.6 (TIFF 6 name) */
#define COMPRESSION_LZW 5 /* Lempel-Ziv & Welch */
#define COMPRESSION_OJPEG 6 /* !6.0 JPEG */
#define COMPRESSION_JPEG 7 /* %JPEG DCT compression */
#define COMPRESSION_T85 9 /* !TIFF/FX T.85 JBIG compression */
#define COMPRESSION_T43 10 /* !TIFF/FX T.43 colour by layered JBIG compression */
#define COMPRESSION_NEXT 32766 /* NeXT 2-bit RLE */
#define COMPRESSION_CCITTRLEW 32771 /* #1 w/ word alignment */
#define COMPRESSION_PACKBITS 32773 /* Macintosh RLE */
#define COMPRESSION_THUNDERSCAN 32809 /* ThunderScan RLE */
/* codes 32895-32898 are reserved for ANSI IT8 TIFF/IT <dkelly@apago.com) */
#define COMPRESSION_IT8CTPAD 32895 /* IT8 CT w/padding */
#define COMPRESSION_IT8LW 32896 /* IT8 Linework RLE */
#define COMPRESSION_IT8MP 32897 /* IT8 Monochrome picture */
#define COMPRESSION_IT8BL 32898 /* IT8 Binary line art */
/* compression codes 32908-32911 are reserved for Pixar */
#define COMPRESSION_PIXARFILM 32908 /* Pixar companded 10bit LZW */
#define COMPRESSION_PIXARLOG 32909 /* Pixar companded 11bit ZIP */
#define COMPRESSION_DEFLATE 32946 /* Deflate compression */
#define COMPRESSION_ADOBE_DEFLATE 8 /* Deflate compression,
as recognized by Adobe */
/* compression code 32947 is reserved for Oceana Matrix <dev@oceana.com> */
#define COMPRESSION_DCS 32947 /* Kodak DCS encoding */
#define COMPRESSION_JBIG 34661 /* ISO JBIG */
#define COMPRESSION_SGILOG 34676 /* SGI Log Luminance RLE */
#define COMPRESSION_SGILOG24 34677 /* SGI Log 24-bit packed */
#define COMPRESSION_JP2000 34712 /* Leadtools JPEG2000 */
#define COMPRESSION_LERC 34887 /* ESRI Lerc codec: https://github.com/Esri/lerc */
/* compression codes 34887-34889 are reserved for ESRI */
#define COMPRESSION_LZMA 34925 /* LZMA2 */
#define COMPRESSION_ZSTD 50000 /* ZSTD: WARNING not registered in Adobe-maintained registry */
#define COMPRESSION_WEBP 50001 /* WEBP: WARNING not registered in Adobe-maintained registry */
这里选择不压缩,也就是选择1。
IMWRITE_TIFF_XDPI 和 IMWRITE_TIFF_YDPI 选择目标值100即可。