bulidin predict function in EM algorithm produce following error on console

asked 2014-11-05 12:29:02 -0600

updated 2014-11-07 12:39:38 -0600

berak gravatar image
static void gausinmixfground( const Mat& fgmask, Mat& clust,double learningRate,
                         Mat& bgmodel, int nmixtures, double backgroundRatio,
                         double varThreshold, double noiseSigma )
{
    int x, y, k, k1, rows = fgmask.rows, cols = fgmask.cols;
     int K = 10;   //no of gaussians
          Mat img_hsv;
     int ncount=0;
     cvtColor(fgmask,img_hsv,CV_RGB2HSV);
    CvEM em_model;
    CvEMParams params;
    for( y = 0; y < rows; y++ )        //count the no of pixcels with nonzero values 
       { const uchar* src2 = img_hsv.ptr<uchar>(y); 
         for( x = 0; x < cols; x++) 
            {
                Vec3f pix(src2[x*3], src2[x*3+1], src2[x*3+2]);
                bool eq =countNonZero(pix) == 0;
                //cout<<"\nprint"<<eq<<"\t";
                if(!eq)   
                {   ncount++;
                    //cout<<"values"<<ncount;
                }
            }
        }

      //  cout<<"ncount"<<ncount;
        float perctge= (float)ncount/(float)(rows*cols);
    if((perctge*100)<12)     //threshold to accept background frame with values greater than zero
        return;
     cout<<"\nenter in processing ";
    static  Mat samples1(rows*cols*5, 3, CV_32FC1 );
    //cout<<endl<<"row"<<samples1.rows<<"cols"<<samples1.cols<<"count"<<count<<"scount"<<scount;
     //   {  return;}
    if(count<5)   // creating sample data from starting five frame   
    {   uchar* dst = samples1.ptr<uchar>(scount); //sample data collector and scount keep track of all pixel 
        for( y = 0; y < rows; y++ )
        {    //const uchar* src1 = fgmask.ptr<uchar>(y); 
          const uchar* src = img_hsv.ptr<uchar>(y);   //image pointer 
               // fgmask pointer 

            for( x = 0; x < cols; x++)  
            {
             Vec3f pix(src[x*3], src[x*3+1], src[x*3+2]);  // check for non zero pixel
                 //cout<<pix;
                 bool eq =countNonZero(pix) == 0;
                // cout<<"print  "<<eq;
                if(!eq)
                {  //cout<<pix;
                    samples1.at<Vec3f > (scount, 0) = pix;
                     //cout<<samples1.at<Vec3f > (scount, 0);
                     //cout<<"scount"<<scount;
                     scount++;                    
                }
            }

        }
        count++;
    }

    else 
    {   if(count==5)   // execute the intialization step for EM algorithm
        {        Mat samples(scount, 3, CV_32FC1 );    // x i,3
                 Mat Means(K,3,CV_32FC1);
                 Mat probs(scount, K, CV_32FC1 );     //  Alpha k,i
                 Mat pie_prior(1,K,CV_32FC1);         // pie k
                 Mat sumprobs_pix=Mat::zeros(K,3, CV_32FC1 );
                 Mat convsum[K];
                  for(int a=0;a<K;a++)  
                   convsum[a]=Mat::eye(3, 3, CV_32FC1);                  
                 float inp= (float)1/K;
                  pie_prior = (Mat_<float>(1,K) << inp,inp, inp, inp, inp, inp,inp,inp,inp,inp);
                   cout<<pie_prior;
                 for(int l=0;l<scount;l++)
                  {
                    //const uchar* src1 = probs.ptr<uchar>(l);   //sample data in the form of no of instances* 3 matrix  
                    samples.at<Vec3f>(l,0) = samples1.at<Vec3f>(l,0);                    

                  }  
                     //cout<<probs;
                     //cout<<"samples count are"<<scount;
                    randu(Means, Scalar::all(0), Scalar::all(255));
                    //randn(Means , 127, 50 );
                     //cout<<Means;
                    cout<<"starting EM intialization";  //EM algorithm starts

    params.covs      = NULL;
    params.means     = NULL;
    params.weights   = NULL;
    params.probs     = NULL;
    params.nclusters = N;
    params.cov_mat_type       = CvEM::COV_MAT_SPHERICAL;
    params.start_step         = CvEM::START_AUTO_STEP;
    params.term_crit.max_iter = 300;
    params.term_crit.epsilon  = 0.1;
    params.term_crit.type     = CV_TERMCRIT_ITER|CV_TERMCRIT_EPS;

    // cluster the data
    em_model.train( samples, Mat(), params, &labels );         
                    }
                count++;  
        }
       else
        {   
          for( y = 0; y < rows; y++ )
            {    //const uchar* src1 = fgmask.ptr<uchar>(y); 
                const uchar* src = img_hsv.ptr<uchar>(y);   //image pointer 
                uchar* dst = clust.ptr<uchar>(y);        // fgmask pointer 
                //probs = em_model.getProbs();      
                for( x = 0; x < cols; x++ )  // for each pixcels create K ...
(more)
edit retag flag offensive close merge delete

Comments

we need to see your code here, please.

berak gravatar imageberak ( 2014-11-06 02:28:15 -0600 )edit

let's start with the obvious:

this is a wall of unreadable, ill-formatted code. no wonder you lost the overview.

berak gravatar imageberak ( 2014-11-07 12:43:34 -0600 )edit

const uchar* src2 = img_hsv.ptr<uchar>(y); // invalid access if img_hsv is a 3channel (Vec3b) image.

please run a debugger.

berak gravatar imageberak ( 2014-11-07 12:49:41 -0600 )edit

what on earth is labels ? and why are you passing the address of it to em_model.train() ?

please use cv::EM, not the c-api CvEM.

berak gravatar imageberak ( 2014-11-07 13:06:28 -0600 )edit