C++ C++;修复OpenCV squares.cpp示例以合并闭合正方形

C++ C++;修复OpenCV squares.cpp示例以合并闭合正方形,c++,algorithm,opencv,computational-geometry,C++,Algorithm,Opencv,Computational Geometry,我一直在使用OpenCV squares.cpp示例来查找图像中正方形的角坐标 该示例有时会多次匹配每个对象。现在我想“合并”,计算每个似乎属于同一对象的正方形的平均角坐标 findSquares函数的结果结构是三维向量类型结构,如下所示: [ [[10,10],[100,10],[100,100],[10,100]], // First square [[100,100],[300,100],[300,100],[100,300]], // Second square

我一直在使用OpenCV squares.cpp示例来查找图像中正方形的角坐标

该示例有时会多次匹配每个对象。现在我想“合并”,计算每个似乎属于同一对象的正方形的平均角坐标

findSquares函数的结果结构是三维向量类型结构,如下所示:

[
    [[10,10],[100,10],[100,100],[10,100]], // First square
    [[100,100],[300,100],[300,100],[100,300]], // Second square
    [[8,11],[110,5],[106,109],[10,97]], // Should be meged with the first square 
    [[112,99],[296,103],[312,98],[92,300]] // Should be merged with the second square
]

(我是C++初学者,所以我用JavaScript数组符号编写向量结构,只是为了可视化结构)

我的想法是:

  • 找到彼此匹配的所有方块(所有方块的4个点在最大距离阈值内,其他方块的4个点)
  • 将它们分组,计算正方形的平均4点
  • 将新的平均平方保存到结果向量
有好的算法吗?OpenCV有内置的吗?有更好的方法吗?

替代方法:

Order by `x` coordinate of first point (the point closes to the origin or something)
For each square:
   Search forwards for a matching point until the `x` coordinate is bigger than the threshold.
这应该是关于
O(nlogn+n)=O(nlogn)
,但是如果有许多正方形彼此靠近,则可能是
O(n^2)