本文算法摘自opencv,可以说opencv是一个大宝库,里面有无穷无尽的算法,但是opencv里面的算法属于研究性质,只能解决“有”的问题,还不能解决“好”的问题。比如下面的简单白平衡算法,核心思想是:在rgb三通道上分别计算直方图,然后将1%的最大值和最小值设置为255和0,其余值映射到(0, 255)区间内,这样使得每个通道的值均匀分布,以实现简单的颜色平衡。实际测试效果,对于某些图像效果还是可以的,尤其是偏色比较厉害的图像。不过该算法实现逻辑比较晦涩。
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>using namespace cv;
using namespace std;enum
{WHITE_BALANCE_SIMPLE = 0,WHITE_BALANCE_GRAYWORLD = 1
};/*白平衡******************************************************************************************************************/
void balanceWhite(std::vector<Mat> &src, Mat &dst, const float inputMin, const float inputMax, const float outputMin, const float outputMax, const int algorithmType)
{// 在rgb三通道上分别计算直方图// 将1%的最大值和最小值设置为255和0// 其余值映射到(0, 255), 这样使得每个值通道的值在rgb中分布较均匀, 以实现简单的颜色平衡switch (algorithmType){case WHITE_BALANCE_SIMPLE:{/********************* Simple white balance *********************/float s1 = 1.0f;// low quantilefloat s2 = 1.0f;// high quantileint depth = 2;// depth of histogram treeint bins = 16;// number of bins at each histogram levelint total = src[0].cols * src[0].rows;int nElements = int(pow((float)bins, (float)depth));// number of elements in histogram treefor (size_t k = 0; k < src.size(); ++k){std::vector<int> hist(nElements, 0);uchar *pImag = src[k].data;// histogram fillingfor (int i = 0; i < total; i++){int pos = 0;float minValue = inputMin - 0.5f;float maxValue = inputMax + 0.5f;float interval = float(maxValue - minValue) / bins;uchar val = pImag[i];for (int j = 0; j < depth; ++j){int currentBin = int((val - minValue + 1e-4f) / interval);++hist[pos + currentBin];pos = (pos + currentBin)*bins;minValue = minValue + currentBin*interval;interval /= bins;}}int p1 = 0, p2 = bins - 1;int n1 = 0, n2 = total;float minValue = inputMin - 0.5f;float maxValue = inputMax + 0.5f;float interval = float(maxValue - minValue) / bins;// searching for s1 and s2for (int j = 0; j < depth; ++j){while (n1 + hist[p1] < s1 * total / 100.0f){n1 += hist[p1++];minValue += interval;}p1 *= bins;while (n2 - hist[p2] > (100.0f - s2) * total / 100.0f){n2 -= hist[p2--];maxValue -= interval;}p2 = p2*bins - 1;interval /= bins;}src[k] = (outputMax - outputMin) * (src[k] - minValue) / (maxValue - minValue) + outputMin;}/****************************************************************/break;}default:CV_Error_(CV_StsNotImplemented, ("Unsupported algorithm type (=%d)", algorithmType));}// switchmerge(src, dst);
}void balanceWhite(const Mat &src, Mat &dst, const int algorithmType, const float inputMin = 0.0f, const float inputMax = 255.0f, const float outputMin = 0.0f, const float outputMax = 255.0f)
{switch (src.depth()){case CV_8U:{std::vector<Mat> mv;split(src, mv);balanceWhite(mv, dst, inputMin, inputMax, outputMin, outputMax, algorithmType);break;}default:CV_Error_(CV_StsNotImplemented, ("Unsupported source image format (=%d)", src.type()));break;}
}
/**************************************************************************************************************************/int main()
{const char* fileName = "dog.png" ;Mat src = imread(fileName);imshow("src", src);cv::Mat dst(src.size(), src. type());balanceWhite(src, dst, WHITE_BALANCE_SIMPLE);imshow("dst", dst);imwrite("result.jpg", dst);cv::waitKey();return 0;
}
       效果如下:
    
                                                
                                              
                                              
       简单的算法,有时也有神奇的一面,这也是做图像算法研究的一种乐趣。对于类似下面的图片,该算法还有去雾霾效果,不过由于该算法仅仅是统计并拉伸像素值,所以局限性很大,去雾霾效果如下:
                              
       参考资料:
       http://stanford.edu/~sujason/ColorBalancing/simplestcb.html
       http://stanford.edu/~sujason/ColorBalancing/robustawb.html
       http://blog.csdn.net/vsooda/article/details/38875037

一种简单的图像白平衡计算方法相关推荐

  1. 四种简单的图像显著性区域特征提取方法-----AC/HC/LC/FT。

    四种简单的图像显著性区域特征提取方法-----> AC/HC/LC/FT. 分类: 图像处理 2014-08-03 12:40 4088人阅读 评论(4) 收藏 举报 salient regio ...

  2. 四种简单的图像显著性区域特征提取方法----- AC/HC/LC/FT。

    四种简单的图像显著性区域特征提取方法-----> AC/HC/LC/FT. 分类: 图像处理 2014-08-03 12:40 4088人阅读 评论(4) 收藏 举报 salient regio ...

  3. 一种简单的图像LDR->HDR算法

    算法比较简单,尽量简单说. 文章:Fully-automatic inverse tone mapping algorithm based on dynamic mid-level tone mapp ...

  4. 鱼眼校正c语言算法,一种简单而精确的鱼眼图像校正算法研究

    舒旭 摘 要: 针对鱼眼图像的校正提出了一种有效区域提取算法,并在鱼眼图像的球面物投影平面展开时与目标半立方体的校正平面建立线性映射.实验结果表明,该算法能有效地提取鱼眼图像轮廓的有效区域,同时半立方 ...

  5. 四种比较简单的图像显著性区域特征提取方法原理及实现

    四种比较简单的图像显著性区域特征提取方法原理及实现-----> AC/HC/LC/FT. laviewpbt  2014.8.4 编辑 Email:laviewpbt@sina.com   QQ ...

  6. 一种简单快速有效的图像暗部增强/亮度均衡算法

    2020/10/27更新:之前为克服光晕效应尝试过引导滤波,惜哉其他地方犯了个小错误以致未达到目标,处理的结果虽然保边但却过于模糊.后期修正之后再次尝试便得到了预期的效果.现将引入了引导滤波去光晕的程 ...

  7. Matlab实现图像白平衡(灰度世界法、全反射算法)

    参考:https://www.cnblogs.com/molakejin/p/5766132.html 白平衡 白平衡的英文为White Balance,其基本概念是"不管在任何光源下,都能 ...

  8. 摄像头、视频采集和摄像设备图像质量判断的几种简单有效目测方法

    来源:http://blog.csdn.net/lezhiyong 视频图像性能的几种简单目测方法,也可在购买视频采集和摄像设备拿来做参考 软件编解码性能测试: 摄像头前快速挥手,看手部是否连贯.是否 ...

  9. 基于qml创建最简单的图像处理程序(1)-基于qml创建界面

    <基于qml创建最简单的图像处理程序>系列课程及配套代码 基于qml创建最简单的图像处理程序(1)-基于qml创建界面 http://www.cnblogs.com/jsxyhelu/p/ ...

  10. ICCV2021 Oral SimROD:简单高效的数据增强!华为提出了一种简单的鲁棒目标检测自适应方法...

    点击上方"3D视觉工坊",选择"星标" 干货第一时间送达 作者丨小马 来源丨我爱计算机视觉 ▊ 写在前面 本文提出了一种简单有效的鲁棒目标检测无监督自适应方法( ...

最新文章

  1. python websocket例程_python 实现websocket
  2. gearman python 实例
  3. HDU1812 - Count the Tetris
  4. Python 学习笔记(1)
  5. MyEclipse 10.5 安装SVN插件
  6. 一直认为in,exists 不走索引,被同事鄙视了……
  7. 《1024伐木累》-程序员妹子与花木兰
  8. 软件机器人从幕后到台前 RPA+Chatbot带来“端到端的自动化”
  9. jquery 弹出遮罩层
  10. 环境安装_Python教程 Python环境安装
  11. mysql学习day02
  12. MySQL蜜罐在护网中提取攻击者微信ID
  13. CAS配置数据库进行用户验证
  14. 程序设计导引及在线实践——练习记录
  15. python 微信公众号发文章_Python 微信公众号文章爬取
  16. 【数据分析|面试】如何介绍你的项目经历
  17. 计算机函数求奖学金,Excel函数在高校奖学金评定中的实践应用参考.pdf
  18. java 实现 指派_Activiti 开发案例之动态指派任务
  19. t00lsudf.php,udf提权
  20. [转] 公共DNS,114.114.114.114和8.8.8.8

热门文章

  1. 《金狐系统维护盘》五周年纪念版【简洁易用,强大实用】
  2. 【翻译】图解Janusgraph系列-事务详解(Janusgraph Transactions)
  3. 关于http响应200 OK的问题
  4. 开源的视频编解码器介绍
  5. 计算机图形学:中点划线法(任意斜率)
  6. OV426+OVM6946基于FPGA调试成像
  7. android 距离感应器控制屏幕熄灭_华为nova3e、小米6X、OPPOA7x对比,选IPS屏幕还是TFT屏幕,自选...
  8. ARM架构(RISC)和x86架构(CISC)以及传统与移动CPU/GPU厂商
  9. 2017年下半年网络工程师真题+答案解析
  10. 不属于微型计算机的技术特标,计算机组成原理汇总