一种简单的图像白平衡计算方法

       本文算法摘自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 quantile
			float s2 = 1.0f;// high quantile

			int depth = 2;// depth of histogram tree
			int bins = 16;// number of bins at each histogram level
			int total = src[0].cols * src[0].rows;
			int nElements = int(pow((float)bins, (float)depth));// number of elements in histogram tree

			for (size_t k = 0; k < src.size(); ++k)
			{
				std::vector<int> hist(nElements, 0);
				uchar *pImag = src[k].data;
				// histogram filling
				for (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 s2
				for (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));
	}// switch

	merge(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;
}
       效果如下:
    
                                                
                                              
                                              
       简单的算法,有时也有神奇的一面,这也是做图像算法研究的一种乐趣。对于类似下面的图片,该算法还有去雾霾效果,不过由于该算法仅仅是统计并拉伸像素值,所以局限性很大,去雾霾效果如下:
                              

       参考资料:




  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值