#include"stdafx.h"using namespaceCV;intMainintargcChar**argv) {Mat img=imread ("d:/pic/lena.jpg"); Imshow ("src", IMG); Cv_assert (!img.empty ()); Vector<Mat>planes; Split (Img,planes); intTotal=img.total (); Mat p (Total,3, Cv_32f,scalar::all (0)); inti; 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); intnum=0;//Number of canopy Doublet1=200.0, t2=100.0;//Two distance thresholdvector<mat> canopy (total);//Canopy Matrix Group while(!P.empty ()) { intR=p.rows;//number of rows for the remaining dataMat temp; intK=rng.uniform (0, R);//randomly selected one of the remaining data as the Canopy centercout<<"The rest of number of rows:"<<r<<", Random:"<<k<<Endl; for(i=0; i<r;i++) { DoubleD=norm (P.row (k), P.row (i));//calculate the distance between the selected point and other points if(d<=t1) Canopy[num].push_back (P.row (i)); //Place all points that are less than T1 into a new canopy if(d>T2) Temp.push_back (P.row (i)); //Update Data} temp.copyto (P); Num++; } cout<<"The total number of canopy:"<<num<<endl;//number of final categories for(i=0; i<num;i++) cout<<" the number of"<<i+1<<"class:"<<canopy[i].total () <<Endl; Waitkey (0); return 0;}
OPENCV implementation of canopy algorithm