2014-08-14 16:50:07 +08:00
|
|
|
/*M///////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
|
|
|
//
|
|
|
|
// By downloading, copying, installing or using the software you agree to this license.
|
|
|
|
// If you do not agree to this license, do not download, install,
|
|
|
|
// copy or use the software.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
2018-01-22 19:14:02 +08:00
|
|
|
// Copyright (C) 2000-2018, Intel Corporation, all rights reserved.
|
2014-08-14 16:50:07 +08:00
|
|
|
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
|
|
|
|
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
|
|
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
// are permitted provided that the following conditions are met:
|
|
|
|
//
|
|
|
|
// * Redistribution's of source code must retain the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
//
|
|
|
|
// * Redistribution's in binary form must reproduce the above copyright notice,
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
//
|
|
|
|
// * The name of the copyright holders may not be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// This software is provided by the copyright holders and contributors "as is" and
|
|
|
|
// any express or implied warranties, including, but not limited to, the implied
|
|
|
|
// warranties of merchantability and fitness for a particular purpose are disclaimed.
|
|
|
|
// In no event shall the Intel Corporation or contributors be liable for any direct,
|
|
|
|
// indirect, incidental, special, exemplary, or consequential damages
|
|
|
|
// (including, but not limited to, procurement of substitute goods or services;
|
|
|
|
// loss of use, data, or profits; or business interruption) however caused
|
|
|
|
// and on any theory of liability, whether in contract, strict liability,
|
|
|
|
// or tort (including negligence or otherwise) arising in any way out of
|
|
|
|
// the use of this software, even if advised of the possibility of such damage.
|
|
|
|
//
|
|
|
|
//M*/
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2018-01-17 22:39:23 +08:00
|
|
|
#include <opencv2/core/utils/configuration.private.hpp>
|
2018-09-06 16:43:07 +08:00
|
|
|
#include <opencv2/core/hal/hal.hpp>
|
2014-08-14 16:50:07 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////// kmeans ////////////////////////////////////////////
|
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
2018-01-17 22:39:23 +08:00
|
|
|
static int CV_KMEANS_PARALLEL_GRANULARITY = (int)utils::getConfigurationParameterSizeT("OPENCV_KMEANS_PARALLEL_GRANULARITY", 1000);
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
static void generateRandomCenter(int dims, const Vec2f* box, float* center, RNG& rng)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
float margin = 1.f/dims;
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
center[j] = ((float)rng*(1.f+margin*2.f)-margin)*(box[j][1] - box[j][0]) + box[j][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
class KMeansPPDistanceComputer : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
2018-01-22 19:14:02 +08:00
|
|
|
KMeansPPDistanceComputer(float *tdist2_, const Mat& data_, const float *dist_, int ci_) :
|
|
|
|
tdist2(tdist2_), data(data_), dist(dist_), ci(ci_)
|
|
|
|
{ }
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2018-03-15 21:16:50 +08:00
|
|
|
void operator()( const cv::Range& range ) const CV_OVERRIDE
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2014-08-14 16:50:07 +08:00
|
|
|
const int begin = range.start;
|
|
|
|
const int end = range.end;
|
2018-01-22 19:14:02 +08:00
|
|
|
const int dims = data.cols;
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int i = begin; i<end; i++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-09-06 16:43:07 +08:00
|
|
|
tdist2[i] = std::min(hal::normL2Sqr_(data.ptr<float>(i), data.ptr<float>(ci), dims), dist[i]);
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-22 19:14:02 +08:00
|
|
|
KMeansPPDistanceComputer& operator=(const KMeansPPDistanceComputer&); // = delete
|
2014-08-14 16:50:07 +08:00
|
|
|
|
|
|
|
float *tdist2;
|
2018-01-22 19:14:02 +08:00
|
|
|
const Mat& data;
|
2014-08-14 16:50:07 +08:00
|
|
|
const float *dist;
|
2018-01-22 19:14:02 +08:00
|
|
|
const int ci;
|
2014-08-14 16:50:07 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
k-means center initialization using the following algorithm:
|
|
|
|
Arthur & Vassilvitskii (2007) k-means++: The Advantages of Careful Seeding
|
|
|
|
*/
|
2018-01-22 19:14:02 +08:00
|
|
|
static void generateCentersPP(const Mat& data, Mat& _out_centers,
|
2014-08-14 16:50:07 +08:00
|
|
|
int K, RNG& rng, int trials)
|
|
|
|
{
|
2017-05-25 23:59:01 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2018-01-22 19:14:02 +08:00
|
|
|
const int dims = data.cols, N = data.rows;
|
|
|
|
cv::AutoBuffer<int, 64> _centers(K);
|
2014-08-14 16:50:07 +08:00
|
|
|
int* centers = &_centers[0];
|
2018-01-22 19:14:02 +08:00
|
|
|
cv::AutoBuffer<float, 0> _dist(N*3);
|
2014-08-14 16:50:07 +08:00
|
|
|
float* dist = &_dist[0], *tdist = dist + N, *tdist2 = tdist + N;
|
|
|
|
double sum0 = 0;
|
|
|
|
|
|
|
|
centers[0] = (unsigned)rng % N;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int i = 0; i < N; i++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-09-06 16:43:07 +08:00
|
|
|
dist[i] = hal::normL2Sqr_(data.ptr<float>(i), data.ptr<float>(centers[0]), dims);
|
2014-08-14 16:50:07 +08:00
|
|
|
sum0 += dist[i];
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 1; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
double bestSum = DBL_MAX;
|
|
|
|
int bestCenter = -1;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < trials; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
double p = (double)rng*sum0;
|
|
|
|
int ci = 0;
|
|
|
|
for (; ci < N - 1; ci++)
|
|
|
|
{
|
|
|
|
p -= dist[ci];
|
|
|
|
if (p <= 0)
|
2014-08-14 16:50:07 +08:00
|
|
|
break;
|
2018-01-22 19:14:02 +08:00
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
|
|
|
|
parallel_for_(Range(0, N),
|
2018-01-22 19:14:02 +08:00
|
|
|
KMeansPPDistanceComputer(tdist2, data, dist, ci),
|
2018-03-18 20:11:42 +08:00
|
|
|
(double)divUp((size_t)(dims * N), CV_KMEANS_PARALLEL_GRANULARITY));
|
2018-01-22 19:14:02 +08:00
|
|
|
double s = 0;
|
|
|
|
for (int i = 0; i < N; i++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
s += tdist2[i];
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (s < bestSum)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
bestSum = s;
|
|
|
|
bestCenter = ci;
|
|
|
|
std::swap(tdist, tdist2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
centers[k] = bestCenter;
|
|
|
|
sum0 = bestSum;
|
|
|
|
std::swap(dist, tdist);
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* src = data.ptr<float>(centers[k]);
|
2014-08-14 16:50:07 +08:00
|
|
|
float* dst = _out_centers.ptr<float>(k);
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
dst[j] = src[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
template<bool onlyDistance>
|
2014-08-14 16:50:07 +08:00
|
|
|
class KMeansDistanceComputer : public ParallelLoopBody
|
|
|
|
{
|
|
|
|
public:
|
2018-01-22 19:14:02 +08:00
|
|
|
KMeansDistanceComputer( double *distances_,
|
|
|
|
int *labels_,
|
|
|
|
const Mat& data_,
|
|
|
|
const Mat& centers_)
|
|
|
|
: distances(distances_),
|
|
|
|
labels(labels_),
|
|
|
|
data(data_),
|
|
|
|
centers(centers_)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-03-15 21:16:50 +08:00
|
|
|
void operator()(const Range& range) const CV_OVERRIDE
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
CV_TRACE_FUNCTION();
|
2014-08-14 16:50:07 +08:00
|
|
|
const int begin = range.start;
|
|
|
|
const int end = range.end;
|
|
|
|
const int K = centers.rows;
|
|
|
|
const int dims = centers.cols;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int i = begin; i < end; ++i)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2015-03-06 15:17:37 +08:00
|
|
|
const float *sample = data.ptr<float>(i);
|
2017-01-31 17:05:08 +08:00
|
|
|
if (onlyDistance)
|
|
|
|
{
|
|
|
|
const float* center = centers.ptr<float>(labels[i]);
|
2018-09-06 16:43:07 +08:00
|
|
|
distances[i] = hal::normL2Sqr_(sample, center, dims);
|
2017-01-31 17:05:08 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-01-22 19:14:02 +08:00
|
|
|
else
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
int k_best = 0;
|
|
|
|
double min_dist = DBL_MAX;
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* center = centers.ptr<float>(k);
|
2018-09-06 16:43:07 +08:00
|
|
|
const double dist = hal::normL2Sqr_(sample, center, dims);
|
2018-01-22 19:14:02 +08:00
|
|
|
|
|
|
|
if (min_dist > dist)
|
|
|
|
{
|
|
|
|
min_dist = dist;
|
|
|
|
k_best = k;
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
distances[i] = min_dist;
|
|
|
|
labels[i] = k_best;
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-01-22 19:14:02 +08:00
|
|
|
KMeansDistanceComputer& operator=(const KMeansDistanceComputer&); // = delete
|
2014-08-14 16:50:07 +08:00
|
|
|
|
|
|
|
double *distances;
|
|
|
|
int *labels;
|
|
|
|
const Mat& data;
|
|
|
|
const Mat& centers;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
double cv::kmeans( InputArray _data, int K,
|
|
|
|
InputOutputArray _bestLabels,
|
|
|
|
TermCriteria criteria, int attempts,
|
|
|
|
int flags, OutputArray _centers )
|
|
|
|
{
|
2018-09-14 05:35:26 +08:00
|
|
|
CV_INSTRUMENT_REGION();
|
2014-08-14 16:50:07 +08:00
|
|
|
const int SPP_TRIALS = 3;
|
|
|
|
Mat data0 = _data.getMat();
|
2018-01-22 19:14:02 +08:00
|
|
|
const bool isrow = data0.rows == 1;
|
|
|
|
const int N = isrow ? data0.cols : data0.rows;
|
|
|
|
const int dims = (isrow ? 1 : data0.cols)*data0.channels();
|
|
|
|
const int type = data0.depth();
|
2014-08-14 16:50:07 +08:00
|
|
|
|
|
|
|
attempts = std::max(attempts, 1);
|
|
|
|
CV_Assert( data0.dims <= 2 && type == CV_32F && K > 0 );
|
|
|
|
CV_Assert( N >= K );
|
|
|
|
|
|
|
|
Mat data(N, dims, CV_32F, data0.ptr(), isrow ? dims * sizeof(float) : static_cast<size_t>(data0.step));
|
|
|
|
|
|
|
|
_bestLabels.create(N, 1, CV_32S, -1, true);
|
|
|
|
|
|
|
|
Mat _labels, best_labels = _bestLabels.getMat();
|
2018-01-22 19:14:02 +08:00
|
|
|
if (flags & CV_KMEANS_USE_INITIAL_LABELS)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
CV_Assert( (best_labels.cols == 1 || best_labels.rows == 1) &&
|
|
|
|
best_labels.cols*best_labels.rows == N &&
|
|
|
|
best_labels.type() == CV_32S &&
|
|
|
|
best_labels.isContinuous());
|
2018-01-22 19:14:02 +08:00
|
|
|
best_labels.reshape(1, N).copyTo(_labels);
|
|
|
|
for (int i = 0; i < N; i++)
|
|
|
|
{
|
|
|
|
CV_Assert((unsigned)_labels.at<int>(i) < (unsigned)K);
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (!((best_labels.cols == 1 || best_labels.rows == 1) &&
|
2014-08-14 16:50:07 +08:00
|
|
|
best_labels.cols*best_labels.rows == N &&
|
2018-01-22 19:14:02 +08:00
|
|
|
best_labels.type() == CV_32S &&
|
|
|
|
best_labels.isContinuous()))
|
|
|
|
{
|
|
|
|
_bestLabels.create(N, 1, CV_32S);
|
|
|
|
best_labels = _bestLabels.getMat();
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
_labels.create(best_labels.size(), best_labels.type());
|
|
|
|
}
|
|
|
|
int* labels = _labels.ptr<int>();
|
|
|
|
|
|
|
|
Mat centers(K, dims, type), old_centers(K, dims, type), temp(1, dims, type);
|
2018-01-22 19:14:02 +08:00
|
|
|
cv::AutoBuffer<int, 64> counters(K);
|
|
|
|
cv::AutoBuffer<double, 64> dists(N);
|
2014-08-14 16:50:07 +08:00
|
|
|
RNG& rng = theRNG();
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (criteria.type & TermCriteria::EPS)
|
2014-08-14 16:50:07 +08:00
|
|
|
criteria.epsilon = std::max(criteria.epsilon, 0.);
|
|
|
|
else
|
|
|
|
criteria.epsilon = FLT_EPSILON;
|
|
|
|
criteria.epsilon *= criteria.epsilon;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (criteria.type & TermCriteria::COUNT)
|
2014-08-14 16:50:07 +08:00
|
|
|
criteria.maxCount = std::min(std::max(criteria.maxCount, 2), 100);
|
|
|
|
else
|
|
|
|
criteria.maxCount = 100;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (K == 1)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
attempts = 1;
|
|
|
|
criteria.maxCount = 2;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
cv::AutoBuffer<Vec2f, 64> box(dims);
|
|
|
|
if (!(flags & KMEANS_PP_CENTERS))
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* sample = data.ptr<float>(0);
|
|
|
|
for (int j = 0; j < dims; j++)
|
|
|
|
box[j] = Vec2f(sample[j], sample[j]);
|
|
|
|
}
|
|
|
|
for (int i = 1; i < N; i++)
|
|
|
|
{
|
|
|
|
const float* sample = data.ptr<float>(i);
|
|
|
|
for (int j = 0; j < dims; j++)
|
|
|
|
{
|
|
|
|
float v = sample[j];
|
|
|
|
box[j][0] = std::min(box[j][0], v);
|
|
|
|
box[j][1] = std::max(box[j][1], v);
|
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
double best_compactness = DBL_MAX;
|
|
|
|
for (int a = 0; a < attempts; a++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
double compactness = 0;
|
|
|
|
|
|
|
|
for (int iter = 0; ;)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
double max_center_shift = iter == 0 ? DBL_MAX : 0.0;
|
|
|
|
|
2014-08-14 16:50:07 +08:00
|
|
|
swap(centers, old_centers);
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (iter == 0 && (a > 0 || !(flags & KMEANS_USE_INITIAL_LABELS)))
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (flags & KMEANS_PP_CENTERS)
|
2014-08-14 16:50:07 +08:00
|
|
|
generateCentersPP(data, centers, K, rng, SPP_TRIALS);
|
|
|
|
else
|
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2018-06-11 06:42:00 +08:00
|
|
|
generateRandomCenter(dims, box.data(), centers.ptr<float>(k), rng);
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// compute centers
|
|
|
|
centers = Scalar(0);
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
counters[k] = 0;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int i = 0; i < N; i++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* sample = data.ptr<float>(i);
|
|
|
|
int k = labels[i];
|
2014-08-14 16:50:07 +08:00
|
|
|
float* center = centers.ptr<float>(k);
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
center[j] += sample[j];
|
|
|
|
counters[k]++;
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (counters[k] != 0)
|
2014-08-14 16:50:07 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// if some cluster appeared to be empty then:
|
|
|
|
// 1. find the biggest cluster
|
|
|
|
// 2. find the farthest from the center point in the biggest cluster
|
|
|
|
// 3. exclude the farthest point from the biggest cluster and form a new 1-point cluster.
|
|
|
|
int max_k = 0;
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k1 = 1; k1 < K; k1++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (counters[max_k] < counters[k1])
|
2014-08-14 16:50:07 +08:00
|
|
|
max_k = k1;
|
|
|
|
}
|
|
|
|
|
|
|
|
double max_dist = 0;
|
|
|
|
int farthest_i = -1;
|
2018-01-22 19:14:02 +08:00
|
|
|
float* base_center = centers.ptr<float>(max_k);
|
|
|
|
float* _base_center = temp.ptr<float>(); // normalized
|
2014-08-14 16:50:07 +08:00
|
|
|
float scale = 1.f/counters[max_k];
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
|
|
|
_base_center[j] = base_center[j]*scale;
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int i = 0; i < N; i++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (labels[i] != max_k)
|
2014-08-14 16:50:07 +08:00
|
|
|
continue;
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* sample = data.ptr<float>(i);
|
2018-09-06 16:43:07 +08:00
|
|
|
double dist = hal::normL2Sqr_(sample, _base_center, dims);
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (max_dist <= dist)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
max_dist = dist;
|
|
|
|
farthest_i = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
counters[max_k]--;
|
|
|
|
counters[k]++;
|
|
|
|
labels[farthest_i] = k;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
const float* sample = data.ptr<float>(farthest_i);
|
|
|
|
float* cur_center = centers.ptr<float>(k);
|
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
base_center[j] -= sample[j];
|
|
|
|
cur_center[j] += sample[j];
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int k = 0; k < K; k++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
float* center = centers.ptr<float>(k);
|
|
|
|
CV_Assert( counters[k] != 0 );
|
|
|
|
|
|
|
|
float scale = 1.f/counters[k];
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
center[j] *= scale;
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (iter > 0)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
double dist = 0;
|
|
|
|
const float* old_center = old_centers.ptr<float>(k);
|
2018-01-22 19:14:02 +08:00
|
|
|
for (int j = 0; j < dims; j++)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
double t = center[j] - old_center[j];
|
|
|
|
dist += t*t;
|
|
|
|
}
|
|
|
|
max_center_shift = std::max(max_center_shift, dist);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-31 17:05:08 +08:00
|
|
|
bool isLastIter = (++iter == MAX(criteria.maxCount, 2) || max_center_shift <= criteria.epsilon);
|
2014-08-14 16:50:07 +08:00
|
|
|
|
2017-01-31 17:05:08 +08:00
|
|
|
if (isLastIter)
|
2018-01-22 19:14:02 +08:00
|
|
|
{
|
|
|
|
// don't re-assign labels to avoid creation of empty clusters
|
2018-06-11 06:42:00 +08:00
|
|
|
parallel_for_(Range(0, N), KMeansDistanceComputer<true>(dists.data(), labels, data, centers), (double)divUp((size_t)(dims * N), CV_KMEANS_PARALLEL_GRANULARITY));
|
2018-01-22 19:14:02 +08:00
|
|
|
compactness = sum(Mat(Size(N, 1), CV_64F, &dists[0]))[0];
|
2017-01-31 17:05:08 +08:00
|
|
|
break;
|
2018-01-22 19:14:02 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// assign labels
|
2018-06-11 06:42:00 +08:00
|
|
|
parallel_for_(Range(0, N), KMeansDistanceComputer<false>(dists.data(), labels, data, centers), (double)divUp((size_t)(dims * N * K), CV_KMEANS_PARALLEL_GRANULARITY));
|
2018-01-22 19:14:02 +08:00
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 19:14:02 +08:00
|
|
|
if (compactness < best_compactness)
|
2014-08-14 16:50:07 +08:00
|
|
|
{
|
|
|
|
best_compactness = compactness;
|
2018-01-22 19:14:02 +08:00
|
|
|
if (_centers.needed())
|
2017-12-16 02:48:48 +08:00
|
|
|
{
|
2018-01-22 19:14:02 +08:00
|
|
|
if (_centers.fixedType() && _centers.channels() == dims)
|
|
|
|
centers.reshape(dims).copyTo(_centers);
|
|
|
|
else
|
|
|
|
centers.copyTo(_centers);
|
2017-12-16 02:48:48 +08:00
|
|
|
}
|
2014-08-14 16:50:07 +08:00
|
|
|
_labels.copyTo(best_labels);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return best_compactness;
|
|
|
|
}
|