正如@Gombat 已经提到的,一个简单的方法是使用std::map,并使用std::pair<cv::Point, cv::Point> 作为键。
您需要。但是要提供自定义比较器,因为 cv::Point 不提供 operator<。
看看这段代码:
#include <opencv2/opencv.hpp>
#include <map>
using namespace cv;
using namespace std;
bool lessPoints(const Point& lhs, const Point& rhs)
{
return (lhs.x == rhs.x) ? lhs.y < rhs.y : lhs.x < rhs.x;
}
struct lessPairPoints
{
bool operator()(const pair<Point, Point>& lhs, const pair<Point, Point>& rhs) const
{
return (lhs.first == rhs.first) ? lessPoints(lhs.second, rhs.second) : lessPoints(lhs.first, rhs.first);
}
};
typedef map<pair<Point, Point>, float, lessPairPoints> MapPoints;
int main()
{
MapPoints map1;
map1[{Point(0, 0), Point(1, 1)}] = 0.3;
map1[{Point(1, 2), Point(1, 1)}] = 0.1;
for (const auto& el : map1)
{
cout << el.first.first << ", " << el.first.second << " -> " << el.second << endl;
}
cout << map1[{Point(0,0), Point(1,1)}] << endl;
auto pp = make_pair(Point(1,2), Point(1,1));
cout << map1[pp] << endl;
return 0;
}