diff --git a/modules/flann/include/opencv2/flann/kmeans_index.h b/modules/flann/include/opencv2/flann/kmeans_index.h index 09d5bd2370..74bbf400c3 100644 --- a/modules/flann/include/opencv2/flann/kmeans_index.h +++ b/modules/flann/include/opencv2/flann/kmeans_index.h @@ -276,7 +276,7 @@ public: public: KMeansDistanceComputer(Distance _distance, const Matrix& _dataset, const int _branching, const int* _indices, const Matrix& _dcenters, const size_t _veclen, - int* _count, int* _belongs_to, std::vector& _radiuses, bool& _converged, cv::Mutex& _mtx) + int* _count, int* _belongs_to, std::vector& _radiuses, bool& _converged) : distance(_distance) , dataset(_dataset) , branching(_branching) @@ -287,7 +287,6 @@ public: , belongs_to(_belongs_to) , radiuses(_radiuses) , converged(_converged) - , mtx(_mtx) { } @@ -311,12 +310,10 @@ public: radiuses[new_centroid] = sq_dist; } if (new_centroid != belongs_to[i]) { - count[belongs_to[i]]--; - count[new_centroid]++; + CV_XADD(&count[belongs_to[i]], -1); + CV_XADD(&count[new_centroid], 1); belongs_to[i] = new_centroid; - mtx.lock(); converged = false; - mtx.unlock(); } } } @@ -332,7 +329,6 @@ public: int* belongs_to; std::vector& radiuses; bool& converged; - cv::Mutex& mtx; KMeansDistanceComputer& operator=( const KMeansDistanceComputer & ) { return *this; } }; @@ -801,8 +797,7 @@ private: } // reassign points to clusters - cv::Mutex mtx; - KMeansDistanceComputer invoker(distance_, dataset_, branching, indices, dcenters, veclen_, count, belongs_to, radiuses, converged, mtx); + KMeansDistanceComputer invoker(distance_, dataset_, branching, indices, dcenters, veclen_, count, belongs_to, radiuses, converged); parallel_for_(cv::Range(0, (int)indices_length), invoker); for (int i=0; i