创建覆盖有圆和十字的图像的动画

2024-06-17 13:29:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个动画,其中每个单独的帧组成的动画包括一个透明的圆和中央十字线覆盖的图像。在每一个新的帧上,所有的东西都应该更新(即图像改变,圆圈和十字线都移动)。基本上,我想检查我是否可以跟踪图像中的移动特征。你知道吗

我可以更新十字架,但图像和圆圈是更大的问题。我搜索了以前可能有用的答案(并尝试了一些建议),但什么都没用。任何帮助都会很好-谢谢!你知道吗

另外,目前我在开始动画之前初始化了情节,并且有一个初始化函数(我只是两个都试过,两个都留着)。你知道吗

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

Tags: andof图像pltmovieimagescrossim