颜色迁移——图片校正

Color Balance 变化检测之光照鲁棒性

传统变化检测——即图像差值法
在复杂的自然场景下,无法抑制光照对多时相图造成的影响。为了
能准确的获得有效的变换区域。我将大致的介绍一下颜色平衡算法,并进行算法实现

Color Balance Algorithm

颜色平衡算法的目的:对两幅多时相图片的亮度、对比度以及结构进行适当的平衡。

算法原理:在Lab通道下分别计算原图像与目标图像的均值、方差

公式:

很明显上述公式所利用的均值和方差是针对全图的,因此针对自然环境中光照的分散特性。其无法展示很好的效果

Global Color Balance Code

注意下面的src1,src2已经经过BGR转化到Lab通道了

Mat globalCB(const Mat& src1, const Mat& src2){
	Mat mean1, mean2, stdv1, stdv2;

	meanStdDev(src1, mean1, stdv1);
	meanStdDev(src2, mean2, stdv2);

	int mean_value1 = mean1.at(0, 0);
	int mean_value2 = mean2.at(0, 0);
	double mean_stdv1 = stdv1.at(0, 0);
	double mean_stdv2 = stdv2.at(0, 0);
	double ratio = mean_stdv1 / mean_stdv2;

	Mat result = Mat::zeros(src1.size(),src1.type());

	for (int i = 0; i < result.rows; i++){
		const uchar* inData = src2.ptr(i);
		uchar* outData = result.ptr(i);
		for (int j = 0; j <3* result.cols; j++){
			*outData++ = mean_value1 + (int)(ratio*((int)*inData++ - mean_value2));
			/*cout <<(int) *outData << endl;*/
		}
	}
	return result;
}

由于上述的全局颜色平衡算法还是无法解决光照影响,为了能够解决上述问题,局部颜色平衡算法出现
公式:
在这里插入图片描述

Local Color Balance Code

Mat localCB(const Mat& src1, const Mat& src2, int ksize,int color_diff){
	Mat img1, img2;
	copyMakeBorder(src1, img1, ksize, ksize, ksize, ksize, BORDER_REPLICATE);
	copyMakeBorder(src2, img2, ksize, ksize, ksize, ksize, BORDER_REPLICATE);
	vector channels1,channels2;
	split(img1, channels1);
	split(img2, channels2);
	Mat result = Mat::zeros(img1.size(), img1.type());
	for (int i = ksize; i < result.rows-ksize; i++){
		const uchar* inData = img2.ptr(i);
		uchar* outData = result.ptr(i);
		for (int j = ksize; j < (result.cols-ksize); j++){
			for (int k = 0; k < 3; k++){
				Mat roi1 = channels1[k](Rect(j - ksize, i - ksize, ksize, ksize));
				Mat roi2 = channels2[k](Rect(j - ksize, i - ksize, ksize, ksize));
				Mat mean1, mean2, stdv1, stdv2;
				meanStdDev(roi1, mean1, stdv1);
				meanStdDev(roi2, mean2, stdv2);
				int mean_value1 = mean1.at(0, 0);
				int mean_value2 = mean2.at(0, 0);
				double mean_stdv1 = stdv1.at(0, 0);
				double mean_stdv2 = stdv2.at(0, 0);
				double ratio = 0;
				if (mean_stdv2!=0)
					ratio = mean_stdv1 / mean_stdv2;
				int value =abs( mean_value1 + (int)(ratio*((int)inData[j * 3 + k] - (int)mean_value2)));
				outData[j * 3 + k] = value;
			}
		}
	}
	return result(Rect(ksize,ksize,src1.cols,src1.rows));
}

参考文献:color Balancing for Change Detection in Multitemporal Images

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