colorphoto / basicsr /metrics /colorfulness.py
My-AI-Projects's picture
Add application file
aaa2047
raw
history blame contribute delete
535 Bytes
import cv2
import numpy as np
from basicsr.utils.registry import METRIC_REGISTRY
@METRIC_REGISTRY.register()
def calculate_cf(img, **kwargs):
"""Calculate Colorfulness.
"""
(B, G, R) = cv2.split(img.astype('float'))
rg = np.absolute(R - G)
yb = np.absolute(0.5 * (R+G) - B)
(rbMean, rbStd) = (np.mean(rg), np.std(rg))
(ybMean, ybStd) = (np.mean(yb), np.std(yb))
stdRoot = np.sqrt((rbStd ** 2) + (ybStd ** 2))
meanRoot = np.sqrt((rbMean ** 2) + (ybMean ** 2))
return stdRoot + (0.3 * meanRoot)