【问题标题】:How to read a signed 16-bit TIFF in python?如何在 python 中读取签名的 16 位 TIFF?
【发布时间】:2018-02-06 02:00:33
【问题描述】:

我使用 OpenCV 的 imread() 来读取 TIFF。但价值观与我已经知道的不同。此 TIFF 是有符号 16 位,它有负值。使用 imread() 的值范围是 0~65535,它是 无符号 16 位

import cv2 as cv

img = cv.imread("MYD_20140102.tif",2)
print img
print img.dtype 
print img.shape
print img.min()
print img.max()

cv.namedWindow("Image")
cv.imshow("Image",img)
cv.waitKey(0)
cv.destroyAllWindows()


output:
img=[[55537 55537 55537 ... 55537 55537 55537]
 [55537 55537 55537 ... 55537 55537 55537]
 [55537 55537 55537 ... 55537 55537 55537]
 ...
 [55537 55537 55537 ... 55537 55537 55537]
 [55537 55537 55537 ... 55537 55537 55537]
 [55537 55537 55537 ... 55537 55537 55537]]
type=uint16
shape=(2318, 2296)
imgMin=0
imgMAX=65535

【问题讨论】:

    标签: python opencv tiff signed


    【解决方案1】:

    库 tifffile (https://pypi.python.org/pypi/tifffile) 完全符合您的要求。 这是一个创建 int16 numpy 数组的示例,将其保存在磁盘上,然后再次加载:

    import tifffile
    import numpy as np
    
    # random array of numbers between -5 and 5
    a = np.asarray(np.random.rand(8, 8)*10-5, dtype=np.int16)
    # save array to disk and display its content
    tifffile.imsave("test.tiff", a)
    print(str(a) + "\n")
    # load the array back from the disk and display its content too
    b = tifffile.imread("test.tiff")
    print(b)
    

    输出:

    a=[[ 1 -1 -3  2  3 -1  4  0]
       [ 2 -2  2  0 -1  0  3 -4]
       [ 0 -4  3  2 -4 -2  0 -3]
       [ 0 -1  0 -2  0  3 -3  1]
       [ 0 -4  3  1 -1  3  2  3]
       [-3  4  4  3 -3  1 -3 -2]
       [ 4  0 -4 -2  1 -3  3 -3]
       [ 4  0  4  2  3  1 -2 -4]]
    
    b=[[ 1 -1 -3  2  3 -1  4  0]
       [ 2 -2  2  0 -1  0  3 -4]
       [ 0 -4  3  2 -4 -2  0 -3]
       [ 0 -1  0 -2  0  3 -3  1]
       [ 0 -4  3  1 -1  3  2  3]
       [-3  4  4  3 -3  1 -3 -2]
       [ 4  0 -4 -2  1 -3  3 -3]
       [ 4  0  4  2  3  1 -2 -4]]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2011-11-07
      • 2019-07-29
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多