browsing image sequence with a slider in bokeh(在bokeh中使用滑块浏览图像序列)
本文介绍了在bokeh中使用滑块浏览图像序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Bokeh显示一系列图像,并希望以交互方式滑动或播放该序列。当我运行我的脚本时,它显示第一个图像,但当我拖动滑块或单击播放按钮时,图像不会更新。我的代码如下:
import numpy as np
import bokeh.io
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import layout
from bokeh.models import ColumnDataSource, HoverTool, SingleIntervalTicker, Slider, Button, Label
from bokeh.palettes import Spectral1
from skimage.external import tifffile as T
img=T.imread('C:/Users/UserXX/Desktop/Image_Sequence.tif')
sources={}
frames=list(range(0,img.shape[0]))
for frame in frames:
sources[frame]=ColumnDataSource(data=dict(image=[img[frame,:,:]]))
source1 = sources[0]
p_img = figure(plot_width=694, plot_height=520, x_range=[0,1388], y_range=[0, 1040])
label = Label(x=1.1, y=18, text=str(frames[0]), text_font_size='70pt', text_color='#eeeeee')
p_img.add_layout(label)
p_img.image(image='image', x=[0], y=[0], dw=[1388], dh=[1040],source=source1, palette="Spectral11")
slider = Slider(start=frames[0], end=frames[-1], value=frames[0],step=1, title="Frame")
def animate_update():
frame = slider.value + 1
if frame > frames[-1]:
frame = frames[0]
slider.value = frame
def slider_update(attr, old, new):
global source
global sources
frame = slider.value
label.text = str(frame)
source= sources[frame]
slider.on_change('value', slider_update)
def animate():
if button.label == '► Play':
button.label = '❚❚ Pause'
curdoc().add_periodic_callback(animate_update, 200)
else:
button.label = '► Play'
curdoc().remove_periodic_callback(animate_update)
button = Button(label='► Play', width=60)
button.on_click(animate)
l = layout([[p_img],[slider, button],], sizing_mode='scale_width')
curdoc().add_root(l)
curdoc().title = "Image_Sequence"
我以此为例: https://github.com/bokeh/bokeh/blob/master/examples/app/gapminder/main.py 我不确定我将新的图像数据传递给源的方式是否正确。 有什么建议吗?
推荐答案
代码中有几个不同的地方需要修复或改进,因此学习和模拟这个简化的完整工作示例可能会更容易:
import numpy as np
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, Slider
from bokeh.plotting import figure
N = 100
x_ = np.linspace(0, 10, 200)
y_ = np.linspace(0, 10, 200)
z_ = np.linspace(0, 10, N)
x, y, z = np.meshgrid(x_, y_, z_, indexing='xy')
data = np.sin(x+z)*np.cos(y)
source = ColumnDataSource(data=dict(image=[data[:, :, 0]]))
p = figure(x_range=(0, 10), y_range=(0, 10))
p.image(image='image', x=0, y=0, dw=10, dh=10, source=source, palette="Spectral11")
slider = Slider(start=0, end=(N-1), value=0, step=1, title="Frame")
def update(attr, old, new):
source.data = dict(image=[data[:, :, slider.value]])
slider.on_change('value', update)
curdoc().add_root(column(p, slider))
请注意,每次更改滑块时都会发生更新,即使是200x200,事情也可能开始变得有点滞后(至少在我的系统上是这样)。因此,您可能还需要考虑Throttling in Bokeh application
中介绍的技术这篇关于在bokeh中使用滑块浏览图像序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
编程基础网
本文标题为:在bokeh中使用滑块浏览图像序列
基础教程推荐
猜你喜欢
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
