在用OpenCV对图像进行处理时,利用颜色定位是常常会接触到的方法,但RGB受光照影响比较严重,转换到HSV XYZ等空间也解决不了时,

可以用白平衡算法进行修正,使其发黄、发蓝、发红的照片更加趋于自然光下的图像。(转摘请说明来源)

程序代码示例如下:

 //该代码实现白平衡算法中的灰度世界法,能有效改善图像发红发蓝发绿的现象;

 #include <opencv2/opencv.hpp>
using namespace cv; int main()
{
Mat g_srcImage,dstImage;
vector<Mat> g_vChannels;
g_srcImage = imread("C:/Users/Administrator/Desktop/区分高架定位/01.jpg");
imshow("原图",g_srcImage);
//waitKey(0); //分离通道
split(g_srcImage,g_vChannels);
Mat imageBlueChannel = g_vChannels.at();
Mat imageGreenChannel = g_vChannels.at();
Mat imageRedChannel = g_vChannels.at(); double imageBlueChannelAvg=;
double imageGreenChannelAvg=;
double imageRedChannelAvg=; //求各通道的平均值
imageBlueChannelAvg = mean(imageBlueChannel)[];
imageGreenChannelAvg = mean(imageGreenChannel)[];
imageRedChannelAvg = mean(imageRedChannel)[]; //求出个通道所占增益
double K = (imageRedChannelAvg+imageGreenChannelAvg+imageRedChannelAvg)/;
double Kb = K/imageBlueChannelAvg;
double Kg = K/imageGreenChannelAvg;
double Kr = K/imageRedChannelAvg; //更新白平衡后的各通道BGR值
addWeighted(imageBlueChannel,Kb,,,,imageBlueChannel);
addWeighted(imageGreenChannel,Kg,,,,imageGreenChannel);
addWeighted(imageRedChannel,Kr,,,,imageRedChannel); merge(g_vChannels,dstImage);//图像各通道合并
imshow("白平衡后图",dstImage);
waitKey();
return ;
}

结果如下:

OpenCV白平衡算法之灰度世界法(消除RGB受光照影响)-LMLPHP   OpenCV白平衡算法之灰度世界法(消除RGB受光照影响)-LMLPHP

05-11 11:07