资源简介

本代码为一个简单的Kmeans应用。分为两个部分,第一部分为对随机数的Kmeans聚类;第二部分为对图像的简单聚类。

资源截图

代码片段和文件信息

#include “stdafx.h“

// static void help()
// {
//     cout << “\nThis program demonstrates kmeans clustering.\n“
//             “It generates an image with random points then assigns a random number of cluster\n“
//             “centers and uses kmeans to move those cluster centers to their representitive location\n“
//             “Call\n“
//             “./kmeans\n“ << endl;
// }
//对随机数进行的kmeans 聚类

int main(int /*argc*/ char** /*argv*/)
{
const int MAX_CLUSTERS = 5;
Scalar colorTab[] =     //因为最多只有5类,所以最多也就给5个颜色
{
Scalar(0 0 255)
Scalar(0 255 0)
Scalar(255 100 100)
Scalar(255 0 255)
Scalar(0 255 255)
};

Mat img(500 500 CV_8UC3);
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;   //产生的样本数,实际上为2通道的列向量,元素类型为Point2f

clusterCount = MIN(clusterCount sampleCount);
Mat centers(clusterCount 1 points.type());    //用来存储聚类后的中心点

/* generate random sample from multigaussian distribution */
for (k = 0; k < clusterCount; k++) //产生随机数
{
Point center;
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 CV_RAND_NORMAL Scalar(center.x center.y) Scalar(img.cols*0.05 img.rows*0.05));
}

randShuffle(points 1 &rng);   //因为要聚类,所以先随机打乱points里面的点,注意points和pointChunk是共用数据的。

kmeans(points clusterCount labels
TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER 10 1.0)
3 KMEANS_PP_CENTERS centers);  //聚类3次,取结果最好的那次,聚类的初始化采用PP特定的随机算法。

img = Scalar::all(0);

for (i = 0; i < sampleCount; i++)
{
int clusterIdx = labels.at(i);
Point ipt = points.at(i);
circle(img ipt 2 colorTab[clusterIdx] CV_FILLED CV_AA);
}

imshow(“clusters“ img);

char key = (char)waitKey();     //无限等待
if (key == 27 || key == ‘q‘ || key == ‘Q‘) // ‘ESC‘
break;
}

return 0;
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2014-04-14 12:33  kmensForImage\
     目录           0  2014-04-14 12:12  kmensForImage\Debug\
     文件       83968  2014-04-14 12:18  kmensForImage\Debug\kmensForImage.exe
     文件      554932  2014-04-14 12:18  kmensForImage\Debug\kmensForImage.ilk
     文件     1731584  2014-04-14 12:18  kmensForImage\Debug\kmensForImage.pdb
     目录           0  2014-04-14 09:38  kmensForImage\ipch\
     目录           0  2014-04-14 12:02  kmensForImage\ipch\kmensforimage-762f8a82\
     文件    55967744  2014-04-14 12:02  kmensForImage\ipch\kmensforimage-762f8a82\kmensforimage-3e0d76b4.ipch
     目录           0  2014-04-14 12:33  kmensForImage\kmensForImage\
     目录           0  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\
     文件       96657  2014-04-14 12:03  kmensForImage\kmensForImage\Debug\kForRng.obj
     文件        2248  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.log
     文件      154928  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.obj
     文件    22872064  2014-04-14 09:47  kmensForImage\kmensForImage\Debug\kmensForImage.pch
     目录           0  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\
     文件       17666  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\CL.read.1.tlog
     文件        1904  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\CL.write.1.tlog
     文件        2386  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\cl.command.1.tlog
     文件         174  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\kmensForImage.lastbuildstate
     文件        2010  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\link.command.1.tlog
     文件        4798  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\link.read.1.tlog
     文件         704  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\kmensForImage.tlog\link.write.1.tlog
     文件      250396  2014-04-14 09:47  kmensForImage\kmensForImage\Debug\stdafx.obj
     文件      560128  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\vc120.idb
     文件     1183744  2014-04-14 12:18  kmensForImage\kmensForImage\Debug\vc120.pdb
     文件        1647  2014-04-14 12:11  kmensForImage\kmensForImage\ReadMe.txt
     文件      137544  2014-04-14 12:23  kmensForImage\kmensForImage\city.jpg
     文件        2425  2014-04-14 12:03  kmensForImage\kmensForImage\kForRng.cpp
     文件        1458  2014-04-14 12:33  kmensForImage\kmensForImage\kmensForImage.cpp
     文件        5199  2014-04-14 12:12  kmensForImage\kmensForImage\kmensForImage.vcxproj
     文件        1320  2014-04-14 12:12  kmensForImage\kmensForImage\kmensForImage.vcxproj.filters
............此处省略9个文件信息

评论

共有 条评论