用vispy动画图像堆栈

时间:2018-07-07 00:14:47

标签: python matlab matplotlib vispy glumpy

我正尝试从MATLAB迁移到Python,在Matlab开发过程中我经常依赖的一项功能是能够通过遍历图层并调用drawow来快速可视化数据多维数据集的切片,例如

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

在MATLAB中执行该命令时,表明该图以约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等)一起安装,该软件包本身可以正常工作,但不再支持{{ 3}}。

然后我下载了vispy,并可以使用suggested in a previous 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看起来非常快,这看起来会让我到达那里,但是如何使用新数据更新画布?谢谢

1 个答案:

答案 0 :(得分: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)