假设我有一张
opencv
的 BGR
图像 img
,如何计算该图像的对比度?对比度的一个定义是RMS对比度,可以通过以下方式计算:
首先,将BGR
图像img
转换为灰度图像:
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
最后,计算灰度图像像素强度的标准差:
contrast = img_grey.std()
import cv2
import numpy as np
# load image as YUV (or YCbCR) and select Y (intensity)
# or convert to grayscale, which should be the same.
# Alternately, use L (luminance) from LAB.
img = cv2.imread("barn.jpg")
Y = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)[:,:,0]
# compute min and max of Y
min = np.min(Y)
max = np.max(Y)
# compute contrast
contrast = (max-min)/(max+min)
print(min,max,contrast)
0 255 1.0
对比度将在0和1之间。
sat
。我哪里打错了? - fmw42