开发者社区> 问答> 正文

在matplotlib中为合成图像创建动画?

我正在加载一组图像,我有一个相应的“矩形补丁”,我想添加到每个图像。我想把它们作为一个连续的动画来播放。

frames = np.load('../data/carseq.npy') #frames
bbox = np.load('carseqrects.npy')  #bounding boxes

final_=[]
fig,ax = plt.subplots(1)
for i in range(num_frames):
    box = bbox[i]
    final_.append([plt.imshow(frames[:,:,i], cmap='Greys_r',animated=True)])
    final_.append([ax.add_patch(patches.Rectangle((box[0],box[1]),height+1,width+1, linewidth=2, 
                  edgecolor='red',fill=False))])

ani = animation.ArtistAnimation(fig, final_, interval=100, blit=True,
                                repeat_delay=1000)

plt.show()

我得到了这段代码的根本错误,

final_.append([ax.add_patch(patches.Rectangle((box[0],box[1]),height+1,width+1, linewidth=2, 
                  edgecolor='red',fill=False))])

只是添加了一个白色的矩形,上面有一个红色的矩形,所以矩形是在前一帧被清除后绘制的——这就创建了一个“闪烁”的效果(因为矩形并不是完全画在帧本身)。还有其他方法吗? 问题来源StackOverflow 地址:/questions/59385141/creating-an-animation-for-composite-images-in-matplotlib

展开
收起
kun坤 2019-12-26 10:48:32 589 0
1 条回答
写回答
取消 提交回答
  • 传递给ArtistAnimation的艺术家列表可以是列表的列表,其中列表的每个“行”包含需要在当前框架上绘制的所有艺术家。

    frames = np.load('../data/carseq.npy') #frames
    bbox = np.load('carseqrects.npy')  #bounding boxes
    
    final_=[]
    fig,ax = plt.subplots(1)
    for i in range(num_frames):
        cur_frame = []
        box = bbox[i]
        cur_frame.append(ax.imshow(frames[:,:,i], cmap='Greys_r',animated=True))
        cur_frame.append(ax.add_patch(patches.Rectangle((box[0],box[1]),height+1,width+1, linewidth=2, 
                      edgecolor='red',fill=False)))
        final_.append(cur_frame)
    
    ani = animation.ArtistAnimation(fig, final_, interval=100, blit=True,
                                    repeat_delay=1000)
    
    plt.show()
    
    2019-12-26 10:48:38
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
复杂环境下的视觉同时定位与地图构建 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载