Load an image with OpenCV and get the average of its saturation and brightness. Since it is treated as a numpy matrix, knowledge of numpy is also required when dealing with OpenCV in Python. In the following example, the transposed matrix (T) is used to calculate the average of each layer.
import sys,cv2
img = cv2.imread(sys.argv[1],1) # 0=grayscale, 1=color
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
print("Shape: {0}".format(hsv.shape))
print("Salute(mean): %.2f" % (hsv.T[1].flatten().mean()))
print("Value(mean): %.2f" % (hsv.T[2].flatten().mean()))
The following repository has a bit of sample code for OpenCV Python bindings.
https://github.com/lumbermill/takachiho/tree/master/sandbox/opencv
Recommended Posts