相关系数法程序设计 opencv C++ CSU
承接实验二
实验配置 C++ vs2017 opencv
实验3 相关系数法程序设计
一、实验目的
- 运用特征提取结果,编写相关系数法程序,掌握影像匹配的流程。
二、实验内容与要求
1 以灰度的形式读入两副核线影像
2 读入左影像提取的特征点
3 沿核线在右影像上计算每个候选匹配点的相关系数值
4 取极值点以及NCC大于阈值的点作为同名点
5 输出同名点到文件中,将同名点画到影像上,并用直线连接同名点。
三、设计与实现
3.1类的设计
3.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 | 取消 |
编辑框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中
参数:无
返回值:无
历史:**日期** **理由** **签名**
2019年10月11日 创建 ***
/**************************************************************************/
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