相关系数法影像匹配

基于VS 2015 opencv 

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main()
{
	Mat airport, plane;
	airport = imread("airport.bmp",0);
	plane = imread("template.bmp", 0);

	Mat Correlation(airport.rows,airport.cols, CV_64FC1, Scalar(0));
	Mat NormCorrelation;
	imshow("原始图", airport);
	imshow("飞机", plane);

	int N = plane.cols*plane.rows;
	double Sgf = 0, Sgg = 0, Sff = 0,Sg = 0,Sf = 0;
	double Ss;

	//模板
	for (int a = 0; a < plane.rows; ++a)
	{
		for (int b = 0; b < plane.cols; ++b)
		{
			Sg+= plane.at<uchar>(a,b);
			Sgg += plane.at<uchar>(a, b)*plane.at<uchar>(a, b);
		}
	}
	double Cgg = Sgg - Sg*Sg / N;

	//再原始影像上进行窗口滑动
	for (int i = plane.rows / 2; i < airport.rows-plane.rows/2 - 1; i++)
	{
		for (int j = plane.rows / 2; j < airport.cols - plane.rows / 2 - 1; j++)
		{
			for (int c = 0; c < plane.rows; ++c)
			{
				for (int f = 0; f < plane.cols; ++f)
				{
					Sf += airport.at<uchar>(i + c - plane.rows / 2, j + f - plane.cols / 2);
					Sff += (airport.at<uchar>(i + c - plane.cols / 2, +f - plane.cols / 2)*airport.at<uchar>(i + c - plane.cols / 2, +f - plane.cols / 2));
					Sgf += (plane.at<uchar>(c, f)*airport.at<uchar>(i + c - plane.rows / 2, j + f - plane.rows, j + f - plane.cols / 2));

				}
			}
			double Cff = Sff - Sf*Sf / N;
			Ss = sqrt(Cff*Cgg);
			Correlation.at<double>(i,j) - (Sgf - Sg*Sg / N) / Ss;
			Sf = 0;
			Sgf = 0;
			Sff = 0;
		}
	}

	imshow("相关系数图像", Correlation);
	double minVal;
	double maxVal;
	Point minLoc;
	Point maxLoc;
	minMaxLoc(Correlation, &minVal, &maxVal, &minLoc, &maxLoc);
	cout << maxVal << endl << maxLoc.x << endl << maxLoc.y << endl;
	rectangle(airport,Point(maxLoc.x - 16, maxLoc.y - 16), Point(maxLoc.x + 16, maxLoc.y + 16), Scalar(0, 0, 255), 2, 8);
	cout << "over" << endl;
	waitKey(0);

}

 

  • 6
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
相关系数(Correlation Coefficient)是一种常用的影像匹配,在OpenCV中也能够实现。 首先,我们需要准备两个待匹配影像,分别称为源影像(source image)和目标影像(template image)。接下来,我们可以使用OpenCV中的相关系数函数`cv.matchTemplate()`进行匹配。 函数使用的语如下: ``` result = cv.matchTemplate(source, template, method) ``` 其中,`source`是源影像,`template`是目标影像,`method`是指定使用的匹配。 常见的匹配有以下几种: - `cv.TM_CCORR`:相关系数匹配 - `cv.TM_CCORR_NORMED`:相关系数归一化匹配 - `cv.TM_CCOEFF`:相关系数系数匹配 - `cv.TM_CCOEFF_NORMED`:相关系数系数归一化匹配 - `cv.TM_SQDIFF`:平方差匹配 - `cv.TM_SQDIFF_NORMED`:平方差归一化匹配 例如,如果我们要使用相关系数进行匹配,可以选择`cv.TM_CCORR`方: ``` result = cv.matchTemplate(source, template, cv.TM_CCORR) ``` 匹配结果`result`是一个单通道灰度影像,其中每个像素值表示了对应位置的匹配度得分。我们可以通过寻找最高得分的位置来确定最佳匹配位置: ``` min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result) ``` 其中,`max_val`表示最高得分,`max_loc`表示最高得分位置。 最后,我们可以在原始影像上标记出最佳匹配位置: ``` cv.rectangle(source, max_loc, (max_loc[0] + w, max_loc[1] + h), (0, 0, 255), 2) ``` 其中,`w`和`h`分别是目标影像的宽度和高度,`(0, 0, 255)`是画矩形的颜色,`2`是矩形线条的粗细。 通过以上步骤,我们就能够使用OpenCV实现相关系数影像匹配
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值