Holoviews颜色坐标

2024-04-25 23:08:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将一个多波段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]

显示所需坐标的示例,使用从上面的r_dsg_dsb_ds创建的全息视图图像: desired coords

使用HoloViews RGB,上面的rgb显示了不需要的坐标: undesired coords


Tags: to图像typenpdsrgbdatasetclass
1条回答
网友
1楼 · 发布于 2024-04-25 23:08:19

注释中提到的Landsat示例使用data形式的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')
)

enter image description here

相关问题 更多 >