資源簡介
本代碼為一個簡單的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“?<//?}
//對隨機數進行的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? {
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? {
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\li
?????文件????????4798??2014-04-14?12:18??kmensForImage\kmensForImage\Debug\kmensForImage.tlog\li
?????文件?????????704??2014-04-14?12:18??kmensForImage\kmensForImage\Debug\kmensForImage.tlog\li
?????文件??????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個文件信息
評論
共有 條評論