我试图将一个多波段tif文件(4个波段-[蓝、绿、红、红外])读入xarray,然后在Jupyter笔记本中使用HoloViews显示为RGB。作为参考,我大致遵循RGB png示例:http://holoviews.org/reference/elements/matplotlib/RGB.html
最终的RGB图像确实显示出来了,但是,通过使用组合数据数组,我丢失了x/y坐标维度np.D堆栈. 最终图像中的x/y坐标默认为~-0.5-+0.5。在
我不知道如何在整个过程中处理坐标尺寸,或者可能如何将原始坐标尺寸应用于最终图像。在
# read .tif
ximg = xarray.open_rasterio('path/to/tif')
print('1.', type(ximg), ximg.coords['x'].values)
# convert to hv.Dataset
r_ds = hv.Dataset(ximg[2,:,:], kdims=['x','y'], vdims='Value')
g_ds = hv.Dataset(ximg[1,:,:], kdims=['x','y'], vdims='Value')
b_ds = hv.Dataset(ximg[0,:,:], kdims=['x','y'], vdims='Value')
print('2.', type(r_ds), r_ds.dimension_values('x'))
# scale to uint8
r = np.squeeze((r_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
g = np.squeeze((g_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
b = np.squeeze((b_ds.data.to_array().astype(np.float64)/8190)*255).astype('uint8')
print('3.', type(r), r.coords['x'].values)
# combine to RGB
dstack = np.dstack([r, g, b]) # lose coordinate dimensions here
print('4.', type(dstack), 'NO COORDS')
rgb = hv.RGB(dstack, kdims=['x','y'])
print('5.', type(rgb), rgb.dimension_values('x'))
1. <class 'xarray.core.dataarray.DataArray'> [557989.5 557992.5 557995.5 ... 563194.5 563197.5 563200.5]
2. <class 'holoviews.core.data.Dataset'> [557989.5 557989.5 557989.5 ... 563200.5 563200.5 563200.5]
3. <class 'xarray.core.dataarray.DataArray'> [557989.5 557992.5 557995.5 ... 563194.5 563197.5 563200.5]
4. <class 'numpy.ndarray'> NO COORDS
5. <class 'holoviews.element.raster.RGB'> [-0.49971231 -0.49971231 -0.49971231 ... 0.49971231 0.49971231
0.49971231]
注释中提到的Landsat示例使用
data
形式的data
参数,该参数将所需的x/y坐标应用于图像。在陆地卫星示例:http://datashader.org/topics/landsat.html
相关问题 更多 >
编程相关推荐