NCC(Normalized Cross Correlation)归一化互相关原理和C++代码实现

2 篇文章 0 订阅

NCC(Normalized Cross Correlation)归一化互相关

图像匹配指在已知目标基准图的子图集合中,寻找与实时图像最相似的子图,以达到目标识别与定位目的的图像技术。主要方法有:基于图像灰度相关方法、基于图像特征方法、基于神经网络相关的人工智能方法(还在完善中)。基于图像灰度的匹配算法简单,匹配准确度高,主要用空间域的一维或二维滑动模版进行图像匹配,不同的算法区别主要体现在模版及相关准则的选择方面,但计算量大,不利于实时处理,对灰度变化、旋转、形变以及遮挡等比较敏感;基于图像特征的方法计算量相对较小,对灰度变化、形变及遮挡有较好的适应性,通过在原始图中提取点、线、区域等显著特征作为匹配基元,进而用于特征匹配,但是匹配精度不高。
通常又把基于灰度的匹配算法,称作相关匹配算法。相关匹配算法又分为两类:一类强调景物之间的差别程度如平法差法(SD)和平均绝对差值法(MAD)等;另一类强调景物之间的相似程度,主要算法又分成两类,一是积相关匹配法,二是相关系数法。今天我们就来说说归一化互相关系数法(NCC).

  1. NCC原理:
    假设两幅进行匹配计算的图像中的小图像为g,大小为m×n,大图像为S,大小为M×N.用Sx,y表示S中以(x,y)为左上角点与g大小相同的子块。利用相关系数公式计算实时图和基准图之间的相关系数,得到相关系数矩阵ρ(x,y),通过对相关系数矩阵的分析,判断两幅图像是否相关。
    ρ(x,y)的定义为
    随机变量X和Y的(Pearson)相关系数)在这里插入图片描述
    式中:
    是Sx,y和g的协方差;是Sx,y和g的协方差;

Dx,y为Sx,y的方差Dx,y为Sx,y的方差

D为g的方差,在这里插入图片描述
g的灰度均值
图像Sx,y的灰度均值在这里插入图片描述
将Dx,y和D代入式得到:
在这里插入图片描述
相关系数满足:
在这里插入图片描述
在[-1,1]绝对尺度范围之间衡量两者的相似性。相关系数刻画了两者之间的近似程度的线性描述。一般说来,越接近于1,两者越近似的有线性关系。
2. C++代码实现

    Mat image1 = imread("E:xx.tif", IMREAD_GRAYSCALE);
	Mat image2 = imread("E:yy.tif", IMREAD_GRAYSCALE); 
	int overlap = 350;
	float pearsonCorrelationCoefficientMax = 0;
	int overlapMaxCorrelationCoefficient = 0;
	for (int overlap = 350; overlap < 650; overlap += 50)
	{
	//****************************************//
		Mat imageTemp = image2(Rect(0, 0, overlap, image1.rows));
		long double tempTotalcount = 0;
		long double tempTotalPixel = 0;
		for (int i = 0; i < overlap; i++)
		{
			for (int j = 0; j < image1.rows; j++)
			{
				tempTotalcount += 1;
				//cout << i<<","<<j<<":"<<int(imageTemp.at<uchar>(j,i)) << ",";
				tempTotalPixel += float(imageTemp.at<uchar>(j, i));
			}
			cout << endl;
		}
		float tempAvg = tempTotalPixel / tempTotalcount;
		//**************************************//	
		long double tempSubstract = 0;
		for (int i = 0; i < overlap; i++)
		{
			for (int j = 0; j < image1.rows; j++)
			{
				long double tempSquare = (long double(imageTemp.at<uchar>(j, i)) - tempAvg)* (long double(imageTemp.at<uchar>(j, i)) - tempAvg);
				tempSubstract = tempSubstract + tempSquare;
			}
			cout << endl;
		}
		float tempVariance = sqrt(tempSubstract / tempTotalcount);
	//***********************************************//
		Mat imageBase = image1(Rect(image1.cols-overlap, 0, overlap, image1.rows));
		int baseTotalcount = 0;
		int baseTotalPixel = 0;
		for (int i = 0; i < overlap; i++)
		{
			for (int j = 0; j < image1.rows; j++)
			{
				baseTotalcount += 1;
				//cout << i<<","<<j<<":"<<int(imageTemp.at<uchar>(j,i)) << ",";
				baseTotalPixel += float(imageBase.at<uchar>(j, i));
			}
			cout << endl;
		}
		float baseAvg = baseTotalPixel / baseTotalcount;

		//*****************************************//	
		long double baseSubstract = 0;
		for (int i = 0; i < overlap; i++)
		{
			for (int j = 0; j < image1.rows; j++)
			{
				long double baseSquare = (long double(imageBase.at<uchar>(j, i)) - baseAvg)* (long double(imageBase.at<uchar>(j, i)) - baseAvg);
				baseSubstract = baseSubstract + baseSquare;
			}
			cout << endl;
		}
		float baseVariance = sqrt(baseSubstract / baseTotalcount);
		//***************************************//
		long double dotMul = 0;
		for (int i = 0; i < overlap; i++)
		{
			for (int j = 0; j < image1.rows; j++)
			{
				dotMul += abs((long double(imageBase.at<uchar>(j, i)) - baseAvg)*(long double(imageTemp.at<uchar>(j, i)) - tempAvg));
			}
			cout << endl;
		}
		float dotMulAvg = dotMul / baseTotalcount;

		float pearsonCorrelationCoefficient=dotMulAvg / (baseVariance*tempVariance);
		if (pearsonCorrelationCoefficientMax < pearsonCorrelationCoefficient)
		{
			pearsonCorrelationCoefficientMax = pearsonCorrelationCoefficient;
			overlapMaxCorrelationCoefficient = overlap;
		}

	}
	cout << "最大相关系数" << pearsonCorrelationCoefficientMax << endl;
	cout << "最大相关系数时重叠区域" << overlapMaxCorrelationCoefficient << endl;
  • 23
    点赞
  • 202
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值