OpenCV: Kmeans的使用一维和二维点集
2024-09-08 06:13:56
OpenCVKmeans算法默认使用了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;
}
即使如此,每次聚类的效果仍然不一定相同,显示一定的随机性。
最新文章
- C# - 网络编程 之 TcpClient与TcpListener
- CSS让图片垂直居中的几种技巧
- Extjs 源码组成(4.0.7)
- 论ubuntu的作死技巧
- cwRsync window下的跨服务器的文件同步
- ADO.NET 快速入门(八):处理 Errors
- 往xml中更新节点
- Runtime 函数 Swizzling 改变OC方法的调度顺序
- Win7无法访问NAS或Samba服务器解决之道 转
- cc2540 cc2541 低功耗实測和总结-与注意事项 - 低功耗小于10uA
- [Centos] mod_wsgi 安装流程以及遇到问题解决办法。apxs: command not found 或 Sorry, Python developer package does not appear to be installed.
- centos 下安装jdk、tomcat 以及tomcat无法从外部访问的解决办法
- PHP中反射的简单实用(动态代理)
- 【转】mysqldump与innobackupex知多少
- pytorch 学习--60分钟入个门
- for 练习
- c++官方文档-class
- 洛谷 P1073 最优贸易
- Qt中 QString 转 char*
- 解决:Bitmap too large to be uploaded into a texture exception
热门文章
- Navicat premium连接Oracle报ORA-12545错误
- 暑假集训D21总结
- nyoj_212_K尾相等数_210402272239
- [bzoj3669][Noi2014]魔法森林_LCT_并查集
- Xmemcached使用之与Spring整合
- 理解linux安装软件
- 前台JSON字符串,spring mvc controller也接收字符串
- android自带的处理Bitmap out Memory 的处理,我仅仅是改变了些写法成为自己用的东西
- 《解读window核心编程》 之 注冊表
- Android之——常见Bug及其解决方式