From 22ecdd16ef902f7c99f746ab83ff473f0ef7b238 Mon Sep 17 00:00:00 2001 From: Aleksandr Tischenko Date: Sun, 18 Mar 2018 14:11:42 +0200 Subject: [PATCH] Merge pull request #11101 from lamantine:fix_11100 * fixed bug #11100 Integer overflow in kmeans * fixed integer overflow in other divUp-s in kmeans code fixed warning about size_t to double conversion --- modules/core/src/kmeans.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/core/src/kmeans.cpp b/modules/core/src/kmeans.cpp index cbf9e47b87..9613971d6b 100644 --- a/modules/core/src/kmeans.cpp +++ b/modules/core/src/kmeans.cpp @@ -128,7 +128,7 @@ static void generateCentersPP(const Mat& data, Mat& _out_centers, parallel_for_(Range(0, N), KMeansPPDistanceComputer(tdist2, data, dist, ci), - divUp(dims * N, CV_KMEANS_PARALLEL_GRANULARITY)); + (double)divUp((size_t)(dims * N), CV_KMEANS_PARALLEL_GRANULARITY)); double s = 0; for (int i = 0; i < N; i++) { @@ -429,14 +429,14 @@ double cv::kmeans( InputArray _data, int K, if (isLastIter) { // don't re-assign labels to avoid creation of empty clusters - parallel_for_(Range(0, N), KMeansDistanceComputer(dists, labels, data, centers), divUp(dims * N, CV_KMEANS_PARALLEL_GRANULARITY)); + parallel_for_(Range(0, N), KMeansDistanceComputer(dists, labels, data, centers), (double)divUp((size_t)(dims * N), CV_KMEANS_PARALLEL_GRANULARITY)); compactness = sum(Mat(Size(N, 1), CV_64F, &dists[0]))[0]; break; } else { // assign labels - parallel_for_(Range(0, N), KMeansDistanceComputer(dists, labels, data, centers), divUp(dims * N * K, CV_KMEANS_PARALLEL_GRANULARITY)); + parallel_for_(Range(0, N), KMeansDistanceComputer(dists, labels, data, centers), (double)divUp((size_t)(dims * N * K), CV_KMEANS_PARALLEL_GRANULARITY)); } }