检测率计算,根据目标真实坐标文本和检测结果坐标文本

本文深入探讨了使用C++实现的一种坐标匹配算法,通过读取文件中的坐标数据,将其转化为坐标向量,并运用距离度量的方式进行坐标匹配,最终计算匹配成功率。涉及字符串处理、数据类型转换及坐标距离计算等关键技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include  <iostream>
#include  <fstream>
#include  <string>
#include <opencv2/opencv.hpp>

using namespace cv;
using   namespace  std;

//分割字符串
void SplitString(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
	std::string::size_type pos1, pos2;
	pos2 = s.find(c);
	pos1 = 0;
	while (std::string::npos != pos2)
	{
		v.push_back(s.substr(pos1, pos2 - pos1));

		pos1 = pos2 + c.size();
		pos2 = s.find(c, pos1);
	}
	if (pos1 != s.length())
		v.push_back(s.substr(pos1));
}

//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
	istringstream iss(str);
	Type num;
	iss >> num;
	return num;
}

//将文件中的字符串转成坐标向量
vector<vector<Point2d> > readFileToVector(const string& inFile){

	fstream origin;
	origin.open(inFile);
	std::string line;
	std::string c = " ";
	std::vector<std::string> v;
	double x = 0, y = 0;
	Point2d temp;
	vector<Point2d> tP;

	vector<vector<Point2d> > originPoints;
	while (getline(origin, line))
	{
		SplitString(line, v, c);

		for (int i = 0; i < v.size() - 1; i += 2)
		{
			x = stringToNum<double>(v[i]);
			y = stringToNum<double>(v[i + 1]);
			temp.x = x;
			temp.y = y;
			tP.push_back(temp);
		}
		originPoints.push_back(tP);

		tP.clear();
		v.clear();
	}

	return originPoints;
}

double distanceTwoPoints(Point2d p, Point2d q){
	return sqrtf((p.x - q.x)*(p.x - q.x) + (p.y - q.y)*(p.y - q.y));
}
//判断两个坐标是否匹配,这里根据两点之间距离作为度量
int matchPoint(vector<Point2d> A, vector<Point2d> B, double th){
	if (B.size() <= 0)
	{
		return -1;
	}
	int countP = 0;
	for (int i = 0; i < B.size();++i)     //B 参照 A进行匹配,成功加1结束
	{
		for (int j = 0; j < A.size();++j)
		{
			if (distanceTwoPoints(B[i],A[j]) <= th)
			{
				++countP;
				break;
			}
		}
	}
	return countP;

}

int main(){

	vector<vector<Point2d> > originPoints;
	originPoints = readFileToVector("a.txt");    //真实坐标
	vector<vector<Point2d> > detectPoints;
	detectPoints = readFileToVector("b.txt");    //检测坐标

	int matchPoints = 0;
	int allPoints = 0;
	for (int i = 0; i < originPoints.size();i++)
	{
		matchPoints += matchPoint(originPoints[i], detectPoints[i], 0.5);
		allPoints += originPoints[i].size();
	}

	double Fdr = (double)matchPoints / allPoints;
	cout << Fdr << endl;
	return 0;
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值