一种简单的图像白平衡计算方法
2017-04-09 22:56
489 查看
本文算法摘自opencv,可以说opencv是一个大宝库,里面有无穷无尽的算法,但是opencv里面的算法属于研究性质,只能解决“有”的问题,还不能解决“好”的问题。比如下面的简单白平衡算法,核心思想是:在rgb三通道上分别计算直方图,然后将1%的最大值和最小值设置为255和0,其余值映射到(0, 255)区间内,这样使得每个通道的值均匀分布,以实现简单的颜色平衡。实际测试效果,对于某些图像效果还是可以的,尤其是偏色比较厉害的图像。不过该算法实现逻辑比较晦涩。
效果如下:
[align=left] 简单的算法,有时也有神奇的一面,这也是做图像算法研究的一种乐趣。对于类似下面的图片,该算法还有去雾霾效果,不过由于该算法仅仅是统计并拉伸像素值,所以局限性很大,去雾霾效果如下:[/align]
[align=left]
[/align]
参考资料:
[align=left] http://stanford.edu/~sujason/ColorBalancing/simplestcb.html[/align]
[align=left] http://stanford.edu/~sujason/ColorBalancing/robustawb.html[/align]
[align=left] http://blog.csdn.net/vsooda/article/details/38875037[/align]
[align=left]
[/align]
[align=left]
[/align]
[align=left]
[/align]
[align=left]
[/align]
#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; }
效果如下:
[align=left] 简单的算法,有时也有神奇的一面,这也是做图像算法研究的一种乐趣。对于类似下面的图片,该算法还有去雾霾效果,不过由于该算法仅仅是统计并拉伸像素值,所以局限性很大,去雾霾效果如下:[/align]
[align=left]
[/align]
参考资料:
[align=left] http://stanford.edu/~sujason/ColorBalancing/simplestcb.html[/align]
[align=left] http://stanford.edu/~sujason/ColorBalancing/robustawb.html[/align]
[align=left] http://blog.csdn.net/vsooda/article/details/38875037[/align]
[align=left]
[/align]
[align=left]
[/align]
[align=left]
[/align]
[align=left]
[/align]
相关文章推荐
- 一种简单的图像显著性计算模型
- 一种简单的图像显著性计算模型
- 一种简单的位姿估计(PoseEstimation)计算方法
- 一种简单的图像显著性计算模型
- 一种简单的图像显著性计算模型
- 一种简单的图像显著性计算模型
- 一种计算图像曝光度(Exposure)的方法
- 在VC下显示JPEG、GIF格式图像的一种简便方法
- 一种简单的SQL语法检测方法
- 74HC244和74HC164与串行口实现多个LED显示的一种简单方法
- .NET20 一种简单的窗口控件UI状态控制方法
- 一种处理stack blown异常的简单方法
- 一种简单实用的全屏方法
- .NET中书写XML的一种简单方法
- 一种简单的结束无法关闭的DOS窗口的方法
- 一种简单的Windows密码重设方法
- 一种简单方法实现页面导出(Excel)功能 选择自 xieyun1977 的 Blog
- SQL计算秒数的一个简单方法
- B/S系统权限控制的一种简单方法
- 一种简单的struts级连菜单实现方法