c++ OpenCV 图像亮度的计算方法 代码

参考https://blog.csdn.net/weixin_39609623/article/details/95306158
稍加修改
第一次学习使用openCV,有错误请不吝指教

1. 方法一

计算图片各个通道的亮度后求的均值

float lightMean(cv::Mat img)
{
	cv::Scalar scalar = cv::mean(img);
	float imgChannel1 = scalar.val[0];
	float imgChannel2 = scalar.val[1];
	float imgChannel3 = scalar.val[2];
	//float imgChannel4 = scalar.val[3];
	//qDebug() << "--4.method mean picture Light : " << QString::number(picMeanLight, 10, 4);
	float imgLight = (imgChannel1 + imgChannel2 + imgChannel3) / 3;
	return imgLight;
}

2. 方法二
c++ OpenCV 图像亮度的计算方法 代码_第1张图片

//根据hsl中l=(max(R,G,B)+min(R,G,B))/2
//用cv::mean()分别计算R,G,B三通道平均值,最大、最小值求Light分量作为亮度
float lightHSL(cv::Mat img)
{
	cv::Scalar scalarHSL = cv::mean(img);
	double imgMax = cv::max(scalarHSL.val[0], scalarHSL.val[1]);
	imgMax = cv::max(imgMax, scalarHSL.val[2]);
	double imgMin = cv::min(scalarHSL.val[0], scalarHSL.val[1]);
	imgMin = cv::min(imgMin, scalarHSL.val[2]);
	float hslLight = (imgMax + imgMin) / 2;
	return hslLight;
}

3. 方法三
转为灰度图片,计算像素平均值作为亮度

float lightGray(cv::Mat img)
{
	cv::Mat imgGray;
	cv::cvtColor(img, imgGray, CV_BGR2GRAY);
	cv::Scalar grayScalar = cv::mean(imgGray);
	float imgGrayLight = grayScalar.val[0];
	return imgGrayLight;
}

测试

int main()
{
	cv::Mat imgTest;
	imgTest = cv::imread("D:\\A_testdata\\binary_tif\\testjpg.jpg");
	//算亮度
	float picLight = lightMean(imgTest);
	float picLight = lightHSL(imgTest);
	float picLight = lightGray(imgTest);
	
    waitKey(0);
    return 0;
}

你可能感兴趣的:(opencv,计算机视觉,c++)