在处理图像自动亮度调节时,需要判断图像是否过亮或者过暗,从而根据图像给出的结果,进行调节。方法如下:文章来源地址https://www.toymoban.com/news/detail-613745.html
int isLight(cv::Mat& src)
{
cv::Mat gray;
//转为灰度图片
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
float sum = 0;
float avg = 0;
cv::Scalar scalar;
int ls[256];
int size = gray.rows * gray.cols;
for (int i = 0; i < 256; i++)
ls[i] = 0;
for (int i = 0; i < gray.rows; i++)
{
for (int j = 0; j < gray.cols; j++)
{
//scalar = cvGet2D(gray, i, j);
scalar = gray.at<uchar>(i, j);
sum += (scalar.val[0] - 128);
int x = (int)scalar.val[0];
ls[x]++;
}
}
avg = sum / size;
float total = 0;
float mean = 0;
for (int i = 0; i < 256; i++)
{
total += abs(float(i - 128) - avg) * ls[i];
}
mean = total / size;
float cast = abs(avg / mean);
std::cout << "亮度异常值:" << cast << std::endl;
if (cast > 1)
{
if (avg > 0)
{
std::cout << "亮度异常 过亮" << avg << std::endl;
return 1;
}
else {
std::cout << "亮度异常 过暗" << avg << std::endl;
return -1;
}
}
else
{
std::cout << "normal" << std::endl;
return 0;
}
}
文章来源:https://www.toymoban.com/news/detail-613745.html
到了这里,关于利用OpenCV判断图像是否过亮或过暗的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!