Ask Your Question
0

[SOLVED]use knnSearch, terminate called after throwing an instance of 'cvflann::anyimpl::bad_any_cast' [closed]

asked 2020-01-22 00:53:37 -0600

HelloWorld gravatar image

updated 2020-01-24 20:52:56 -0600

supra56 gravatar image

output error:

  terminate called after throwing an instance of 'cvflann::anyimpl::bad_any_cast'

My code:

cvflann::Matrix<uchar> data((uchar*) fetaures.data, fetaures.rows,
        fetaures.cols);
cvflann::Index<cvflann::Hamming<uchar>> flann_index(data,
        cvflann::LshIndexParams(10, 10, 10));
flann_index.buildIndex();

Ptr<ORB> detector = ORB::create(minHessian);
std::vector<cv::KeyPoint> keypoints;
cv::Mat queryDescriptors;
detector->detectAndCompute(Q, noArray(), keypoints, queryDescriptors);

Mat disMat(cv::Size(queryDescriptors.rows, queryDescriptors.cols), CV_32F);
Mat indeicesMat(cv::Size(queryDescriptors.rows, queryDescriptors.cols),
        CV_32F);
cvflann::Matrix<unsigned char> queries((unsigned char*) queryDescriptors.data,
        queryDescriptors.rows, queryDescriptors.cols);
cvflann::Matrix<int> indices((int*) indeicesMat.data, queryDescriptors.rows,
        queryDescriptors.cols);
cvflann::Matrix<int> dists((int*) disMat.data, queryDescriptors.rows,
        queryDescriptors.cols);
flann_index.knnSearch(queries, indices, dists, knn,
        cvflann::SearchParams(32));

And I found cvflann:index and cv::flann::index, which should i use?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by HelloWorld
close date 2020-01-24 06:50:55.097509

Comments

https://answers.opencv.org/question/1...

i see that post. but the method there doesn't work for me

HelloWorld gravatar imageHelloWorld ( 2020-01-22 04:46:28 -0600 )edit

why not use BFMatcher(HAMMING) with ORB ? (what do you need a knn index search for ?)

berak gravatar imageberak ( 2020-01-22 09:46:06 -0600 )edit

cv::flann::Index flann_index(samples_32f, cv::flann::LshIndexParams(10, 10, 10));

can work.

HelloWorld gravatar imageHelloWorld ( 2020-01-22 09:53:52 -0600 )edit

^^ not if you use ORB

berak gravatar imageberak ( 2020-01-22 09:57:38 -0600 )edit

to search image lable

HelloWorld gravatar imageHelloWorld ( 2020-01-22 23:10:16 -0600 )edit

samples_32f is a descriptor for many pictures

HelloWorld gravatar imageHelloWorld ( 2020-01-23 01:49:05 -0600 )edit

for (int i = 1; i < descriptors.size(); i++) { cv::vconcat(samples_32f, descriptors.at(i), samples_32f); }

HelloWorld gravatar imageHelloWorld ( 2020-01-23 01:56:08 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-01-23 05:28:38 -0600

berak gravatar image

you can try like this:

Mat trainData = ... // concatenated features, one per row

cv::Ptr<cv::flann::Index> index;

if (trainData.type() == CV_8U) // ORB
     index = makePtr<cv::flann::Index>(trainData, 
         cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_HAMMING);
else // SIFT
     index =  makePtr<cv::flann::Index>(trainData, 
         cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);

// later...

int K=5;
cv::flann::SearchParams params;
cv::Mat dists;
cv::Mat indices;
index->knnSearch(testFeature, indices, dists, K, params);
edit flag offensive delete link more

Comments

thank you !

HelloWorld gravatar imageHelloWorld ( 2020-01-23 08:23:25 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-01-22 00:53:37 -0600

Seen: 280 times

Last updated: Jan 24 '20