Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动画期间python绘图中的图像未更新_Python - Fatal编程技术网

动画期间python绘图中的图像未更新

动画期间python绘图中的图像未更新,python,Python,问题: 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

问题:

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()
我试图通过循环浏览保存在目录中的一系列静态图像来模拟实时视频,但当我添加动画和更新功能时,我的绘图显示为空

我为什么这样做的背景:

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

我尝试过的:

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()
  • 我按照一个示例列出了文件夹中的所有文件,并给出了一个示例 更新绘图。但是,当我运行 代码
  • 我添加了一行来打印当前文件名,我可以看到这一点 按预期骑自行车
  • 我还确保,如果我只是创建,图像将显示在绘图中 一个绘图并添加一个图像,他们就这样做了。但是,当与 动画功能情节是空白的,我不确定我做了什么 错误/未能包括
  • 我还尝试在更新中添加一个plt.pause(),但这次 没用
  • 我把间隔时间增加到2000年,以给它更多的时间,但这不起作用。我相信2000是极端的,我希望它能以20-30fps的速度工作。转到0.5fps会告诉我代码错误或不完整,而不仅仅是需要时间读取图像文件的问题
我很感激没有其他人有我的照片,但它们没什么特别的。我用的是60张图片,但我想它可以用任意两张随机图片进行测试,并将范围(60)设置为范围(2)

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

A = np.random.randn(10,10)
im.set_array(A)
…取而代之的是我的形象

im = cv2.imread(files[i],0)
…并且绘图仍为空/空白。我得到了一个名为“Figure1”的窗口(就像使用随机数组时),但与数组不同的是,此窗口中没有任何内容

完整代码:

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()

非常感谢你,这很有效,我很感谢你的评论,这有助于我理解它。我添加了“导入操作系统”,现在我将研究如何使图像灰度化,我应该能够弄清楚这一点。这是一个巨大的帮助,再次感谢!嗯,奇怪的事情发生了。如果我让它运行(一遍又一遍地循环我的60个图像),那么它会在第850帧±10帧时失败,并出现一系列错误,最后一个错误,我假设是关键错误,即:MemoryError:无法分配具有形状(1220、1620)和数据类型bool的数组。@d显然,对图像的引用没有被释放,所以它的内存不足。我已经更新了我的答案,并对此进行了修复
ax.clear()
将释放对所显示图像的引用,我们只需要调用
ax.update
来重置我们想要的轴的属性,即
frame\u on=False、xticks=[]和yticks=[]
。再次感谢,我明天将尝试调整。