假设我有一个opencv
BGR
图像img
,如何计算该图像的对比度?
lkarhlkqjwr 回答:如何计算图像的对比度?
这里是对比度的一种度量,(Ymax-Ymin)/(Ymax + Ymin),以及如何在Python / OpenCV / Numpy中进行计算。对比度将在0到1之间。低对比度接近零,高对比度接近一。使用YUV或YCbCr的Y(强度)通道,或者使用LAB的L通道,甚至只是将图像转换为灰度并使用。
输入:
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
所以对比度是1.0
,对比度的一种定义是RMS Contrast,可以按如下计算:
首先,将BGR
图像img
转换为灰度:
img_grey = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
最后,计算灰色图像像素强度的标准偏差:
contrast = img_grey.std()