python学习之读取TIFF

    • OpenCV中的cv::imdecode函数是从指定的内存缓存中读一幅图像,而cv::imencode是将一幅图像写进内存缓存中。cv::imread是从指定文件载入一幅图像,cv::imwrite是保存一幅图像到指定的文件中。cv::imread和cv::imdecode内部都是通过ImageDecoder类来进行图像解码的。cv::write和cv::encod内部都是通过ImageEncoder类来进行图像编码的
    • Python: cv2.imread(filename[, flags]) → retval

Parameters:

    • filename – Name of file to be loaded.
    • flags –
      Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one
      • >0 Return a 3-channel color image.
        Note
        In the current implementation the
        alpha channel透明度通道), if any, is stripped from the output image. Use negative value if you need the alpha channel.
    • =0 Return a grayscale image.
    • <0 Return the loaded image as is (with alpha channel).

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ). Currently, the following file formats are supported:

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • WebP - *.webp (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)

来自 <https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html?highlight=tiff>

  • Flags的参考值

flag=-1时,8位深度,原通道
flag=0,8位深度,1通道
flag=1, 8位深度 ,3通道
flag=2,原深度,1通道
flag=3, 原深度,3通道
flag=4,8位深度 ,3通道

  • 示例代码:

 

#导入cv模块
import cv2 as cv
import numpy as np
#读取图像,支持 bmp、jpg、png、tiff 等常用格式
#第二个参数是通道数和位深的参数,有四种选择,参考https://www.cnblogs.com/goushibao/p/6671079.html
img = cv.imread("MYD_20140102.tif",2)
print img
#在这里一开始我写成了img.shape(),报错因为img是一个数组不是一个函数,只有函数才可以加()表示请求执行,
#参考http://blog.csdn.net/a19990412/article/details/78283742
print img.shape
print img.dtype 
print img.min()
print img.max()
img_reshape=img.reshape(-1,1)
print img_reshape

#创建窗口并显示图像
cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)
#释放窗口
cv.destroyAllWindows() 

用这种方法读TIFF出现一个问题,signed 16-bit(-32768~32767)的TIFF无法识别,会读成unsigned 16-bit,值域范围是0~65535.可能是opencv不支持有符号的16bit图像

  • tifffile包
from libtiff import TIFF
tif = TIFF.open('filename.tif', mode='r')
img = tif.read_image()

但是出现以下问题

TIFFReadDirectory: Warning, Unknown field with tag 33550 (0x830e) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33922 (0x8482) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34735 (0x87af) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 34737 (0x87b1) encountered.

但是目前看没什么大碍,好像是未识别tags的问题,日后再探究。





  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值