赞
踩
参考 GiantPandaCV公众号
完美反射算法是自动白平衡常用的算法之一
完美反射理论假设图像中最亮的点就是白点,并以此白点为参考对图像进行自动白平衡,最亮点定义为R+G+B的最大值。
Mat PerfectReflectionAlgorithm(Mat src) { int row = src.rows; int col = src.cols; Mat dst(row, col, CV_8UC3); int HistRGB[767] = { 0 }; int MaxVal = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[0]); MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[1]); MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[2]); int sum = src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]; HistRGB[sum]++; } } int Threshold = 0; int sum = 0; for (int i = 766; i >= 0; i--) { sum += HistRGB[i]; if (sum > row * col * 0.1) { Threshold = i; break; } } int AvgB = 0; int AvgG = 0; int AvgR = 0; int cnt = 0; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int sumP = src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2]; if (sumP > Threshold) { AvgB += src.at<Vec3b>(i, j)[0]; AvgG += src.at<Vec3b>(i, j)[1]; AvgR += src.at<Vec3b>(i, j)[2]; cnt++; } } } AvgB /= cnt; AvgG /= cnt; AvgR /= cnt; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { int Blue = src.at<Vec3b>(i, j)[0] * MaxVal / AvgB; int Green = src.at<Vec3b>(i, j)[1] * MaxVal / AvgG; int Red = src.at<Vec3b>(i, j)[2] * MaxVal / AvgR; if (Red > 255) { Red = 255; } else if (Red < 0) { Red = 0; } if (Green > 255) { Green = 255; } else if (Green < 0) { Green = 0; } if (Blue > 255) { Blue = 255; } else if (Blue < 0) { Blue = 0; } dst.at<Vec3b>(i, j)[0] = Blue; dst.at<Vec3b>(i, j)[1] = Green; dst.at<Vec3b>(i, j)[2] = Red; } } return dst; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。