Mean value of each channel of several images

Viewed 11182

I want to compute mean value for every RGB channel through all dataset stored in a numpy array. I know it's done with np.mean and I know its basic usage.

np.mean(arr, axis=(??))

But as the array has 4 dimensions, I'm a bit lost in setting the correct axis. All examples I found were dealing with just 1-D or 2-D arrays. So how should the function call look like having an array e.g. (1000, 512, 512, 3)?

1 Answers

For a generic ndarray, you could create a tuple to cover all axes except the last one corresponding to the color channel and then use that for the axis param with np.mean, like so -

np.mean(a, axis=tuple(range(a.ndim-1)))

Sample run to verify against a loop-comprehension version -

In [141]: np.random.seed(0)

In [142]: a = np.random.rand(4,5,6,7,3)

In [143]: [a[...,i].mean() for i in range(a.shape[-1])]
Out[143]: [0.50479333735828591, 0.49485716677174307, 0.51110772176772712]

In [144]: np.mean(a, axis=tuple(range(a.ndim-1)))
Out[144]: array([ 0.50479334,  0.49485717,  0.51110772])

Alternatively, we could reshape to 2D and find mean along the first axis -

In [145]: a.reshape(-1,a.shape[-1]).mean(0)
Out[145]: array([ 0.50479334,  0.49485717,  0.51110772])
Related