【问题标题】:opencv FLANN radiusSearch give bad resultsopencv FLANN radiusSearch 给出不好的结果
【发布时间】:2018-07-11 10:57:33
【问题描述】:

我想进行半径搜索以找到所有有效的邻居,但这似乎给了我错误的结果。这是我的代码

#include "opencv/cv.hpp"
#include <iostream>
#include <vector>

int main () {
    // create a group of points
    std::vector<cv::Point2f> points;
    points.emplace_back(438.6, 268.8);
    points.emplace_back(439.1, 268.6);
    points.emplace_back(438.2, 268.1);
    points.emplace_back(498.3, 285.9);
    points.emplace_back(312.9, 245.9);
    points.emplace_back(313.4, 245.7);
    points.emplace_back(313.1, 245.5);
    points.emplace_back(312.5, 245.4);
    points.emplace_back(297.6, 388.1);
    points.emplace_back(291.7, 309.8);
    points.emplace_back(194.1, 369.8);
    points.emplace_back(439.9, 314.9);
    points.emplace_back(312.8, 246.0);

    // create features array
    cv::Mat_<float> features(0, 2);

    for (auto && point : points) {

        //Fill matrix
        cv::Mat row = (cv::Mat_<float>(1, 2) << point.x, point.y);
        features.push_back(row);
    }
    std::cout << features << std::endl;

    cv::flann::Index flann_index(features, cv::flann::KDTreeIndexParams());
    std::vector<float> query{ 300.6f, 268.8f };
    std::vector<int> ind;
    std::vector<float> d;
    unsigned int max_neighbours = 10;
    // Here I deliberately increase the radius to contain all the points
    double radius = 500.0;
    flann_index.radiusSearch(query, ind, d, radius, max_neighbours,
        cv::flann::SearchParams());
}

ind 的输出是[0,0,0,0,0,0,0,0,0,0],全为零,这是意料之外的。

有人知道为什么吗?

=-=-=-=-=-=-=-=-=-=-= 更新

int main() {

// create a group of points
std::vector<cv::Point2f> points;
points.emplace_back(438.6, 268.8);
points.emplace_back(439.1, 268.6);
points.emplace_back(438.2, 268.1);
points.emplace_back(498.3, 285.9);
points.emplace_back(312.9, 245.9);
points.emplace_back(313.4, 245.7);
points.emplace_back(313.1, 245.5);
points.emplace_back(312.5, 245.4);
points.emplace_back(297.6, 388.1);
points.emplace_back(291.7, 309.8);
points.emplace_back(194.1, 369.8);
points.emplace_back(439.9, 314.9);
points.emplace_back(312.8, 246.0);

// create features array
cv::Mat_<float> features(0, 2);

for (auto && point : points) {

    //Fill matrix
    cv::Mat row = (cv::Mat_<float>(1, 2) << point.x, point.y);
    features.push_back(row);
}
std::cout << features << std::endl;

cv::flann::GenericIndex<cvflann::L2<float> > index(features, cvflann::KDTreeIndexParams());
std::vector<float> query(438.6f, 268.8f);
std::vector<int> ind;
std::vector<float> d;
index.radiusSearch(query, ind, d, 45.f, cvflann::SearchParams());
// I can print std::vector by some method, the reader may not, so I comment this line
//std::cout << d << std::endl;
}

由于 cv::flann::Index 已弃用,我更改为新的 API,但这一次,该程序不再工作。

【问题讨论】:

    标签: c++ opencv nearest-neighbor kdtree opencv3.1


    【解决方案1】:

    我遇到了这个问题,解决方法是半径必须指定为半径的平方(^2)。并且ind和d的长度会由max_neighbors来设置,但是必须检查radiusSearch的返回,找出哪个更小,num_found或者max_neighbours;

    double radius = 500.0;
    int num_found = flann_index.radiusSearch(query, ind, d, radius*radius, max_neighbours, cv::flann::SearchParams());
    num_found = min(num_found,(int)ind.size()); // check correct size
    for(int i=0; i<num_found; i++) { ... ind[i] ... d[i] ... }
    

    就我而言,我还必须调整索引和搜索参数以返回正确的结果:

    flann::KDTreeIndexParams indexParams(128);
    flann::SearchParams searchParams(1024,0.0,true);
    

    【讨论】:

      【解决方案2】:

      你必须设置ind和d的大小。

      【讨论】:

        【解决方案3】:

        如果您查看我使用的普通 FLANN 示例 here,您会看到他们调用 buildIndex(),但您没有。会这样吗?

        试试:

        cv::flann::Index flann_index(features, cv::flann::KDTreeIndexParams());
        flann_index.buildIndex();
        

        【讨论】:

        • cv::flann::Index 现在已弃用。我认为应该在示例中更新基于新 API 的新示例代码。我很快就会更新,也许你可以帮我检查一下。
        • 我已经添加了新代码,这次程序只是没有工作,它在运行时终止了。
        • 我的回答是否适用于原始问题@ted930511?
        猜你喜欢
        • 2023-03-30
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        • 2020-10-19
        相关资源
        最近更新 更多