0

我正在尝试使用 pyplot 动画实时显示 CCD 相机捕获的帧。我编写了一个简短的 python 脚本来测试它,虽然它可以工作,但它的运行不规律。它会快速更新十几个动画帧,然后暂停一秒钟,然后再次更新,然后再次暂停,以此类推。我想让它连续流畅地更新情节,但我不确定我哪里出错了。

我知道这不是调用相机帧缓冲区的部分。我测试了只是在循环中调用它并且它从未减慢速度,所以我认为它在动画帧的实际处理中的某个地方。

我的代码如下:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
import Pixis100
import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    

# Create ccd object
ccd = Pixis100.device()
ccd.snapshot()
ccd.focusStart()
# focusStart() tells the camera to start putting captured frames into the buffer

line, = ax.plot(ccd.getFrame()[:,2:].sum(axis=0)[::-1],'b-')
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)
    return line,

def data_gen():
    while True: yield ccd.getFrame()[:,2:].sum(axis=0)[::-1]

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen,interval=10,blit=True)

plt.show()
ccd.focusStop()
# focusStop() just tells the camera to stop capturing frames

作为参考, ccd.getFrame()[:,2:].sum(axis=0)[::-1] 返回一个 1x1338 整数数组。我认为这对于动画一次处理来说不会太多。

4

1 回答 1

1

问题不在于animation,以下工作正常:

import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

import time

# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = fig.add_subplot(111)    
ax.set_xlim([0, 2 *np.pi])
ax.set_ylim([-1, 1])

th = np.linspace(0, 2 * np.pi, 1000)

line, = ax.plot([],[],'b-', animated=True)
line.set_xdata(th)
# getFrame() makes a call to the CCD frame buffer and retrieves the most recent frame

# animation function
def update(data):    
    line.set_ydata(data)

    return line,

def data_gen():
    t = 0
    while True:
        t +=1
        yield np.sin(th + t * np.pi/100)

# call the animator
anim = animation.FuncAnimation(fig, update, data_gen, interval=10, blit=True)
plt.show()

不稳定可能来自您的图像采集卡,您正在对其进行的计算,或者 gui 在主线程上获得足够的时间来重新绘制的问题。看到保持 QThread 响应所需的 time.sleep() 了吗?

于 2013-07-14T19:46:24.170 回答