gpt4 book ai didi

python - 用于改变 Bokeh 图像图中使用的切片的交互式 slider

转载 作者:太空狗 更新时间:2023-10-30 01:07:17 24 4
gpt4 key购买 nike

我对使用 Bokeh 将图像放入 IPython 笔记本很感兴趣。特别是我经常与之交互的数据类型是具有 3 个或更多维度的多维 NumPy 数组。以 3 维数组为例。一个经常遇到的例子是 RGB 图像。三个维度是xycolor

我对使用 Bokeh 在 IPython 笔记本中绘制单个图像 channel 很感兴趣。我想提供一个交互式 slider ,允许 IPython notebook 的用户单击第 3 维的每个索引,在本例中为颜色。

我下面的代码(在 IPython 笔记本中运行时)成功显示了第一个颜色 channel 的绘图。但我无法弄清楚是什么导致了我对 interact 的调用出错。我是否正确定义了我的 ColumnDataSource 并在 Bokeh 图构造中正确引用了它?

# imports
import numpy as np
from scipy.misc import imread

from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import ColumnDataSource
from bokeh.palettes import Greys9

from IPython.html.widgets import interact

# enable Bokeh to plot to the notebook 
output_notebook()

# Make the Bokeh plot of the "first" layer of the 3D data
## This part works
TOOLS="pan, box_zoom, reset, save"

# The image from https://windycitizensports.files.wordpress.com/2011/10/baboon.jpg?w=595
RGB_image = imread('/Users/curt/Downloads/BaboonRGB.jpg')

nx, ny, n_colors = RGB_image.shape

source = ColumnDataSource(data={'image': RGB_image[:, :, 0]})

p = figure(title="ColorChannel",
tools=TOOLS,
x_range=[0, nx],
y_range=[0, ny],
)

p.image([source.data['image'][::-1, :]-1],
x=0,
y=0,
dh=[ny],
dw=[nx],
palette=Greys9,
source=source,
)

show(p)

# try to add interactive slider
## This part does not work & gives a JavaScript error

def update(idx=0):
global RGB_image
source.data['image'] = RGB_image[:, :, idx]
source.push_notebook()

interact(update, idx=(0, 2))

Javascript 错误是:

Javascript error adding output!
TypeError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The provided float value is non-finite.
See your browser Javascript console for more details.

我不确定这是怎么回事。我尝试在定义 RGB_Image 后立即执行 RGB_Image = RGB_Image.astype(float) 将其强制转换为 float ,但我遇到了同样的错误。

最佳答案

本质上,您的预定义图像和您尝试更新的图像之间的数据格式不一致。

确实有效的解决方法:

def update(idx=0):
global RGB_image
source.data['image'] = RGB_image[:, :, idx]
p.image([source.data['image'][::-1, :]-1],
x=0,
y=0,
dh=[ny],
dw=[nx],
palette=Greys9,
source=source,
)
show(p)

interact(update, idx=(0, 2))

我承认,一遍又一遍地定义图像并不是执行此操作的首选方法,但它至少应该让您了解要查看的位置。

关于python - 用于改变 Bokeh 图像图中使用的切片的交互式 slider ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31979476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com