I have 3-channels cv2 frame which as well known is ndarray. So the shape of array is (640, 480, 3). I want to calculate mean value for each color channel for entire frame. Is there convinient one-string way in numpy to do that?
Asked
Active
Viewed 231 times
-2
-
Does this answer your question? [How to find the average colour of an image in Python with OpenCV?](https://stackoverflow.com/questions/43111029/how-to-find-the-average-colour-of-an-image-in-python-with-opencv) Watch also this one [How to find the average of an array of tuples with numpy](https://stackoverflow.com/questions/50241168/how-to-find-the-average-of-an-array-of-tuples-with-numpy) – Wippo Dec 05 '20 at 13:19
-
You should show what you have tried so far. First thing to question yourself is, is the problem you're facing really a atypical one? Because, if not then there is always a short or a long solution and Google is the best place to start. – sai Dec 05 '20 at 14:39
1 Answers
0
You can use numpy's built-in mean function and specify the axes to average over:
import numpy as np
img_arr = np.random.randn(640, 480, 3)
print(img_arr.shape)
# (640, 480, 3)
means = np.mean(img_arr, axis=(0, 1))
print(means.shape)
# (3,)

Chris Mueller
- 6,490
- 5
- 29
- 35