我目前正在開發一個行爲系統,它將利用移動的酒吧,旋轉酒吧,交替的領域,甚至小型隨機移動物體。最初所有這些都是在matlab中編程的,並且效果很好。然而,由於與最終將使用代碼的硬件不兼容,該項目已被轉移到python。OpenCV或matplotlib動畫建設?
我已經開始使用python中的matplotlib模塊編程系統,並取得了一些好成績。我利用matplotlib中的動畫函數來產生快速移動物體的一致且流暢的運動。然而,當我深入研究用matplotlib進行編程時,我注意到了一些問題。其中一個特別的是物體的流體運動並不像以前想象的那樣流暢。
因爲我將利用opencv作爲行爲系統的另一部分,所以我想知道opencv是否比matplotlib具有任何特別的優勢,特別是關於繪製速率和動畫。
我會在下面進行更詳細的介紹。
這是構建動畫腳本的一部分,注意這個版本崩潰了,我還沒有弄清楚爲什麼。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import time
fig = plt.figure()
ax = fig.add_subplot(111)
fig.patch.set_facecolor([0,0,0])
fig.tight_layout()
plt.xlim(-100, 100)
plt.ylim(-100, 100)
plt.axis('off')
# List of variables
width = 1
bars = 20
spacing = 20
step = .01
direction = -1
RB = [] # Establish RB as a Python list
for a in range(bars):
RB.append(patches.Rectangle((a*spacing-200,-100), width, 200,
color=[1,0,0], alpha=0.50))
def init():
for a in range(bars):
ax.add_patch(RB[a])
return RB
def animate(i):
for i in range(bars):
temp = np.array(RB[i].get_xy())
if temp[0] > 200:
temp[0] = -199
elif temp[0] < -200:
temp[0] = 199
else:
temp[0] = temp[0] + step*direction;
RB[i].set_xy(temp)
return RB
t = time.time()
plt.show()
while t < timewidow
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=30,
interval=1,
blit=True)
fig.clf()
做功的代碼就在這裏。只是每個單獨的對象不會彼此同步移動。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import matplotlib.animation as animation
import time
fig = plt.figure()
ax = fig.add_subplot(111)
fig.patch.set_facecolor([0,0,0])
fig.tight_layout()
plt.xlim(-100, 100)
plt.ylim(-100, 100)
plt.axis('off')
# List of variables
width = 1
bars = 20
spacing = 20
step = .01
direction = -1
RB = [] # Establish RB as a Python list
for a in range(bars):
RB.append(patches.Rectangle((a*spacing-200,-100), width, 200,
color=[1,0,0], alpha=0.50))
def init():
for a in range(bars):
ax.add_patch(RB[a])
return RB
def animate(i):
for i in range(bars):
temp = np.array(RB[i].get_xy())
if temp[0] > 200:
temp[0] = -199
elif temp[0] < -200:
temp[0] = 199
else:
temp[0] = temp[0] + step*direction;
RB[i].set_xy(temp)
return RB
anim = animation.FuncAnimation(fig, animate,
init_func=init,
frames=30,
interval=1,
blit=True)
plt.show()
沒想到這個方面,謝謝指點出來。第一組代碼中while循環的目的是試圖在短時間內動畫並讓它停止運行。但是,當我試圖按順序運行代碼plt.show() - > animate - > plt.clf。這個數字會出現並立即消失。 –
要停止動畫,您可以使用'anim.event_source.stop()',參見[這個問題](https://stackoverflow.com/questions/16732379/stop-start-pause-in-python-matplotlib-animation) 。你可以通過點擊鼠標或[另一個計時器]來觸發它(https://matplotlib.org/examples/event_handling/timers.html) – ImportanceOfBeingErnest