/* Learn GMMs parameters. */ static void learnGMMs( const Mat& img, const Mat& mask, const Mat& pixelModel, GMM& bgGMM, GMM& fgGMM ) { bgGMM.initParameter(); fgGMM.initParameter(); //Point p; for( int ci = 0; ci < GMM::modelNumInGMM; ci++ ) { for( int x = 0; x < img.rows; x++ ) { for( int y = 0; y < img.cols; y++ ) { if( pixelModel.at<int>(x,y) == ci ) { if( mask.at<uchar>(x,y) == BG || mask.at<uchar>(x,y) == PBG ) bgGMM.calcParameter( ci, img.at<Vec3b>(x,y) ); else fgGMM.calcParameter( ci, img.at<Vec3b>(x,y) ); } } } } bgGMM.endLearning(); fgGMM.endLearning(); }
/* Learn GMMs parameters. */ void learnGMMs( const Mat& img, const Mat& mask, const Mat& compIdxs, GMM& bgdGMM, GMM& fgdGMM ) { bgdGMM.initLearning(); fgdGMM.initLearning(); Point p; for( int ci = 0; ci < GMM::componentsCount; ci++ ) { for( p.y = 0; p.y < img.rows; p.y++ ) { for( p.x = 0; p.x < img.cols; p.x++ ) { if( compIdxs.at<int>(p) == ci ) { if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD ) bgdGMM.addSample( ci, img.at<Vec3b>(p) ); else fgdGMM.addSample( ci, img.at<Vec3b>(p) ); } } } } bgdGMM.endLearning(); fgdGMM.endLearning(); }
static void initGMMs( const Mat& img, const Mat& mask, GMM& bgGMM, GMM& fgGMM ) { const int kMeansNum = 10; const int kMeansType = KMEANS_PP_CENTERS; Mat bgClustered; Mat fgClustered; vector<Vec3f> bgSamples; vector<Vec3f> fgSamples; //for each pixel in the image, put those BG in bgsample, FG in fgsample for( int x = 0; x < img.rows; x++ ) { for( int y = 0; y < img.cols; y++ ) { if( mask.at<uchar>(x,y) == BG || mask.at<uchar>(x,y) == PBG ) bgSamples.push_back( (Vec3f)img.at<Vec3b>(x,y) ); else // GC_FGD | GC_PR_FGD fgSamples.push_back( (Vec3f)img.at<Vec3b>(x,y) ); } } Mat bgdSamplesMat( (int)bgSamples.size(), 3, CV_32FC1, &bgSamples[0][0] ); kmeans( bgdSamplesMat, GMM::modelNumInGMM, bgClustered, TermCriteria( CV_TERMCRIT_ITER, kMeansNum, 0.0), 0, kMeansType ); Mat fgdSamplesMat( (int)fgSamples.size(), 3, CV_32FC1, &fgSamples[0][0] ); kmeans( fgdSamplesMat, GMM::modelNumInGMM, fgClustered, TermCriteria( CV_TERMCRIT_ITER, kMeansNum, 0.0), 0, kMeansType ); bgGMM.initParameter(); for( int i = 0; i < (int)bgSamples.size(); i++ ) bgGMM.calcParameter( bgClustered.at<int>(i,0), bgSamples[i] ); bgGMM.endLearning(); fgGMM.initParameter(); for( int i = 0; i < (int)fgSamples.size(); i++ ) fgGMM.calcParameter( fgClustered.at<int>(i,0), fgSamples[i] ); fgGMM.endLearning(); }
/* Initialize GMM background and foreground models using kmeans algorithm. */ void initGMMs( const Mat& img, const Mat& mask, GMM& bgdGMM, GMM& fgdGMM ) { const int kMeansItCount = 10; const int kMeansType = KMEANS_PP_CENTERS; Mat bgdLabels, fgdLabels; vector<Vec3f> bgdSamples, fgdSamples; Point p; for( p.y = 0; p.y < img.rows; p.y++ ) { for( p.x = 0; p.x < img.cols; p.x++ ) { if( mask.at<uchar>(p) == GC_BGD || mask.at<uchar>(p) == GC_PR_BGD ) bgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) ); else // GC_FGD | GC_PR_FGD fgdSamples.push_back( (Vec3f)img.at<Vec3b>(p) ); } } CV_Assert( !bgdSamples.empty() && !fgdSamples.empty() ); Mat _bgdSamples( (int)bgdSamples.size(), 3, CV_32FC1, &bgdSamples[0][0] ); kmeans( _bgdSamples, GMM::componentsCount, bgdLabels, TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType, 0 ); Mat _fgdSamples( (int)fgdSamples.size(), 3, CV_32FC1, &fgdSamples[0][0] ); kmeans( _fgdSamples, GMM::componentsCount, fgdLabels, TermCriteria( CV_TERMCRIT_ITER, kMeansItCount, 0.0), 0, kMeansType, 0 ); bgdGMM.initLearning(); for( int i = 0; i < (int)bgdSamples.size(); i++ ) bgdGMM.addSample( bgdLabels.at<int>(i,0), bgdSamples[i] ); bgdGMM.endLearning(); fgdGMM.initLearning(); for( int i = 0; i < (int)fgdSamples.size(); i++ ) fgdGMM.addSample( fgdLabels.at<int>(i,0), fgdSamples[i] ); fgdGMM.endLearning(); }