Reputation: 2089
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)
?
Upvotes: 10
Views: 16179
Reputation: 111
Explanation for beginners (like me): I had the same problem and it took me a while to solve it. Basically for working with images you do:
mean= np.mean(images, axis=(0,1,2))
With this, what you are really saying is "I want to take for every image the height and with of every channel and compute its mean". So as a result you get 'mean' with shape (3,) meaning you have the red, blue and green means across all your dataset.
Upvotes: 6
Reputation: 221744
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])
Upvotes: 22