gpt4 book ai didi

c++ - opencv FLANN radiusSearch 给出不好的结果

转载 作者:太空宇宙 更新时间:2023-11-03 23:11:48 26 4
gpt4 key购买 nike

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

#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,但这一次,程序不再工作了。

最佳答案

如果你检查一下我使用的普通 FLANN 的例子 here ,您会看到他们调用了 buildIndex(),而您没有。可以吗?

尝试:

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

关于c++ - opencv FLANN radiusSearch 给出不好的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48557303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com