GeoTiff透明度#

背景#

大部分情况下,兴趣区域(ROI)并不能完美的符合矩形图片的边界,外部的那些部分是我们不需要的,将它排除可以降低后续数据处理的难度

crop_png.png

对于GeoTiff有两种制作透明部分的方法,分别是用来对付RGB类和反射波谱类。查看下面的网站获取更多信息:Pix4D | Relectance map vs Orthomosaic

  1. 像PNG格式那样使用额外的alpha图层来表示透明度,适合RGB图像类的GeoTiff,如正摄影像DOM等。像素的数值被均衡和拉伸成为0-255范围内的整数。这个方法可以表示不同的透明度程度,并且不会丢失原本的像素信息

  2. 直接把透明地方的像素值修改为-10000(默认值,或者其他Geotiff无数据标签指定的值)。这个方法适合反射波谱类的Geotiff,如地形图DSM,多光谱图。像素值没有进行任何拉伸处理,不同的图层可以拥有完全不同的数值范围

数据检查#

例如,我们有下面两个不同的Geotiff数据,index_trans.tif 是反射波谱图而 mosaic_trans.tif 是RGB图

ref_rgb.png

具体的像素数据可以用 tifffile 包,或者比较难安装的 rasterio 包(但请务必查看文档里面的GeoTiff无数据掩膜部分: RasterIO docs | Nodata Masks)

>>> import tifffile as tf
>>> tif_rgb = tf.TiffFile("mosaic_trans.tif")   # RGB map
>>> tif_ref = tf.TiffFile("index_trans.tif")   # Reflectance map
>>> page_rgb = tif_rgb.pages[0]  # read the first page of pyramid
>>> page_ref = tif_ref.pages[0]  # 读取金字塔的第一层

检查Geotiff的标签

>>> print(page_ref.tags)
TiffTag 256 ImageWidth @24 SHORT @36 17520
TiffTag 257 ImageLength @44 SHORT @56 19056
...
# nodata tag
TiffTag 42113 GDAL_NODATA @384 ASCII[7] @396 -10000

检查Geotiff像素值

>>> page_rgb.asarray().shape  # RGB
(19056, 17520, 4)
>>> page_ref.asarray().shape  # reflectance
(19056, 17520, 3)

RGB的值和范围

>>> page_rgb.asarray()[:,:,2]  # the third layer
array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8)
>>> page_rgb.asarray()[:,:,2].min()
0
>>> page_rgb.asarray()[:,:,2].max()
255

反射波谱的值和范围

>>> page_ref.asarray()[:,:,2]
array([[-10000., -10000., -10000., ..., -10000., -10000., -10000.],
       [-10000., -10000., -10000., ..., -10000., -10000., -10000.],
       [-10000., -10000., -10000., ..., -10000., -10000., -10000.],
       ...,
       [-10000., -10000., -10000., ..., -10000., -10000., -10000.],
       [-10000., -10000., -10000., ..., -10000., -10000., -10000.],
       [-10000., -10000., -10000., ..., -10000., -10000., -10000.]],
       dtype=float32)
>>> page_ref.asarray()[:,:,2].max()
254.93721