消除RGB受光照影响

//该代码实现白平衡算法中的灰度世界法,能有效改善图像发红发蓝发绿的现象;
  
 #include 
 using namespace cv;
 
 int main()
 {
      Mat g_srcImage,dstImage;
     vector 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(0);
     Mat imageGreenChannel = g_vChannels.at(1);     
    Mat imageRedChannel = g_vChannels.at(2);
 
     double imageBlueChannelAvg=0;
     double imageGreenChannelAvg=0;
     double imageRedChannelAvg=0;
 
     //求各通道的平均值
     imageBlueChannelAvg = mean(imageBlueChannel)[0];
     imageGreenChannelAvg = mean(imageGreenChannel)[0];
     imageRedChannelAvg = mean(imageRedChannel)[0];
 
     //求出个通道所占增益
     double K = (imageRedChannelAvg+imageGreenChannelAvg+imageRedChannelAvg)/3;
     double Kb = K/imageBlueChannelAvg;
     double Kg = K/imageGreenChannelAvg;
     double Kr = K/imageRedChannelAvg;
 
     //更新白平衡后的各通道BGR值
     addWeighted(imageBlueChannel,Kb,0,0,0,imageBlueChannel);
     addWeighted(imageGreenChannel,Kg,0,0,0,imageGreenChannel);
     addWeighted(imageRedChannel,Kr,0,0,0,imageRedChannel);
 
     merge(g_vChannels,dstImage);//图像各通道合并
     imshow("白平衡后图",dstImage);
     waitKey(0);
    return 0;
 }


你可能感兴趣的:(图像处理,Opencv)