#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 << endl;
imshow("聚類", 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:2165
_Tp y
點的 y 座標
定義 types.hpp:202
_Tp x
點的 x 座標
定義 types.hpp:201
隨機數生成器。
Definition core.hpp:2879
int uniform(int a, int b)
返回 [a,b) 範圍內均勻分佈的整數隨機數
void fill(InputOutputArray mat, int distType, InputArray a, InputArray b, bool saturateRange=false)
用隨機數填充陣列。
為迭代演算法定義終止標準的類。
定義 types.hpp:893
#define CV_32FC2
定義 interface.h:119
#define MIN(a, b)
定義 cvdef.h:514
int main(int argc, char *argv[])
定義 highgui_qt.cpp:3