当前位置:   article > 正文

ISP自动白平衡:完美反射算法_完美反射法

完美反射法


前言

之前学习了ISP自动白平衡 - 灰度世界算法,这里继续跟大家分享下第二个经典算法 - 完美反射算法。


1. 完美反射算法介绍

完美反射算法是选取图像中R/G/B三通道中像素值最大的点作为白点,以此来更新图像,实现图像白平衡。
算法步骤:

  1. 创建一个一维数组用来保存R/G/B三通道像素点的和,数组大小为766(每通道像素值范围在0-255, 三个通道像素值的和在0-765,所以定义数组大小为766);
  2. 遍历图像,填充步骤1定义的数组,并统计图像中最大像素值MaxVal;
  3. 查找阈值:按照从大到小的索引顺序遍历步骤1中的数组,并指定一个像素点数比率(比如0.1),当步骤1中数组累积像素点数大于图像像素点数和定义的比率之积时,此时的索引值即为阈值;
  4. 遍历图像,当每一像素点R/G/B三通道值的和大于步骤三定义的阈值时,分别统计R/G/B三通道像素值的和以及满足阈值的像素点数;
  5. 根据步骤4计算得到的R/G/B三通道像素值的和与统计的像素点数,分别计算R/G/B三通道像素均值,记为 R m e a n , G m e a n , B m e a n R_{mean}, G_{mean}, B_{mean} Rmean,Gmean,Bmean
  6. 遍历图像, 根据步骤5计算的R/G/B像素均值和步骤2获得的最大像素值,对图像R/G/B三通道像素值进行更新,更新公式:
    R = R ∗ M a x V a l R m e a n , B = B ∗ M a x V a l B m e a n , G = G ∗ M a x V a l G m e a n R = \frac{R*MaxVal}{R_{mean}}, B = \frac{B*MaxVal}{B_{mean}}, G = \frac{G*MaxVal}{G_{mean}} R=RmeanRMaxVal,B=BmeanBMaxVal,G=GmeanGMaxVal

2. 完美反射算法C++ Opencv实现

#include <iostream>
#include <opencv2\imgcodecs.hpp>
#include <opencv2\imgproc.hpp>
#include <opencv2\core.hpp>
#include <opencv2\highgui.hpp>
#include <vector>

using namespace cv;

// Auto White Balance - Gray World Algorithm
int AWB_GrayWorld(InputArray src, OutputArray dst)
{
	CV_Assert(src.channels() == 3, "AWB_GrayWorld() input image must be 3 channels!");

	Mat mSrc = src.getMat();
	if (mSrc.empty())
	{
		std::cout << "AWB_GrayWorld() input image is empty!" << std::endl;
		return -1;
	}
	
	dst.create(mSrc.size(), mSrc.type());
	Mat mDst = dst.getMat();

	if (mDst.empty())
	{
		std::cout << "AWB_GrayWorld() create dst image failed!" << std::endl;
		return -1;
	}

	//对输入src图像进行RGB分离
	std::vector<Mat> splitedBGR;
	splitedBGR.reserve(3);

	split(mSrc, splitedBGR);

	//分别计算R/G/B图像像素值均值
	double meanR = 0, meanG = 0, meanB = 0;
	meanB = mean(splitedBGR[0])[0];
	meanG = mean(splitedBGR[1])[0];
	meanR = mean(splitedBGR[2])[0];

	//计算R/G/B图像的增益
	double gainR = 0, gainG = 0, gainB = 0;
	gainR = (meanR + meanG + meanB) / (3 * meanR);
	gainG = (meanR + meanG + meanB) / (3 * meanG);
	gainB = (meanR + meanG + meanB) / (3 * meanB);

	//计算增益后R/G/B图像
	splitedBGR[0] = splitedBGR[0] * gainB;
	splitedBGR[1] = splitedBGR[1] * gainG;
	splitedBGR[2] = splitedBGR[2] * gainR;

	//将三个单通道图像合成一个三通道图像
	merge(splitedBGR, mDst);

	return 0;
}

int AWB_PerfectReflect(InputArray src, OutputArray dst)
{
	CV_Assert_2(src.channels() == 3, "AWB_PerfectReflect() src image must has 3 channels!");

	Mat mSrc = src.getMat();
	if (mSrc.empty())
	{
		std::cout << "AWB_PerfectReflect() src image can't be empty!" << std::endl;
		return -1;
	}

	dst.create(mSrc.size(), mSrc.type());
	Mat mDst = dst.getMat();

	int sumHist[766] = { 0 };//max(R+G+B) = 255*3 = 765, 0~765->766
	int maxVal = 0;

	for (int i = 0; i < mSrc.rows; i++)
	{
		for (int j = 0; j < mSrc.cols; j++)
		{
			Vec3b p = mSrc.at<Vec3b>(i, j);
			int sum = p[0] + p[1] + p[2];
			sumHist[sum]++;
			maxVal = maxVal > p[0] ? maxVal : p[0];
			maxVal = maxVal > p[1] ? maxVal : p[1];
			maxVal = maxVal > p[2] ? maxVal : p[2];
		}
	}

	int totalPixels = 0;
	for (int i = 765; i >= 0; i--)
	{
		totalPixels += sumHist[i];
	}

	CV_Assert_2(totalPixels == mSrc.rows*mSrc.cols, "sumHist pixels number isn't equal with image size!");

	float ratio = 0.1;
	int cumPixel = 0;
	int threshold = 0;
	for (int i = 765; i >= 0; i--)
	{
		cumPixel += sumHist[i];
		if (cumPixel >= ratio * mSrc.rows* mSrc.cols)
		{
			threshold = i;
			break;
		}
	}

	int avgB = 0, avgG = 0, avgR = 0;
	int countPixels = 0;
	for (int i = 0; i < mSrc.rows; i++)
	{
		for (int j = 0; j < mSrc.cols; j++)
		{
			Vec3b p = mSrc.at<Vec3b>(i, j);
			int sum = p[0] + p[1] + p[2];
			if (sum > threshold)
			{
				countPixels++;
				avgB += p[0];
				avgG += p[1];
				avgR += p[2];
			}
		}
	}

	avgB /= countPixels;
	avgG /= countPixels;
	avgR /= countPixels;

	for (int i = 0; i < mSrc.rows; i++)
	{
		for (int j = 0; j < mSrc.cols; j++)
		{
			Vec3b p = mSrc.at<Vec3b>(i, j);
			int B = p[0] * maxVal / avgB;
			B = B > 255 ? 255 : B;
			mDst.at<Vec3b>(i, j)[0] = (uchar)B;

			int G = p[1] * maxVal / avgG;
			G = G > 255 ? 255 : G;
			mDst.at<Vec3b>(i, j)[1] = (uchar)G;

			int R = p[2] * maxVal / avgR;
			R = R > 255 ? 255 : R;
			mDst.at<Vec3b>(i, j)[2] = (uchar)R;
		}
	}

	return 0;
}

int main()
{
	std::string imgPath = "C:\\Temp\\common\\Workspace\\Opencv\\images\\awb_grayworld.jpg";
	Mat src = imread(imgPath);
	Mat dstGW;
	int status = AWB_GrayWorld(src, dstGW);
	if (status != 0)
		goto EXIT;

	imshow("src", src);
	imshow("AWB GrayWorld", dstGW);
	waitKey(0);

	{
		Mat dstPR;
		status = AWB_PerfectReflect(src, dstPR);
		if (status != 0)
			goto EXIT;

		imshow("AWB PerfectReflect", dstPR);
		waitKey(0);
	}

EXIT:
	system("pause");
	destroyAllWindows();

	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183

3. 执行结果

原图:
在这里插入图片描述
灰度世界算法结果:
在这里插入图片描述
完美反射算法结果:
在这里插入图片描述

总结

从结果来看,完美反射算法结果要好一些,但是如果图像最亮点不是白点的话,效果不佳。

参考

https://www.cnblogs.com/Imageshop/archive/2013/04/20/3032062.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/287476
推荐阅读
相关标签
  

闽ICP备14008679号