From 89434411159ac2988a7f6c8a3bb6bd4dd59b59e8 Mon Sep 17 00:00:00 2001 From: Hamdi Sahloul Date: Mon, 8 Jan 2018 18:26:10 +0900 Subject: [PATCH] resolves #10548 - `FLANN::knnSearch` garbage bug (when kNN is larger than the dataset size) --- modules/flann/src/miniflann.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/flann/src/miniflann.cpp b/modules/flann/src/miniflann.cpp index 9c3647d455..760a426bba 100644 --- a/modules/flann/src/miniflann.cpp +++ b/modules/flann/src/miniflann.cpp @@ -482,6 +482,9 @@ void runKnnSearch_(void* index, const Mat& query, Mat& indices, Mat& dists, typedef typename Distance::ResultType DistanceType; int type = DataType::type; int dtype = DataType::type; + IndexType* index_ = (IndexType*)index; + + CV_Assert((size_t)knn <= index_->size()); CV_Assert(query.type() == type && indices.type() == CV_32S && dists.type() == dtype); CV_Assert(query.isContinuous() && indices.isContinuous() && dists.isContinuous()); @@ -489,8 +492,8 @@ void runKnnSearch_(void* index, const Mat& query, Mat& indices, Mat& dists, ::cvflann::Matrix _indices(indices.ptr(), indices.rows, indices.cols); ::cvflann::Matrix _dists(dists.ptr(), dists.rows, dists.cols); - ((IndexType*)index)->knnSearch(_query, _indices, _dists, knn, - (const ::cvflann::SearchParams&)get_params(params)); + index_->knnSearch(_query, _indices, _dists, knn, + (const ::cvflann::SearchParams&)get_params(params)); } template