python - 动画期间图像不更新情节

标签 python opencv matplotlib matplotlib-animation

问题:

我试图通过循环浏览我保存在目录中的一系列静态图像来模拟实时视频,但是当我添加动画和更新功能时,我的绘图显示为空。

我这样做的背景:

我认为这样做对我来说很重要,而不是完全改变方法,比如先将图像转换为视频然后显示它,因为我真正想测试的是我将添加的图像分析和然后覆盖在每一帧上。最终的应用程序将从相机一一接收帧,并且需要进行一些处理,显示图像+注释+将数据输出为.csv等...我现在正在模拟这个,因为我没有任何处理生成图像的硬件的数量,并且在几个月内不会有它,在此期间我需要设置图像处理,但我确实可以访问一些大约将产生的剧照集。如果它相关,我的模拟图像是 1680x1220 和 1.88 Mb TIFF,尽管我可以根据需要隐藏和压缩它们,并且在最终形式中,分辨率会更高一些,并且可能可以根据需要调整图像格式。

我尝试过的:

  • 我按照一个示例列出了文件夹中的所有文件,以及一个示例 更新情节。但是,当我运行时,该图显示空白 代码。
  • 我添加了一行来打印当前文件名,我可以看到这一点 按预期循环。
  • 如果我刚刚创建,我还确保图像将显示在图中 一个情节并添加一张图像,他们就这样做了。但是,当与 动画功能情节是空白的,我不确定我做了什么 错误/未能包含。
  • 我还尝试在更新中添加 plt.pause() ,但还是这样 没用。
  • 我将间隔增加到 2000,以给它更多的时间,但这没有用。我相信 2000 是极端的,我预计它应该能在 20-30fps 左右工作。达到 0.5fps 告诉我代码错误或不完整,而不仅仅是需要时间读取图像文件的问题。

我很感激没有人拥有我的照片,但它们没什么特别的。我使用了 60 张图像,但我想可以用任意 2 张随机图像进行测试,并将范围(60)设置为范围(2)?

我复制的示例最初通过制作随机数组演示了动画功能,如果我这样做,它将显示一个按预期用随机方 block 更新的绘图。 替换:

A = np.random.randn(10,10)
im.set_array(A)

...用我的图片代替...

im = cv2.imread(files[i],0)

...并且情节仍然是空的/空白。我得到一个名为“Figure1”的窗口(就像使用随机数组时一样),但与数组不同的是,该窗口中没有任何内容。

Plot is blank

完整代码:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import os
import cv2

def update(i):
    im = cv2.imread(files[i],0) 
    print(files[i])
    #plt.pause(0.1)
    return im

path = 'C:\\Test Images\\'

files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.TIFF' in file:
            files.append(os.path.join(r, file))

ani = FuncAnimation(plt.gcf(), update, frames=range(60), interval=50, blit=False)
plt.show()

我是一个Python和编程新手,所以依赖于调整其他人在网上给出的示例,但我对它们的工作原理只有一个简单的理解,最终在语法上进行了大量的试验和错误。我只是想不出任何办法可以让这个工作发挥作用。

为任何帮助干杯!

最佳答案

没有显示任何内容的主要原因是您从未将图像添加到绘图中。我在下面提供了一些代码来完成您想要的操作,请务必查找您好奇或不理解的任何内容!

import glob
import os
from matplotlib import animation
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

IMG_DIRPATH = 'C:\\Test Images\\'  # the folder with your images (be careful about
                                   # putting spaces in directory names!)
IMG_EXT = '.TIFF' # the file extension of your images

# Create a figure, and set to the desired size.
fig = plt.figure(figsize=[5, 5])

# Create axes for the current figure so that images can be sized appropriately.
# Passing in [0, 0, 1, 1] makes the axes fill the whole figure.
# frame_on=False means we won't have a bounding box, and setting xticks=[] and
# yticks=[] means that we won't have pesky tick marks along our image.
ax_props = {'frame_on': False, 'xticks': [], 'yticks': []}
ax = plt.axes([0, 0, 1, 1], **ax_props)

# Get all image filenames.
img_filepaths = glob.glob(os.path.join(IMG_DIRPATH, '*' + IMG_EXT))

def update_image(img_filepath):
    # Remove all existing images on the axes, and restore our settings.
    ax.clear()
    ax.update(ax_props)

    # Read the current image.
    img = mpimg.imread(img_filepath)

    # Add the current image to the plot axes.
    ax.imshow(img)

anim = animation.FuncAnimation(fig, update_image, frames=img_filepaths, interval=250)
plt.show()

关于python - 动画期间图像不更新情节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59192549/

相关文章:

c++ - 在 OpenCV 中显式复制像素值时出现空白帧

python - 如何使用两个重新排列的轴设置 matplotlib 的背景?

python - 带有 Pandas 数据点的线图

python - 为什么这个 python 代码不打印字谜?

javascript - 如何使 javascript 在 Flask 上与 python 一起工作?

python - 使用元组的 Python 列表将多行插入数据库

java - 将 C++ 中的数组指针转换为 Java (OpenCV)

python - OpenCV Python - 替换图像中的 channel

python - latex 颜色未在 matplotlib 文本中呈现

python - 如何将列表值分配给excel文件中的列?