#include <iostream>
{
const int MAX_CLUSTERS = 5;
{
};
RNG rng(12345);
for(;;)
{
int k, clusterCount = rng.uniform(2, MAX_CLUSTERS+1);
int i, sampleCount = rng.uniform(1, 1001);
Mat points(sampleCount, 1,
CV_32FC2), labels;
clusterCount =
MIN(clusterCount, sampleCount);
std::vector<Point2f> centers;
for( k = 0; k < clusterCount; k++ )
{
center.
x = rng.uniform(0, img.cols);
center.y = rng.uniform(0, img.rows);
Mat pointChunk = points.rowRange(k*sampleCount/clusterCount,
k == clusterCount - 1 ? sampleCount :
(k+1)*sampleCount/clusterCount);
rng.fill(pointChunk, RNG::NORMAL,
Scalar(center.x, center.y),
Scalar(img.cols*0.05, img.rows*0.05));
}
double compactness =
kmeans(points, clusterCount, labels,
TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 10, 1.0),
img = Scalar::all(0);
for( i = 0; i < sampleCount; i++ )
{
int clusterIdx = labels.at<int>(i);
}
for (i = 0; i < (int)centers.size(); ++i)
{
}
cout << "Compactness: " << compactness << endl;
if( key == 27 || key == 'q' || key == 'Q' )
break;
}
return 0;
}
_Tp x
x coordinate of the point
Definition: modules/core/include/opencv2/core/types.hpp:201
void randShuffle(InputOutputArray dst, double iterFactor=1., RNG *rng=0)
Shuffles the array elements randomly.
Point2i Point
Definition: modules/core/include/opencv2/core/types.hpp:209
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
Point_< float > Point2f
Definition: modules/core/include/opencv2/core/types.hpp:207
double kmeans(InputArray data, int K, InputOutputArray bestLabels, TermCriteria criteria, int attempts, int flags, OutputArray centers=noArray())
Finds centers of clusters and groups input samples around the clusters.
@ KMEANS_PP_CENTERS
Definition: core/include/opencv2/core.hpp:3070
#define CV_32FC2
Definition: core/include/opencv2/core/hal/interface.h:119
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
#define MIN(a, b)
Definition: cvdef.h:514
@ circle
Definition: gr_skig.hpp:62
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
@ LINE_AA
antialiased line
Definition: imgproc/include/opencv2/imgproc.hpp:894
@ FILLED
Definition: imgproc/include/opencv2/imgproc.hpp:891
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
Definition: core/include/opencv2/core.hpp:107