user3007270 user3007270 - 3 years ago 199
Python Question

How can I produce an image array as the average of multiple image arrays?

I'm trying to create the average heatmap from multiple heatmaps.

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import misc

f1 = misc.imread('heatmap1.png')
f2 = misc.imread('heatmap2.png')
f3 = misc.imread('heatmap3.png')
f_avg=np.average([f1,f2,f3],axis=0)
plt.imshow(f_avg)
plt.show()


This is heatmap1.png:

This is 'heatmap1'

This is the result of averaging the 3 heatmaps:

enter image description here

It looks like this is not the way to compute the avg of color arrays.

Am I missing something here?

Answer Source

You should use plt.imshow(f_avg.astype(np.uint8)) so that imshow can detect that this is an image and not arbitrary floating-point data. I don't have access to all three of your images, but if I test using f1 three times, or with random noise images, this change makes the code behave as I would expect (where it doesn't without the change).

Also beware that these image arrays have RGB values and an alpha value that are all getting averaged - the average of these three images won't necessarily be the same as the heatmap you'd get with all of the data included (but it's a good rough estimate, if that's all you have access to).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download