OpenCVKmeans算法默认使用了Kmeans++选取种子点

参考:OpenCv中Kmeans算法实现和使用

//效果:根据半径聚类,并不一定能得到好的结果。
float CBlotGlint::ClusterByR( )
{
//根据半径大小聚类,找出合适的类别个数和每一类的个数
std::vector<float> radiuses(this->blobs.size() );
std::vector<std::pair<float,int> > radiusesIdx(this->blobs.size() );
for ( int i=0; i< this->blobs.size(); ++i ){
radiuses[i] =this->blobs[i].diaGlint;
radiusesIdx[i].first = radiuses[i];
radiusesIdx[i].second = i;
} {
using namespace cv; this->blobs[0].diaGlint; const int MAX_CLUSTERS = 5;
Scalar colorTab[] =
{
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 );
std::vector<std::vector<float> > outC;
std::vector<std::vector<std::pair<float,int> > > outCidx; {
outC.clear();
int k;
int clusterCount = rng.uniform(2, MAX_CLUSTERS+1);
int i;
int sampleCount =radiuses.size();// rng.uniform(1, 1001);
Mat points(sampleCount, 1, CV_32FC1), labels;
for ( int i=0; i< sampleCount; ++i ){
points.at<float>(i) = radiuses[i];
} clusterCount = MIN(clusterCount, sampleCount);
clusterCount = std::max(clusterCount,3);
clusterCount = 3; Mat centers;
kmeans(points, clusterCount, labels,
TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),
3, KMEANS_PP_CENTERS, centers); outC.resize( clusterCount );
outCidx.resize( clusterCount ); img = Scalar::all(0);
for( i = 0; i < sampleCount; i++ )
{
int clusterIdx = labels.at<int>( i );
outC[clusterIdx].push_back( points.at<float>( i ) );
outCidx[clusterIdx].push_back(
std::make_pair ( points.at<float>( i ) ,radiusesIdx[i].second ) ); Point ipt = this->blobs[i].centerOfGlint;
circle( img, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA );
} cv::imshow("clusters", img); char key = (char)cv::waitKey(1);
} return 0; } return 1.0;
}

即使如此,每次聚类的效果仍然不一定相同,显示一定的随机性。

最新文章

  1. C# - 网络编程 之 TcpClient与TcpListener
  2. CSS让图片垂直居中的几种技巧
  3. Extjs 源码组成(4.0.7)
  4. 论ubuntu的作死技巧
  5. cwRsync window下的跨服务器的文件同步
  6. ADO.NET 快速入门(八):处理 Errors
  7. 往xml中更新节点
  8. Runtime 函数 Swizzling 改变OC方法的调度顺序
  9. Win7无法访问NAS或Samba服务器解决之道 转
  10. cc2540 cc2541 低功耗实測和总结-与注意事项 - 低功耗小于10uA
  11. [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.
  12. centos 下安装jdk、tomcat 以及tomcat无法从外部访问的解决办法
  13. PHP中反射的简单实用(动态代理)
  14. 【转】mysqldump与innobackupex知多少
  15. pytorch 学习--60分钟入个门
  16. for 练习
  17. c++官方文档-class
  18. 洛谷 P1073 最优贸易
  19. Qt中 QString 转 char*
  20. 解决:Bitmap too large to be uploaded into a texture exception

热门文章

  1. Navicat premium连接Oracle报ORA-12545错误
  2. 暑假集训D21总结
  3. nyoj_212_K尾相等数_210402272239
  4. [bzoj3669][Noi2014]魔法森林_LCT_并查集
  5. Xmemcached使用之与Spring整合
  6. 理解linux安装软件
  7. 前台JSON字符串,spring mvc controller也接收字符串
  8. android自带的处理Bitmap out Memory 的处理,我仅仅是改变了些写法成为自己用的东西
  9. 《解读window核心编程》 之 注冊表
  10. Android之——常见Bug及其解决方式