Python 使用 matplotlib 显示图像序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17888593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:24:48  来源:igfitidea点击:

Display sequence of images using matplotlib

pythonopencvmatplotlib

提问by Yash

I have this simple python script using OpenCV to load images from a folder and display them in a loop. I want to reproduce this effect using matplotlib.

我有这个简单的 python 脚本,使用 OpenCV 从文件夹加载图像并循环显示它们。我想使用matplotlib.

import cv2 as cv
import os

im_files = [for f in os.listdir('.') if f[-3:] == 'png']

for f in im_files:
    im = cv.imread(f, 0) #read image in greyscale
    cv.imshow('display', im)
    cv.waitKey(1)

cv.destroyAllWindows()

I tried the following script but the pyplot window which opens to display the plots becomes un responsive.

我尝试了以下脚本,但打开以显示绘图的 pyplot 窗口变得没有响应。

import pylab as pl
import os

files = [f for f in os.listdir('.') if f[-3:] == 'png']
pl.ion()
for f in files:
    im=pl.imread(f)
    pl.imshow(im)
    pl.draw()

I have googled a lot but couldn't find any solution. How do I go about doing this? I am using Anaconda 1.6 32bit on Windows 8.

我用谷歌搜索了很多,但找不到任何解决方案。我该怎么做?我在 Windows 8 上使用 Anaconda 1.6 32 位。

采纳答案by tacaswell

img = None
for f in files:
    im=pl.imread(f)
    if img is None:
        img = pl.imshow(im)
    else:
        img.set_data(im)
    pl.pause(.1)
    pl.draw()

回答by Bily

I have implemented a handy script based on matplotlib that just suits your need and much more. Check it here

我已经实现了一个基于 matplotlib 的方便的脚本,它正好适合您的需要等等。在这里检查

In your case, the following snippet should work:

在您的情况下,以下代码段应该有效:

import os
from scipy.misc import imread

img_files = [for f in os.listdir('.') if f[-3:] == 'png']

# redraw_fn draw frame f in a image sequence
def redraw_fn(f, axes):
    img_file = img_files[f]
    img = imread(img_file)
    if not redraw_fn.initialized:
        redraw_fn.im = axes.imshow(img, animated=True)
        redraw_fn.initialized = True
    else:
        redraw_fn.im.set_array(img)
redraw_fn.initialized = False

videofig(len(img_files), redraw_fn, play_fps=30)

回答by robertofbaycot

I like the following way of doing this which is really straight-forward and allows the whole figure to be updated, including title, labels etc. rather than just the image.

我喜欢以下这样做的方式,它非常简单,允许更新整个图形,包括标题、标签等,而不仅仅是图像。

import numpy as np
from matplotlib import pyplot as plt

for j in range(0,3):
    img = np.random.normal(size=(100,150))
    plt.figure(1); plt.clf()
    plt.imshow(img)
    plt.title('Number ' + str(j))
    plt.pause(3)

A random image is formed.

形成随机图像。

plt.figurecreates the figure the first time round if it does not already exist, and thereafter just makes figure 1 the current figure.

plt.figure如果图形不存在,则第一次创建图形,此后仅将图形 1 设为当前图形。

plt.clfclears the figure so subsequent updates don't overlay each other. The image is then displayed with a title.

plt.clf清除数字,以便后续更新不会相互重叠。然后显示带有标题的图像。

The plt.pausestatement is the key, since this causes the display to be updated - including title, labels etc.

plt.pause声明是关键,因为这将导致显示被更新-包括标题,标签等。