相关系数法程序设计 opencv C++ CSU

承接实验二

实验配置 C++ vs2017 opencv

实验3 相关系数法程序设计

一、实验目的

  • 运用特征提取结果,编写相关系数法程序,掌握影像匹配的流程。

二、实验内容与要求

1 以灰度的形式读入两副核线影像

2 读入左影像提取的特征点

3 沿核线在右影像上计算每个候选匹配点的相关系数值

4 取极值点以及NCC大于阈值的点作为同名点

5 输出同名点到文件中,将同名点画到影像上,并用直线连接同名点。

三、设计与实现

3.1类的设计

图1:类的设计示意图

3.2控件按钮及其属性

图2:主界面图

按钮ID CADPTION 对应函数
IDOK 打开并显示灰度图像 OnBnClickedOk()
IDC_read2 二值化图像 OnBnClickedread2()
IDC_read3 Guass滤波/opencv的Guass滤波图像 OnBnClickedread3()
IDC_read5 Moravec算子 OnBnClickedread5()
IDC_read6 相关系数法匹配 OnBnClickedread6()
IDC_read4 帮助 OnBnClickedread4()
IDCANCEL 取消

图3:相关系数法程序设计图

编辑框ID 变量名 变量类型
IDC_Moravecsize1 Moravecsize1 int
IDC_Moravecsize2 Moravecsize2 int
IDC_Moraveclowestdoor Moraveclowstdoor int
IDC_dist_width dist_width int
IDC_lowst_door lowst_door float
IDC_halflengthsize halflengthsize int
按钮ID CADPTION 对应函数
ID_readleft 打开左影像 OnBnClickedreadleft()
ID_readright 打开右影像 OnBnClickedreadright()
ID_view2 左侧Moravec算子输出至txt中 OnBnClickedview()
ID_view 读取txt显示 OnBnClickedview2()
IDC_BUTTON1 ? OnBnClickedButton1()
IDCANCEL 取消

3.3主要代码

这里仅展示涉及到相关系数法相关功能的代码。

3.3.1文件:< CMatchingImg.h >
/***************************************************************************
类:CMatchingImg
作用:核线影像相关系数计算类
Welcome to my Github and my CSDN blog , more information will be available about the project!
Github:https://github.com/Yiqingde
CSDN Blog:https://me.csdn.net/weixin_42348202
历史:**日期**         **理由**            **签名**
	  2019年10月11日        创建              ***
/**************************************************************************/
#pragma once
#include "zrxCFeatureVector.h"
class CMatchingImg
{
   
public:
	CMatchingImg();
	~CMatchingImg();
	int Div(const CString strLine, char split, CStringArray &strArray);//字符串分割函数
	void lastview(Mat match_LImg, Mat match_RImg, vector<Point3f> f, vector<Point3f> r);//最后显示
	void Get_coefficient(Mat Lmatchwindow, Mat match_RImg, int x, int y, float &cofficent);//求相关系数
	bool read(vector < Point3f> &f);//读取txt到vector
	void vectorsort(vector < Point3f> &Temp_sort);//排序
	void MatchingImg(Mat &match_LImg, Mat &match_RImg, Mat match_RGBLImg, Mat match_RGBRImg, float lowst_door, int dist_width, int halflengthsize);//主函数
};
3.3.2文件:< CMatchingImg.cpp >
#include "stdafx.h"
#include "CMatchingImg.h"


CMatchingImg::CMatchingImg()
{
   
}


CMatchingImg::~CMatchingImg()
{
   
}
//***********************************
//字符分割函数
//***********************************
int CMatchingImg::Div(const CString strLine, char split, CStringArray &strArray)
{
   
	strArray.RemoveAll();//自带清空属性
	CString temp = strLine;
	int tag = 0;
	while (1)
	{
   
		tag = temp.Find(split);
		if (tag >= 0)
		{
   
			strArray.Add(temp.Left(tag));
			temp = temp.Right(temp.GetLength() - tag - 1);
		}
		else {
    break; }
	}
	strArray.Add(temp);
	return strArray.GetSize();
}

/***************************************************************************
函数:read(vector < Point3i> &f)
作用:读取txt文件,储存至vector中
参数:无
返回值:无
历史:**日期**         **理由**            **签名**
	  20191011日        创建              ***
/**************************************************************************/
bool CMatchingImg::read(vector < Point3f> &f)
{
   
	CFileDialog dlgFile(TRUE, _T("txt"), NULL,
		OFN_ALLOWMULTISELECT | OFN_EXPLORER,
		//_T("(文本文件)|*.txt"));
		_T(""));
	if (dlgFile.DoModal() == IDCANCEL) return 0;
	CString strName = dlgFile.GetPathName();//获取打开文件文件名(路径)
	setlocale(LC_ALL, "");
	CStdioFile sf;
	if (!sf.Open(strName, CFile::modeRead)) return 0;//打开strName文件路径中的内容
	CString strLine;
	CString strContent;//接受内容字符串
	CStringArray array;//供下文分割使用
	strContent.Empty();//strContent中内容清空
	//开始读数据 
	BOOL bEOF = sf.ReadString(strLine);//第一行
	if (bEOF == 0) {
    AfxMessageBox(_T("空数据")); return 0; }
	//读取控制点
	bEOF = sf.ReadString(strLine);//第二行 
	if (bEOF == 0) {
    AfxMessageBox(_T("数据不规范")); return 0; }
	bEOF = sf.ReadString(strLine);//第三行 
	if (bEOF == 0) {
    AfxMessageBox(_T("数据不规范")); return 0; }
	int n = Div(strLine, ':', array);
	if (n < 2) {
    AfxMessageBox(_T("数据缺失")); return 0; }
	int i = _ttoi(array[1]);
	bEOF = sf.ReadString(strLine);//第四行 
	if (bEOF == 0) {
    AfxMessageBox(_T("数据不规范")); return 0; }
	while 
  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值