【opencv初学者】5、读取图片求图片亮度

#include 
#include 
#include 
#include 
#include 

using namespace std;

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;
}

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("C:/Users/xxx/Desktop/xxx20220718-100821.jpg");
	//
	float picLight1 = lightMean(imgTest);
	cout << "亮度值1为:" << picLight1 << endl;

	float picLight2 = lightGray(imgTest);
	cout << "亮度值2为:" << picLight2 << endl;
    
    // 等待任意按钮结束
    cv::waitKey(0);
	return 0;
}

你可能感兴趣的:(opencv,计算机视觉,人工智能)