John
John

Reputation: 3090

How to compute mean of an 4D array individually?

I have an array img with shape is 64x128x512x3 that is concated from three images 64x128x512. I want to compute mean of each image individually, given by the array img. Hence, I performed the code as bellows:

import numpy as np
img_means = np.mean(img, (0, 1, 2)))

Is it correct? My expected result is that img_means[0,:,:,:] is mean of the first image, img_means[1,:,:,:] is mean of second image, img_means[2,:,:,:] of third image.

Upvotes: 0

Views: 1953

Answers (2)

guofei9987
guofei9987

Reputation: 21

Your code is not working in python 3.x Do it like this: First generate the data

import numpy as np
img=np.arange(64*128*512*3).reshape(64,128,512,3)

And this is what you want:

img_means=[img[:,:,:,i].mean() for i in range(img.shape[3]) ]

Upvotes: 0

Miriam Farber
Miriam Farber

Reputation: 19664

Yes it is correct, but note that img_means is just an array of three numbers (each one is the mean of the corresponding figure).

Upvotes: 1

Related Questions