コード例 #1
0
ファイル: main.cpp プロジェクト: bigdig/GrabCut
/*
 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();
}
コード例 #2
0
ファイル: main.cpp プロジェクト: bigdig/GrabCut
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();
}