Holoviews RGB 坐标
Holoviews RGB coordinates
我正在尝试将多波段 tif 文件(4 波段 - [蓝色、绿色、红色、红外线])读入 xarray, then display as RGB using HoloViews within a Jupyter notebook. For reference, I'm loosely following the RGB png example here: http://holoviews.org/reference/elements/matplotlib/RGB.html
最终的 RGB 图像确实显示了,但是,我通过使用 np.dstack 组合 DataArrays 失去了 x/y 坐标维度。最终图像中的 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]
显示所需坐标的示例,使用从上面的 r_ds
、g_ds
和 b_ds
创建的 HoloViews 图像:
显示不需要的坐标的示例,使用 HoloViews RGB,在上面命名为 rgb
:
评论中提到的 Landsat 示例使用 (xdim, ydim, R, G, B, A)
形式的 data
参数,它将所需的 x/y 坐标应用于图像。
陆地卫星示例:http://datashader.org/topics/landsat.html
rgb = hv.RGB(
(
ximg['x'],
ximg['y'],
r.data[::-1],
g.data[::-1],
b.data[::-1]
),
vdims=list('RGB')
)
我正在尝试将多波段 tif 文件(4 波段 - [蓝色、绿色、红色、红外线])读入 xarray, then display as RGB using HoloViews within a Jupyter notebook. For reference, I'm loosely following the RGB png example here: http://holoviews.org/reference/elements/matplotlib/RGB.html
最终的 RGB 图像确实显示了,但是,我通过使用 np.dstack 组合 DataArrays 失去了 x/y 坐标维度。最终图像中的 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]
显示所需坐标的示例,使用从上面的 r_ds
、g_ds
和 b_ds
创建的 HoloViews 图像:
显示不需要的坐标的示例,使用 HoloViews RGB,在上面命名为 rgb
:
评论中提到的 Landsat 示例使用 (xdim, ydim, R, G, B, A)
形式的 data
参数,它将所需的 x/y 坐标应用于图像。
陆地卫星示例:http://datashader.org/topics/landsat.html
rgb = hv.RGB(
(
ximg['x'],
ximg['y'],
r.data[::-1],
g.data[::-1],
b.data[::-1]
),
vdims=list('RGB')
)