您好,欢迎来到华拓科技网。
搜索
您的当前位置:首页opencv实现canopy算法

opencv实现canopy算法

来源:华拓科技网
#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;
}

 

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo6.cn 版权所有 赣ICP备2024042791号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务