首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >opencv FLANN radiusSearch结果不佳

opencv FLANN radiusSearch结果不佳
EN

Stack Overflow用户
提问于 2018-02-01 15:19:45
回答 2查看 1.3K关注 0票数 1

我想执行radius搜索来查找所有有效的邻居,但它似乎给出了错误的结果。以下是我的代码

代码语言:javascript
复制
#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],全为零,这是意想不到的。

有人知道为什么吗?

=-=更新

代码语言:javascript
复制
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,但这一次,该程序不再工作。

EN

回答 2

Stack Overflow用户

发布于 2018-02-03 00:45:30

如果你检查我使用here的普通FLANN的例子,你会看到他们调用buildIndex(),而你没有。这是真的吗?

尝试:

代码语言:javascript
复制
cv::flann::Index flann_index(features, cv::flann::KDTreeIndexParams());
flann_index.buildIndex();
票数 0
EN

Stack Overflow用户

发布于 2021-05-11 17:01:10

您必须设置indd的大小。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48557303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档