首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Glumpy将NumPy数组显示为连续更新图像

用Glumpy将NumPy数组显示为连续更新图像
EN

Stack Overflow用户
提问于 2010-10-15 13:15:31
回答 2查看 4.6K关注 0票数 8

我已经使用NumPy和SciPy在Python中运行了一个仿真模型,它生成一个2D NumPy数组作为每次迭代的输出。我一直使用matplotlib和imshow函数将此输出显示为图像。但是,我发现了Glumpy,在它的文档页上写着:

由于有了IPython shell,可以在交互模式下运行glumpy,当它们的内容发生更改时,您可以在显示的数组中体验实时更新。

然而,我似乎想不出如何用他们给出的例子来解决这个问题。基本上,我的模型作为一个函数运行,其中包含一个大的for循环,用于循环我正在运行的迭代次数。在for循环每次迭代结束时,我想显示数组。目前,我正在使用matplotlib将图像保存到png文件中,因为通过matplotlib在屏幕上显示它似乎会冻结python进程。

我肯定有办法用Glumpy来做这件事,我只是不知道怎么做,我找不到任何有用的教程。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-01-24 04:48:04

Glumpy文档相当不存在!下面是一个简单的模拟示例,将数组可视化与glumpymatplotlib进行比较

代码语言:javascript
复制
import numpy as np
import glumpy
from OpenGL import GLUT as glut
from time import time
from matplotlib.pyplot import subplots,close
from matplotlib import cm

def randomwalk(dims=(256,256),n=3,sigma=10,alpha=0.95,seed=1):
    """ A simple random walk with memory """
    M = np.zeros(dims,dtype=np.float32)
    r,c = dims
    gen = np.random.RandomState(seed)
    pos = gen.rand(2,n)*((r,),(c,))
    old_delta = gen.randn(2,n)*sigma
    while 1:
        delta = (1.-alpha)*gen.randn(2,n)*sigma + alpha*old_delta
        pos += delta
        for ri,ci in pos.T:
            if not (0. <= ri < r) : ri = abs(ri % r)
            if not (0. <= ci < c) : ci = abs(ci % c)
            M[ri,ci] += 1
        old_delta = delta
        yield M

def mplrun(niter=1000):
    """ Visualise the simulation using matplotlib, using blit for 
    improved speed"""
    fig,ax = subplots(1,1)
    rw = randomwalk()
    im = ax.imshow(rw.next(),interpolation='nearest',cmap=cm.hot,animated=True)
    fig.canvas.draw()
    background = fig.canvas.copy_from_bbox(ax.bbox) # cache the background

    tic = time()
    for ii in xrange(niter):
        im.set_data(rw.next())          # update the image data
        fig.canvas.restore_region(background)   # restore background
        ax.draw_artist(im)          # redraw the image
        fig.canvas.blit(ax.bbox)        # redraw the axes rectangle

    close(fig)
    print "Matplotlib average FPS: %.2f" %(niter/(time()-tic))

def gprun(niter=1000):
    """ Visualise the same simulation using Glumpy """
    rw = randomwalk()
    M = rw.next()

    # create a glumpy figure
    fig = glumpy.figure((512,512))

    # the Image.data attribute is a referenced copy of M - when M
    # changes, the image data also gets updated
    im = glumpy.image.Image(M,colormap=glumpy.colormap.Hot)

    @fig.event
    def on_draw():
        """ called in the simulation loop, and also when the
        figure is resized """
        fig.clear()
        im.update()
        im.draw( x=0, y=0, z=0, width=fig.width, height=fig.height )

    tic = time()
    for ii in xrange(niter):
        M = rw.next()           # update the array          
        glut.glutMainLoopEvent()    # dispatch queued window events
        on_draw()           # update the image in the back buffer
        glut.glutSwapBuffers()      # swap the buffers so image is displayed

    fig.window.hide()
    print "Glumpy average FPS: %.2f" %(niter/(time()-tic))

if __name__ == "__main__":
    mplrun()
    gprun()

使用matplotlibGTKAgg作为后端,并使用blit避免每次绘制背景,我可以达到大约95个FPS。使用Glumpy,我可以得到大约250-300FPS,尽管我目前在笔记本电脑上的图形设置相当糟糕。话虽如此,Glumpy在工作上还是比较谨慎的,除非您正在处理巨大的矩阵,或者您需要一个非常高的框架,不管出于什么原因,我还是坚持使用matplotlibblit

票数 11
EN

Stack Overflow用户

发布于 2018-03-31 23:25:40

使用公式0.2.8,您可以使用pf.screen创建一个非阻塞屏幕:

代码语言:javascript
复制
import pyformulas as pf
import numpy as np

canvas = np.floor(np.random.normal(scale=50, size=(480,640,3)) % 256).astype(np.uint8)
screen = pf.screen(canvas)

while screen.exists():
    canvas = np.floor(np.random.normal(scale=50, size=(480,640,3)) % 256).astype(np.uint8)
    screen.update(canvas)

#screen.close()

免责声明:我是肾盂配方的维护者。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3942549

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档