OpenCV - GrabCut与自定义前景/背景模型 [英] OpenCV - GrabCut with custom foreground/background models

查看:777
本文介绍了OpenCV - GrabCut与自定义前景/背景模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用在OpenCV上实现的GrabCut算法。

I want to use the GrabCut algorithm implemented on OpenCV.

文档这是函数签名:

void grabCut(
    InputArray img, 
    InputOutputArray mask, 
    Rect rect, 
    InputOutputArray bgdModel, // *
    InputOutputArray fgdModel, // *
    int iterCount, 
    int mode=GC_EVAL) 

> param,指示如何使用 rect (矩形边界框)或掩码(其值对应于用户绘制的前景的矩阵)初始化算法/背景区域。

The mode param, indicates how to initialize the algorithm, either with the rect (a rectangle bounding box) or with the mask (a matrix whose values correspond to user paintings of the foreground/background regions.

我已经有了FG和BG的颜色模型,所以理想情况下我不需要提供掩码或矩形,这些模型作为初始化(我想防止OpenCV计算新模型和使用我的代替)。我发现 bgdModel fgdModel 参数以某种方式包含此模型信息。 Unfortunatelly,文档不提供有关如何存储模型信息的任何详细信息

I already have the color models for both the FG and the BG, so ideally I shouldn’t need to provide a mask or a rectangle, but use those models as an initialization (I want to prevent OpenCV to compute new models and use mine instead). I see that bgdModel and fgdModel parameters somehow contain this model information. Unfortunatelly, the documentation does not provide any details on how the model information is stored there.

可以填充这些模型连接现有数据并使用 c $ c = mode = GC_EVAL 运行方法,如果需要,我需要如何对模型进行编码? / p>

Is it possible to populate those models whith existing data and run the method with mode=GC_EVAL?, if so, how do I need to encode the models?

推荐答案

在opencv / sources / modules / imgproc / src / grabcut.cpp中,你可以看看模型编码:

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<double>(0);
    mean = coefs + componentsCount;
    cov = mean + 3*componentsCount;

    for( int ci = 0; ci < componentsCount; ci++ )
        if( coefs[ci] > 0 )
             calcInverseCovAndDeterm( ci );
}

所以你需要为每个模型cv :: Mat 1 x 65双(componentsCount等于5)。每个组件有3个手段,因为它在RGB颜色空间中进行计算。使用GC_EVAL确实会使模型完好无损,但我从未尝试过使用预先计算的模型。

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.

这篇关于OpenCV - GrabCut与自定义前景/背景模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆