用vispy动画图像堆栈

问题描述 投票:2回答:1

我正在尝试从MATLAB迁移到Python,我在Matlab开发过程中经常依赖的一件事就是能够通过循环遍历图层并调用drawow来快速可视化数据立方体的切片。

tst = randn(1000,1000,100);
for n = 1:size(tst, 3)
    imagesc(tst(:,:,n));
    drawnow;
end

当我在MATLAB中对此进行tic / toc时,它表明该数字正在以大约28fps更新。相反,当我尝试使用matplotlib的imshow()命令执行此操作时,相比之下,即使使用set_data(),它也会以蜗牛的速度运行。

    import matplotlib as mp
    import matplotlib.pyplot as plt
    import numpy as np

    tmp = np.random.random((1000,1000,100))

    myfig = plt.imshow(tmp[:,:,i], aspect='auto')
    for i in np.arange(0,tmp.shape[2]):

        myfig.set_data(tmp[:,:,i])
        mp.pyplot.title(str(i))
        mp.pyplot.pause(0.001)

在我的计算机上,它以大约16fps的速度运行,默认(非常小)的比例,如果我将其调整为更大并且与matlab图形相同,它会减慢到大约5 fps。从一些较旧的线程我看到了使用glumpy的建议,我安装了所有相应的包和库(glfw等),并且包本身工作正常,但它不再支持suggested in a previous thread的简单图像可视化。

然后我下载了vispy,我可以使用this thread的代码作为模板用它来制作图像:

    import sys
    from vispy import scene
    from vispy import app
    import numpy as np

    canvas = scene.SceneCanvas(keys='interactive')
    canvas.size = 800, 600
    canvas.show()

    # Set up a viewbox to display the image with interactive pan/zoom
    view = canvas.central_widget.add_view()

    # Create the image
    img_data = np.random.random((800,800, 3))
    image = scene.visuals.Image(img_data, parent=view.scene)
    view.camera.set_range()

    # unsuccessfully tacked on the end to see if I can modify the figure.
    # Does nothing.
    img_data_new = np.zeros((800,800, 3))
    image = scene.visuals.Image(img_data_new, parent=view.scene)
    view.camera.set_range()

Vispy似乎非常快,看起来它会让我在那里,但你如何用新数据更新画布?谢谢,

python matlab matplotlib vispy glumpy
1个回答
0
投票

请参阅ImageVisual.set_data方法

# Create the image
img_data = np.random.random((800,800, 3))
image = scene.visuals.Image(img_data, parent=view.scene)
view.camera.set_range()

# Generate new data :
img_data_new = np.zeros((800,800, 3))
img_data_new[400:, 400:, 0] = 1.  # red square
image.set_data(img_data_new)
© www.soinside.com 2019 - 2024. All rights reserved.