2013-07-12 191 views
0

我試圖使用pyplot動畫實時顯示由CCD相機捕獲的幀。我寫了一個簡短的python腳本來測試這個,並且在它工作的時候,它的確是不正常的。它會快速更新十幾個動畫幀,然後暫停一秒鐘,然後再次更新,然後再次暫停,依此類推。我希望能夠持續平穩地更新劇情,但我不確定我出錯的位置。Pyplot動畫不連續更新

我知道它不是調用相機幀緩衝區的部分;我測試過只是在一個循環中調用它,並且它永遠不會放慢速度,所以我認爲它在動畫幀的實際處理中處於某個位置。

我的代碼是下面:

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:]。總和(軸= 0)[:: - 1]返回整數的1x1338陣列。我不認爲這對動畫一次處理太多了。

+0

如果您增加間隔,它會運行得更好嗎?我會指出10ms是100fps。我懷疑這裏的罪魁禍首是你正在使用的gui-frame工作,不能以此速度重畫畫布。這也不清楚你是否有問題或只是抱怨。 – tacaswell

+0

更改間隔時間不起作用。正如我所描述的,我的問題是什麼會導致情節更新不正常,如果這是pyplot的問題,並且不同的繪圖庫會更好地工作,或者如果pyplot內有某些東西可以解決此問題。 – camronm21

回答

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 = 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) 

的choppyness可能來自你的圖像採集卡,你正在做它的計算,或者與GUI得到足夠的時間問題在主線上重新繪製。請參閱time.sleep() required to keep QThread responsive?