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