em.cpp
Go to the documentation of this file.
00001 // Software License Agreement (BSD License)
00002 // 
00003 //   Copyright (c) 2011, Shulei Zhu <schuleichu@gmail.com>
00004 //   All rights reserved.
00005 // 
00006 //   Redistribution and use in source and binary forms, with or without
00007 //   modification, are permitted provided that the following conditions
00008 //   are met:
00009 // 
00010 //    * Redistributions of source code must retain the above copyright
00011 //      notice, this list of conditions and the following disclaimer.
00012 //    * Redistributions in binary form must reproduce the above
00013 //      copyright notice, this list of conditions and the following
00014 //      disclaimer in the documentation and/or other materials provided
00015 //      with the distribution.
00016 //    * Neither the name of Shulei Zhu nor the names of its
00017 //      contributors may be used to endorse or promote products derived
00018 //      from this software without specific prior written permission.
00019 // 
00020 //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 //   BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 //   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 //   CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 //   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 //   ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 //   POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // 
00034 // em.cpp --- 
00035 // File            : em.cpp
00036 // Created: Sa Jun 18 14:05:36 2011 (+0200)
00037 // Author: Shulei Zhu
00038 
00039 // Code:
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         // form the training samples
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     // initialize model's parameters
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     // cluster the data
00093     em_model.train( samples, 0, params, labels );
00094 
00095 #if 0
00096     // the piece of code shows how to repeatedly optimize the model
00097     // with less-constrained parameters
00098     //(COV_MAT_DIAGONAL instead of COV_MAT_SPHERICAL)
00099     // when the output of the first stage is used as input for the second.
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     // to use em_model2, replace em_model.predict()
00109     // with em_model2.predict() below
00110 #endif
00111     // classify every image pixel
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     //draw the clustered samples
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 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends Defines


contracting_curve_density_algorithm
Author(s): Shulei Zhu, Dejan Pangercic
autogenerated on Thu May 23 2013 14:41:45