Python OpenCV: Calculate the Mean of Red, Green and Blue Channel

In this tutorial, we will use an example to show how to calculate the color mean of red, green and blue channel in python opencv.

Python OpenCV - Calculate the Mean of Red, Green and Blue Channel for Beginners

1.Import library

import cv2
import numpy as np
from matplotlib import pyplot as plt

2.Open an image with BGR in python opencv

image_bgr = cv2.imread('images/plane_256x256.jpg', cv2.IMREAD_COLOR)

3.Calculate Mean Color Of Each Color Channel

channels = cv2.mean(image_bgr)

# Swap blue and red values (making it RGB, not BGR)
observation = np.array([(channels[2], channels[1], channels[0])])

4.Show mean values

print(observation)

Run this code, you may see this mean value:

array([[  90.53204346,  133.11735535,  169.03074646]])

5.Display mean value

plt.imshow(observation), plt.axis("off")
plt.show()

Run this code, you may see image:

Python OpenCV - Calculate the Mean of Red, Green and Blue Channel