Larrelt
Larrelt

Reputation:

Mean value for dimension in numpy array

My numpy array (name: data) has following size: (10L,3L,256L,256L). It has 10 images with each 3 color channels (RGB) and each an image size of 256x256 pixel.

I want to compute the mean pixel value for each color channel of all 10 images. If I use the numpy function np.mean(data), I receive the mean for all pixel values. Using np.mean(data, axis=1) returns a numpy array with size (10L, 256L, 256L).

Upvotes: 6

Views: 4947

Answers (1)

Simon Gibbons
Simon Gibbons

Reputation: 7194

If I understand your question correctly you want an array containing the mean value of each channel for each of the three images. (i.e. an array of shape (10,3) ) (Let me know in the comments if this is incorrect and I can edit this answer)

If you are using a version of numpy greater than 1.7 you can pass multiple axes to np.mean as a tuple

mean_values = data.mean(axis=(2,3))

Otherwise you will have to flatten the array first to get it into the correct shape.

mean_values = data.reshape((data.shape[0], data.shape[1], data.shape[2]*data.shape[3])).mean(axis=2)

Upvotes: 9

Related Questions