当前位置:   article > 正文

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

normalized cross correlation

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;
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/504415
推荐阅读
相关标签
  

闽ICP备14008679号