我想创建一个动画,其中构成动画的每个单独的框架都包含一个覆盖有透明圆圈和中央十字线的图像。在每个新帧处,一切都应该更新(即图像改变,并且圆圈和十字线都移动)。基本上,我想检查一下我是否可以跟踪图像中的移动特征。
我可以更新十字架,但图像和圆圈更有问题。我搜索了以前可能有帮助的答案(并尝试了一些建议),但还没有任何工作。任何帮助都会很棒 - 谢谢!
P.S。目前我在开始动画之前初始化了剧情,并且有了一个init_func(我只是尝试了两个并且两者都离开了。)
import scipy as sp, numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
def movie(images, xpos, ypos, nim=100, nxy=10):
"""
nim = no. of images
nxy = no. of pixels along x and y axes in each image
images = array of shape nim x nxy x nxy. i.e. 100 images of 10x10 pixels
xpos = x-coordinate of centre of circle (and cross-hairs). Array of length nim
ypos = y-coordinate of centre of circle (and cross-hairs). Array of length nim
"""
# set up figure
fig = plt.figure(1, (10,10))
plt.clf()
fig.set_dpi(100)
# initialise plot before running animation
im = plt.imshow([[],[]], interpolation='nearest', cmap='hot', origin='lower')
circ = plt.Circle(([], []), 3, color='c', fill=False, lw=2)
cross, = plt.plot([], [], 'cx', ms=7, mew=2)
# initial function to 'populate' plot with empty image, circle and cross hairs for animation
def init_movie():
""" """
im = plt.imshow([[],[]], interpolation='nearest', cmap='hot', origin='lower')
circ = plt.Circle(([], []), 3, color='c', fill=False, lw=2)
cross, = plt.plot([], [], 'cx', ms=7, mew=2)
return im, circ, cross,
# animation function: update the image, and the locations of the circle and central cross hairs
def animate_movie(i):
""" """
im.set_data( images[i] )
circ.centre = (xpos[i], ypos[i])
cross.set_data( xpos[i], ypos[i] )
return im, circ, cross,
# run animation
anim = animation.FuncAnimation(fig, animate_movie, init_func=init_movie,
frames=nim, interval=100, blit=True)
# save animation
anim.save('animation.mp4', fps=10, \
extra_args=['-vcodec', 'h264', '-pix_fmt', 'yuv420p'])
plt.show()
return