OpenCV - GrabCut with custom foreground/background models

前端 未结 2 870
深忆病人
深忆病人 2021-01-03 04:21

I want to use the GrabCut algorithm implemented on OpenCV.

As shown in the documentation this is the function signature:

void grabCut(
    InputArra         


        
2条回答
  •  滥情空心
    2021-01-03 04:44

    In opencv/sources/modules/imgproc/src/grabcut.cpp you can have a look how the models (GMMs) are encoded:

    GMM::GMM( Mat& _model )
    {
        const int modelSize = 3/*mean*/ + 9/*covariance*/ + 1/*component weight*/;
        if( _model.empty() )
        {
            _model.create( 1, modelSize*componentsCount, CV_64FC1 );
            _model.setTo(Scalar(0));
        }
        else if( (_model.type() != CV_64FC1) || (_model.rows != 1) || (_model.cols != modelSize*componentsCount) )
            CV_Error( CV_StsBadArg, "_model must have CV_64FC1 type, rows == 1 and cols == 13*componentsCount" );
    
        model = _model;
    
        coefs = model.ptr(0);
        mean = coefs + componentsCount;
        cov = mean + 3*componentsCount;
    
        for( int ci = 0; ci < componentsCount; ci++ )
            if( coefs[ci] > 0 )
                 calcInverseCovAndDeterm( ci );
    }
    

    So you need for every model a cv::Mat of 1 x 65 doubles (componentsCount equals 5). There are 3 means per component because its computing in RGB colorspace. Using GC_EVAL would indeed leave the models intact but I never tried it with pre-computed models.

提交回复
热议问题