2014-03-31 29 views
0

我有圖像的堆棧和我正在尋找一種方式來總和圖像

  • 負載的前十個圖像的堆棧;
  • 求和對應的數組;
  • 繪製總和的結果。

有兩個圖像,代碼看起來像

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

你如何把它擴展到一個循環?

+0

你是說只是做你在那裏,但在所有10個圖像一次循環? –

+0

這就是我正在想的 –

回答

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

好,趕快幫忙! –