OpenCV代码精妙之三 实现点集元素之间最小距离
goodFeaturesToTrack 可传入一个参数 表示 返回的特征点像素距离需要 高于 该参数指定的值
具体如何实现的呢
首先 goodFeaturesToTrack 返回的 特征点 是具有 所谓 强度 的,类似于 得分
得分越高, 越优先保留
在该基础之上, 如果某两个特征点 距离低于 了 参数 指定阈值,
那么 其中 得分较低 的将被 剔除
最简单的做法是,遍历所有特征点,
检查其他特征点 与该特征点的距离 是否低于阈值,如果低于,那么删除 这些特征点
该做法 时间复杂度 O(n*n)
OpenCV 的实现的核心 思想了 利用了一个二维数组,
该二维数组 实际上是 将 图像 分成了 N * N的网格
网格的高宽即为 距离阈值,
数组中每一个元素 是一个动态数组, 包含了坐标位于 该网格内的 所有特征点的坐标
std::vector<std::vector<Point2f> > grid(grid_width*grid_height);
注意,std::vector<Point2f>
即为 数组内每一个元素
grid 可理解为 一个对应到 图像像素平面 的二维数组, 两个维度大小分别为 grid_width, grid_height
然后 遍历 每一个特征点,
获取 所在网格, 然后 遍历 位于 该网格 以及 其 八连通 邻域内的特征点,
比较 其 距离是否 低于 距离阈值 即可
为什么 是 八连通 邻域呢?
如下图 所示, 即可得出该结论。即, 低于距离阈值的特征点 一定位于 该网格 以及 其 八连通 邻域内
const int grid_width = (w + cell_size - 1) / cell_size;
这种除法都是为了向上取整,比如 w为100, cell_size 为10, 那么10个cell 刚刚就够了
但w 为91, cell_size 为10, 此时也需要 10个cell