From 123ca7e1c5547650f9501965b87c35d92127cf88 Mon Sep 17 00:00:00 2001 From: Takahiro Poly Horikawa Date: Thu, 27 Nov 2014 16:29:05 +0900 Subject: [PATCH] Parallize building kmeans index in flann --- modules/flann/include/opencv2/flann/kmeans_index.h | 85 +++++++++++++++++----- 1 file changed, 67 insertions(+), 18 deletions(-) diff --git a/modules/flann/include/opencv2/flann/kmeans_index.h b/modules/flann/include/opencv2/flann/kmeans_index.h index bfc9b91..1655ac6 100644 --- a/modules/flann/include/opencv2/flann/kmeans_index.h +++ b/modules/flann/include/opencv2/flann/kmeans_index.h @@ -69,7 +69,6 @@ struct KMeansIndexParams : public IndexParams } }; - /** * Hierarchical kmeans index * @@ -271,6 +270,68 @@ public: return FLANN_INDEX_KMEANS; } + class KMeansDistanceComputer : public cv::ParallelLoopBody + { + public: + KMeansDistanceComputer(Distance _distance, const Matrix& _dataset, + const int _branching, const int* _indices, const Matrix& _dcenters, const int _veclen, + int* _count, int* _belongs_to, std::vector& _radiuses, bool* _updated) + : distance(_distance) + , dataset(_dataset) + , branching(_branching) + , indices(_indices) + , dcenters(_dcenters) + , veclen(_veclen) + , count(_count) + , belongs_to(_belongs_to) + , radiuses(_radiuses) + , updated(_updated) + { + } + + void operator()(const cv::Range& range) const + { + const int begin = range.start; + const int end = range.end; + + for( int i = begin; inew_sq_dist) { + new_centroid = j; + sq_dist = new_sq_dist; + } + } + if (sq_dist > radiuses[new_centroid]) { + radiuses[new_centroid] = sq_dist; + } + if (new_centroid != belongs_to[i]) { + count[belongs_to[i]]--; + count[new_centroid]++; + belongs_to[i] = new_centroid; + updated[i] = true; + } else { + updated[i] = false; + } + } + } + + private: + Distance distance; + const Matrix& dataset; + const int branching; + const int* indices; + const Matrix& dcenters; + int veclen; + int* count; + int* belongs_to; + std::vector& radiuses; + bool* updated; + }; + /** * Index constructor * @@ -708,6 +769,7 @@ private: bool converged = false; int iteration = 0; + bool* updated = new bool[indices_length]; while (!converged && iterationnew_sq_dist) { - new_centroid = j; - sq_dist = new_sq_dist; - } - } - if (sq_dist>radiuses[new_centroid]) { - radiuses[new_centroid] = sq_dist; - } - if (new_centroid != belongs_to[i]) { - count[belongs_to[i]]--; - count[new_centroid]++; - belongs_to[i] = new_centroid; - + if (updated[i]) { converged = false; + break; } } @@ -828,6 +876,7 @@ private: delete[] centers; delete[] count; delete[] belongs_to; + delete[] updated; } -- 2.7.4