opencv实现canopy算法

时间:2024-06-07 10:36:14
#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,,CV_32F,Scalar::all());
int i;
for(i=;i<total;i++)
{
p.at<float>(i,)=planes[].data[i];
p.at<float>(i,)=planes[].data[i];
p.at<float>(i,)=planes[].data[i];
}
RNG rng();
int num=; //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(,r); //在余下的数据中随机抽选一个作为canopy中心
cout<<"The rest of number of rows: "<<r<<", random:"<<k<<endl;
for(i=;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=;i<num;i++)
cout<<"the number of "<<i+<<" class: "<<canopy[i].total()<<endl;
waitKey();
return ;
}