The average value to measure in a numpy array

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

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

+4
source share
1 answer

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

If you are using a numpy version greater than 1.7, you can pass multiple axes into np.meana tuple

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

Otherwise, you will have to smooth the array first to get it in the correct form.

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

Source: https://habr.com/ru/post/1606929/


All Articles