总结一堆图像

时间:2014-03-31 21:09:47

标签: python image

我有一堆图像,我正在寻找一种方法

  • 加载前十张图片;
  • 求和相应的数组;
  • 绘制总和的结果。

有两个图像,代码看起来像

import matplotlib
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from PIL import Image

filename_1 = ('images/image_001.txt')
filename_2 = ('images/image_002.txt')
matrix_1 = np.loadtxt(filename_1)
matrix_2 = np.loadtxt(filename_2)
matrix = (matrix_1 + matrix_2)
plt.imshow(matrix, cmap = cm.Greys_r, interpolation='none')
plt.show()

您如何将其扩展到循环?

2 个答案:

答案 0 :(得分:0)

我不知道matplotlib如何处理矩阵,因此您可能需要编辑格式化矩阵的方式。

import matplotlib
from pylab import *
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from PIL import Image

matrix = []

for num in range(20):
    filename = ('images/image_0' + str((num + 1)) + '.txt')
    matrix.append(np.loadtxt(filename))

plt.imshow(matrix, cmap = cm.Greys_r, interpolation='none')
plt.show()

请在下面用问题发表评论。

答案 1 :(得分:0)

我将如何做到这一点。

import matplotlib
from pylab import *
import numpy as np
import matplotlib.pyplot as plt

image_iter = ('images/image_%03d.txt' % i for i in xrange(1, 11))
image_sum = np.loadtxt(next(image_iter))

for image in image_iter:
   image_sum += np.loadtxt(image)

plt.imshow(image_sum, cmap=cm.Greys_r, interpolation='none')
plt.show()