Ask Your Question

Revision history [back]

bulidin predict function in EM algorithm produce following error on console

OpenCV Error: Assertion failed (isTrained()) in predict, file /home/munender/opencv-2.4.9/modules/ml/src/em.cpp, line 129 terminate called after throwing an instance of 'cv::Exception' what(): /home/munender/opencv-2.4.9/modules/ml/src/em.cpp:129: error: (-215) isTrained() in function predict

enter in processing Aborted (core dumped)

click to hide/show revision 2
below is the code with error

bulidin predict function in EM algorithm produce following error on console

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[x3], src2[x3+1], src2[x*3+2]); bool eq =countNonZero(pix) == 0; //cout<<"\nprint"<<eq&lt;&lt;"\t"; if(!eq)="" <br=""> { ncount++; //cout<<"values"<<ncount; }="" }="" }<="" p="">

  //  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 gaussian models
            {   // cout<<"entered in the maximization";
                Vec3f pix(src[x*3], src[x*3+1], src[x*3+2]);
                //cout<<pix;
                //float d2 = diff.dot(diff);
                 bool eq =countNonZero(pix) == 0;
                 // cout<<"print  "<<eq;
                if(!eq)
                {    //cout<<pix;
                     Mat sample( 1, 3, CV_32FC1 );
                     //sample.at<Vec3f>(1,0) = pix;
                      sample.at<Vec3f>(0) = pix;
                     //cout<<sample;
                     float response = em_model.predict(sample, NULL);
                      dst[x]=response;
                       cout<<response; 
                }

            }

        }

    }  
}
return;

} OpenCV Error: Assertion failed (isTrained()) in predict, file /home/munender/opencv-2.4.9/modules/ml/src/em.cpp, line 129 terminate called after throwing an instance of 'cv::Exception' what(): /home/munender/opencv-2.4.9/modules/ml/src/em.cpp:129: error: (-215) isTrained() in function predict

enter in processing Aborted (core dumped)

click to hide/show revision 3
No.3 Revision

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

berak gravatar image

bulidin predict function in EM algorithm produce following error on console

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[x3], src2[x3+1], pix(src2[x*3], src2[x*3+1], src2[x*3+2]);
                bool eq =countNonZero(pix) == 0;
                //cout<<"\nprint"<<eq&lt;&lt;"\t"; if(!eq)="" <br="">
//cout<<"\nprint"<<eq<<"\t";
                if(!eq)   
                {   ncount++;
                    //cout<<"values"<<ncount; }="" }="" }<="" p="">

//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 gaussian models
             {   // cout<<"entered in the maximization";
                 Vec3f pix(src[x*3], src[x*3+1], src[x*3+2]);
                 //cout<<pix;
                 //float d2 = diff.dot(diff);
                  bool eq =countNonZero(pix) == 0;
                  // cout<<"print  "<<eq;
                 if(!eq)
                 {    //cout<<pix;
                      Mat sample( 1, 3, CV_32FC1 );
                      //sample.at<Vec3f>(1,0) = pix;
                       sample.at<Vec3f>(0) = pix;
                      //cout<<sample;
                      float response = em_model.predict(sample, NULL);
                       dst[x]=response;
                        cout<<response; 
                 }

             }

         }

     }  
 }
 return;

} OpenCV Error: Assertion failed (isTrained()) in predict, file /home/munender/opencv-2.4.9/modules/ml/src/em.cpp, line 129 terminate called after throwing an instance of 'cv::Exception' what(): /home/munender/opencv-2.4.9/modules/ml/src/em.cpp:129: error: (-215) isTrained() in function predict

predict enter in processing Aborted (core dumped)

dumped)