Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

yes, but you have to "flatten" (reshape) the Mat to an 1d float vector

// assuming, you have a vector<Mat> vecs for training:
Mat trainData; // one row for each train Mat
for (size_t i=0; i<vecs.size(); i++) {
    Mat m = vecs[i].reshape(1,1);
    m.convertTo(m, CV_32F);
    trainData.push_back(m);
}
cv::Ptr<cv::flann::Index> = 
    makePtr<cv::flann::Index>(trainData, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);


Mat test = ...;
test = test.reshape(1,1);
test.convertTo(test, CV_32F);

int K=5;
cv::flann::SearchParams params;
cv::Mat dists;
cv::Mat indices;
index->knnSearch(testFeature, indices, dists, K, params);

yes, but you have to "flatten" (reshape) the Mat to an 1d float vector

// assuming, you have a vector<Mat> vecs for training:
Mat trainData; // one row for each train Mat
// WARNING ! YOU HAVE TO KEEP THE trainData MAT ALIVE UNTIL THE PREDICTION !!!
// (flann does dirty things with pointers there)

for (size_t i=0; i<vecs.size(); i++) {
    Mat m = vecs[i].reshape(1,1);
    m.convertTo(m, CV_32F);
    trainData.push_back(m);
}
cv::Ptr<cv::flann::Index> = 
    makePtr<cv::flann::Index>(trainData, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);


Mat test = ...;
test = test.reshape(1,1);
test.convertTo(test, CV_32F);

int K=5;
cv::flann::SearchParams params;
cv::Mat dists;
cv::Mat indices;
index->knnSearch(testFeature, indices, dists, K, params);

yes, but you have to "flatten" (reshape) the Mat to an 1d float vector

// assuming, you have a vector<Mat> vecs for training:
Mat trainData; // one row for each train Mat
// WARNING ! YOU HAVE TO KEEP THE trainData MAT ALIVE UNTIL THE PREDICTION !!!
// (flann does dirty things with pointers there)

for (size_t i=0; i<vecs.size(); i++) {
    Mat m = vecs[i].reshape(1,1);
    m.convertTo(m, CV_32F);
    trainData.push_back(m);
}
cv::Ptr<cv::flann::Index> = 
    makePtr<cv::flann::Index>(trainData, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);


Mat test = ...;
test = test.reshape(1,1);
test.convertTo(test, CV_32F);

int K=5;
cv::flann::SearchParams params;
cv::Mat dists;
cv::Mat indices;
index->knnSearch(testFeature, indices, dists, K, params);