#include <iostream>
{
const int MAX_CLUSTERS = 5;
{
};
for(;;)
{
int k, clusterCount = rng.
uniform(2, MAX_CLUSTERS+1);
int i, sampleCount = rng.
uniform(1, 1001);
clusterCount =
MIN(clusterCount, sampleCount);
std::vector<Point2f> centers;
for( k = 0; k < clusterCount; k++ )
{
Mat pointChunk = points.
rowRange(k*sampleCount/clusterCount,
k == clusterCount - 1 ? sampleCount
(k+1)*sampleCount/clusterCount);
}
randShuffle(points, 1, &rng);
double compactness = kmeans(points, clusterCount, labels,
TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 10, 1.0),
3, KMEANS_PP_CENTERS, centers);
img = Scalar::all(0);
for( i = 0; i < sampleCount; i++ )
{
int clusterIdx = labels.
at<
int>(i);
circle( img, ipt, 2, colorTab[clusterIdx], FILLED, LINE_AA );
}
for (i = 0; i < (int)centers.size(); ++i)
{
circle( img, c, 40, colorTab[i], 1, LINE_AA );
}
cout << "Compactness: " << compactness << endl;
imshow("clusters", img);
char key = (char)waitKey();
if( key == 27 || key == 'q' || key == 'Q' )
break;
}
return 0;
}
_Tp & at(int i0=0)
返回指定数组元素的引用。
Mat rowRange(int startrow, int endrow) const
为指定行跨度创建一个矩阵头。
int rows
行和列的数量,或者当矩阵具有超过 2 维时为 (-1, -1)
定义 mat.hpp:2138
_Tp y
点的 y 坐标
定义 types.hpp:202
_Tp x
点的 x 坐标
定义 types.hpp:201
int uniform(int a, int b)
返回 [a,b) 范围内的均匀分布的整型随机数
void fill(InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false)
用随机数填充数组。
定义迭代算法终止条件的类。
定义 types.hpp:886
#define CV_32FC2
定义 interface.h:119
#define CV_8UC3
定义 interface.h:90
#define MIN(a, b)
定义 cvdef.h:513
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
与磁盘上文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102