NCC(NormalizedCrossCorrelation)归⼀化互相关原理和C++代码实现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的协⽅差;
大学生学年鉴定表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;
int overlapMaxCorrelationCoefficient = 0;
for (int overlap = 350; overlap < 650; overlap += 50)
{
//****************************************//
Mat imageTemp = image2(Rect(0, 0, overlap, ws));
long double tempTotalcount = 0;
long double tempTotalPixel = 0;
for (int i = 0; i < overlap; i++)
{
for (int j = 0; j < ws; 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 < ws; 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 imageBa = image1(ls-overlap, 0, overlap, ws));
int baTotalcount = 0;
int baTotalPixel = 0;
for (int i = 0; i < overlap; i++)
{
for (int j = 0; j < ws; j++)
{
寻觅的意思baTotalcount += 1;
//cout << i<<","<<j<<":"<<int(imageTemp.at<uchar>(j,i)) << ",";
baTotalPixel += float(imageBa.at<uchar>(j, i));
}
cout << endl;
}
float baAvg = baTotalPixel / baTotalcount;
//*****************************************//
long double baSubstract = 0;
for (int i = 0; i < overlap; i++)
{儿童吃什么可以长高个子
for (int j = 0; j < ws; j++)
{
long double baSquare = (long double(imageBa.at<uchar>(j, i)) - baAvg)* (long double(imageBa.at<uchar>(j, i)) - baAvg); baSubstract = baSubstract + baSquare;
}
cout << endl;
}
float baVariance = sqrt(baSubstract / baTotalcount);
//***************************************//
long double dotMul = 0;
for (int i = 0; i < overlap; i++)
{
for (int j = 0; j < ws; j++)
{
dotMul += abs((long double(imageBa.at<uchar>(j, i)) - baAvg)*(long double(imageTemp.at<uchar>(j, i)) - tempAvg));校长工作总结
}
社保断交一个月有什么影响
}
cout << endl;
}
float dotMulAvg = dotMul / baTotalcount;
float pearsonCorrelationCoefficient=dotMulAvg / (baVariance*tempVariance); if (pearsonCorrelationCoefficientMax < pearsonCorrelationCoefficient)
{
pearsonCorrelationCoefficientMax = pearsonCorrelationCoefficient;
overlapMaxCorrelationCoefficient = overlap;
}
}
cout << "最⼤相关系数" << pearsonCorrelationCoefficientMax << endl;
cout << "最⼤相关系数时重叠区域" << overlapMaxCorrelationCoefficient << endl;