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

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

问题描述

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

I want to use the GrabCut algorithm implemented on OpenCV.

文档所示,这是函数签名:

As shown in the documentation this is the function signature:

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

mode 参数,指示如何使用 rect(矩形边界框)或 ma​​sk(一个矩阵,其值对应于用户对前景/背景区域的绘制.

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 计算新模型和改用我的).我看到 bgdModelfgdModel 参数以某种方式包含此模型信息.不幸的是,文档没有提供任何细节关于模型信息是如何存储在那里的.

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.

是否可以使用现有数据填充这些模型并使用 mode=GC_EVAL运行该方法?,如果可以,我该怎么做需要对模型进行编码吗?

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 你可以看看模型(GMM)是如何编码的:

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 );
}

因此,您需要为每个模型提供 1 x 65 双精度的 cv::Mat(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天全站免登陆