#include
"stdafx.h"
using namespace cv;
int main(
int argc,
char**
argv)
{
Mat img=imread(
"d:/pic/lena.jpg");
imshow("src",img);
CV_Assert(!
img.empty());
vector<Mat>
planes;
split(img,planes);
int total=
img.total();
Mat p(total,3,CV_32F,Scalar::all(
0));
int i;
for(i=
0;i<total;i++
)
{
p.at<
float>(i,
0)=planes[
0].data[i];
p.at<
float>(i,
1)=planes[
1].data[i];
p.at<
float>(i,
2)=planes[
2].data[i];
}
RNG rng(12345);
int num=
0;
//canopy个数
double t1=
200.0,t2=
100.0;
//两个距离阈值
vector<Mat> canopy(total);
//canopy矩阵组
while(!
p.empty())
{
int r=p.rows;
//余下的数据的行数
Mat temp;
int k=rng.uniform(
0,r);
//在余下的数据中随机抽选一个作为canopy中心
cout<<
"The rest of number of rows: "<<r<<
", random:"<<k<<
endl;
for(i=
0;i<r;i++
)
{
double d=norm(p.row(k),p.row(i));
//计算选出点和其它点的距离
if(d<=
t1)
canopy[num].push_back(p.row(i)); //将距离小于t1的所有点放入到一个新的canopy中
if(d>
t2)
temp.push_back(p.row(i)); //更新数据
}
temp.copyTo(p);
num++
;
}
cout<<
"the total number of canopy:"<<num<<endl;
//最终类别数
for(i=
0;i<num;i++
)
cout<<
"the number of "<<i+
1<<
" class: "<<canopy[i].total()<<
endl;
waitKey(0);
return 0;
}
转载于:https://www.cnblogs.com/denny402/p/4669255.html