使用基于phython的OpenCV库imwrite()保存tiff格式图像
目录
问题描述
解决方案
问题描述
保存.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即可。
使用基于phython的OpenCV库imwrite()保存tiff格式图像相关推荐
- python dlib opencv人脸识别准确度_基于dlib和opencv库的人脸识别
基于dlib和opencv库的人脸识别 需下载68个特征点的人脸检测模型: http://dlib.net/files/ 文件名为shape_predictor_68_face_landmarks.d ...
- R语言使用tiff函数将可视化图像结果保存tiff格式、使用tiff函数打开图像设备、使用dev.off函数关闭图像设备、使用compression参数自定义压缩类型(形式)
R语言使用tiff函数将可视化图像结果保存tiff格式.使用tiff函数打开图像设备.使用dev.off函数关闭图像设备.使用compression参数自定义压缩类型(形式) 目录
- matlab imwrite函数保存jpg格式图像丢失数据或改变图像
http://www.mamicode.com/info-detail-2009684.html https://blog.csdn.net/majinlei121/article/details/5 ...
- GDI+ 中发生一般性错误(在 OutputStream 中保存 PNG 格式图像时遇到的问题)
在将图片以 PNG 格式保存至 Response.OutputStream 时,会碰到如下错误: GDI+ 中发生一般性错误. 原因: 在写 PNG 格式的图像时,指针需要在存储的位置来回移动.而 R ...
- Matlab与C/C++混合编程 (基于Opencv库)
之前用过基于VS2018 与MATLAB2018a 混合编程(C++特性)(见https://blog.csdn.net/wwwoowww/article/details/83013801),奈何后来 ...
- 调用opencv库进行身份证号码识别主要流程
如题,就是对身份证拍照,处理相应照片,识别出身份证号码 这里需要调用opencv库.opencv库包含了许多处理图像的函数,功能全面而且强大,兼容多种语言.如何配置可以自行搜索. 主要流程如下: 读 ...
- 【转】前端图片该保存为什么格式?png or jpg?
疑虑: 图片存储为web格式,该用什么格式保存呢?png?jpg?压缩比例该为多大?css spript的优劣?有时候我们可能会因为一张格式不正确的图片而导致设计品质的下降以及页面性能的降低.了解图片 ...
- 【OpenCV 例程200篇】02. 图像的保存(cv2.imwrite)
[OpenCV 例程200篇]02. 图像的保存(cv2.imwrite) 欢迎关注 『OpenCV 例程200篇』 系列,持续更新中 欢迎关注 『Python小白的OpenCV学习课』 系列,持续更 ...
- 【OpenCV 例程300篇】02. 图像的保存(cv2.imwrite)
专栏地址:『youcans 的 OpenCV 例程300篇 - 总目录』 01. 图像的读取(cv2.imread) 02. 图像的保存(cv2.imwrite) 03. 图像的显示(cv2.imsh ...
最新文章
- pyqt5教程8:对话框
- Druid-基本概念
- 网页传世服务器端,传世服务端文件分析
- 操作系统之进程管理:3、进程控制(进程状态转化的实现)、原语、进程通信(共享、管道、消息)
- vonic 环境配置_Vonic 2.0 全新文档站上线
- BZOJ3421 : Poi2013 Walk
- ORBSLAM关键帧的筛选和插入
- 从开锁到中年危机的杂谈
- linux 查看硬盘序列号、设备序列号、系统安装时间
- 计算机驱动恢复出厂设置在哪里,Windows系统恢复出厂设置在哪?怎么恢复出厂设置?...
- 关于长高问题 我的看法
- xp计算机u盘重装系统,机械师电脑u盘重装系统xp教程
- HDU 1427 速算24点(dfs)
- 史上最好的Markdown编辑器
- asp.net网站服务器,vs2010制作简单的asp.net网站
- 奇迹gm工具 奇迹批量远程工具
- Elixir-Basic types 之 Binaries、strings、charlist
- 基于opencv人脸识别
- Dynamics CRM 知识库设置
- 图像处理:手写实现图像增广算法(旋转、亮度调整、裁剪与拼接)