00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "opencv/ml.h"
00044 #include "opencv/highgui.h"
00045 using namespace std;
00046 using namespace cv;
00047 int main( int argc, char** argv )
00048 {
00049 const int N = 4;
00050 const int N1 = (int)sqrt((double)N);
00051 const CvScalar colors[] = {{0,0,255}},{{0,255,0}},{{0,255,255}},{{255,255,0}};
00052 int i, j;
00053 int nsamples = 100;
00054 CvRNG rng_state = cvRNG(-1);
00055 CvMat* samples = cvCreateMat( nsamples, 2, CV_32FC1 );
00056 CvMat* labels = cvCreateMat( nsamples, 1, CV_32SC1 );
00057 IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
00058 float _sample[2];
00059 CvMat sample = cvMat( 1, 2, CV_32FC1, _sample );
00060 CvEM em_model;
00061 CvEMParams params;
00062 CvMat samples_part;
00063
00064 cvReshape( samples, samples, 2, 0 );
00065 for( i = 0; i < N; i++ )
00066 {
00067 CvScalar mean, sigma;
00068
00069
00070 cvGetRows( samples, &samples_part, i*nsamples/N,
00071 (i+1)*nsamples/N );
00072 mean = cvScalar(((i
00073 ((i/N1)+1.)*img->height/(N1+1));
00074 sigma = cvScalar(30,30);
00075 cvRandArr( &rng_state, &samples_part, CV_RAND_NORMAL,
00076 mean, sigma );
00077 }
00078 cvReshape( samples, samples, 1, 0 );
00079
00080
00081 params.covs = NULL;
00082 params.means = NULL;
00083 params.weights = NULL;
00084 params.probs = NULL;
00085 params.nclusters = N;
00086 params.cov_mat_type = CvEM::COV_MAT_SPHERICAL;
00087 params.start_step = CvEM::START_AUTO_STEP;
00088 params.term_crit.max_iter = 10;
00089 params.term_crit.epsilon = 0.1;
00090 params.term_crit.type = CV_TERMCRIT_ITER|CV_TERMCRIT_EPS;
00091
00092
00093 em_model.train( samples, 0, params, labels );
00094
00095 #if 0
00096
00097
00098
00099
00100 CvEM em_model2;
00101 params.cov_mat_type = CvEM::COV_MAT_DIAGONAL;
00102 params.start_step = CvEM::START_E_STEP;
00103 params.means = em_model.get_means();
00104 params.covs = (const CvMat**)em_model.get_covs();
00105 params.weights = em_model.get_weights();
00106
00107 em_model2.train( samples, 0, params, labels );
00108
00109
00110 #endif
00111
00112 cvZero( img );
00113 for( i = 0; i < img->height; i++ )
00114 {
00115 for( j = 0; j < img->width; j++ )
00116 {
00117 CvPoint pt = cvPoint(j, i);
00118 sample.data.fl[0] = (float)j;
00119 sample.data.fl[1] = (float)i;
00120 int response = cvRound(em_model.predict( &sample, NULL ));
00121 CvScalar c = colors[response];
00122
00123 cvCircle( img, pt, 1, cvScalar(c.val[0]*0.75,
00124 c.val[1]*0.75,c.val[2]*0.75), CV_FILLED );
00125 }
00126 }
00127
00128
00129 for( i = 0; i < nsamples; i++ )
00130 {
00131 CvPoint pt;
00132 pt.x = cvRound(samples->data.fl[i*2]);
00133 pt.y = cvRound(samples->data.fl[i*2+1]);
00134 cvCircle( img, pt, 1, colors[labels->data.i[i]], CV_FILLED );
00135 }
00136
00137 cvNamedWindow( "EM-clustering result", 1 );
00138 cvShowImage( "EM-clustering result", img );
00139 cvWaitKey(0);
00140
00141 cvReleaseMat( &samples );
00142 cvReleaseMat( &labels );
00143 return 0;
00144 }