kmeans()和partition()聚类测试

partition()聚类:

//距离小于10的点归为一类
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
using namespace cv;
using namespace std;
bool _EqPredicate(const Point& a, const Point& b)
{
return ((b.y - a.y)*(b.y - a.y) + (b.x - a.x)*(b.x - a.x)<10 * 10);
}
void main()
{
Mat img(500, 500, CV_8UC3);
RNG rng(12345);
vector<Point> points;
vector<int> labels;
int sampleCount = 6;
points.push_back(Point(200, 200));
points.push_back(Point(200, 215));
points.push_back(Point(205, 200));
points.push_back(Point(400, 200));
points.push_back(Point(400, 205));
points.push_back(Point(405, 200));
randShuffle(points, 1, &rng);
long count = partition(points, labels, _EqPredicate); //已经得到聚类后的标签labels
cout<<"classes number ="<<count<<endl;  //总共有count类
img = Scalar::all(0);
for (int i = 0; i < sampleCount; i++)
{
Point ipt = points.at(i);
circle(img, ipt, 2, CV_RGB(255, 255, 255), 1, CV_AA);
}
imshow("test-points", img);
img = Scalar::all(0);
for (int i = 0; i < sampleCount; i++)
{
int Idx = labels.at(i) + 1;
cout <<"label="<< Idx << endl;
Point ipt = points.at(i);
circle(img, ipt, 2, CV_RGB(Idx * 100 % 255, 0, 0), CV_FILLED, CV_AA);
}
imshow("partition result", img);
waitKey(0);
}


kmeans()聚类:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <iostream>
//http://www.cnblogs.com/tornadomeet/archive/2012/11/23/2783709.html    K-Means  Cluster
using namespace cv;
using namespace std;
// 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;
// }
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<int>(i);
Point ipt = points.at<Point2f>(i);
circle(img, ipt, 2, colorTab[clusterIdx], CV_FILLED, CV_AA);
}
imshow("clusterstest", img);
char key = (char)waitKey();     //无限等待
if (key == 27 || key == 'q' || key == 'Q') // 'ESC'
break;
}
return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

元气少女缘结神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值