问题描述
我想使用在OpenCV上实现的GrabCut算法。
I want to use the GrabCut algorithm implemented on OpenCV.
如这是函数签名:
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与自定义前景/背景模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!