无法在后台减法器MOG2中设置用户参数
Cannot set user parameters in BackgroundSubtractorMOG2
OpenCV 库版本 2.42.我想BackgroundSubtractorMOG2
对象中设置一个参数,例如
BackgroundSubtractorMOG2 bgr;
// the following doesn't work because 'nmixtures', 'backgroundRatio'
// and 'fVarMin' are a protected members.
bgr.nmixtures = 3;
bgr.backgroundRatio = 0.9;
bgr.fVarMin = 5;
// the following works
bgr.set('nmixtures', 3);
// both of the following lines will give a run-time error
// `Access violation reading location 0x0000000000000008.`
bgr.set("backgroundRatio", 0.9);
bgr.set("fVarMin", 5);
backgroundRatio
和fVarMin
是控制算法的参数。用户应该能够根据文档更改这些参数。
如何设置BackgroundSubtractorMOG2
的参数?
编辑 正如下面的答案中正确提到的,这是OpenCV中的一个错误。该错误已在 OpenCV 版本 2.4.6 中修复。
我刚刚查看了OpenCV源代码,并在文件/modules/video/src/video_init.cpp
中找到了有趣的初始化。在这里:
CV_INIT_ALGORITHM(BackgroundSubtractorMOG2, "BackgroundSubtractor.MOG2",
obj.info()->addParam(obj, "history", obj.history);
obj.info()->addParam(obj, "nmixtures", obj.nmixtures);
obj.info()->addParam(obj, "varThreshold", obj.varThreshold);
obj.info()->addParam(obj, "detectShadows", obj.bShadowDetection));
似乎可以使用方法 set
仅设置这四个参数。
并且还可以查看文件modules/video/src/bgfg_gaussmix2.cpp
,它有一个BackgroundSubtractorMOG2
类。它具有以下字段:
float fVarInit;
float fVarMax;
float fVarMin;
//initial standard deviation for the newly generated components.
//It will will influence the speed of adaptation. A good guess should be made.
//A simple way is to estimate the typical standard deviation from the images.
//I used here 10 as a reasonable value
并且值fVarMin
(您要更改)设置为:
fVarMin = defaultVarMin2
在两个构造函数中。以下是所有这些:
static const float defaultVarInit2 = 15.0f; // initial variance for new components
static const float defaultVarMax2 = 5*defaultVarInit2;
static const float defaultVarMin2 = 4.0f;
有趣的是,此值未在任何其他文件中使用,因此目前似乎无法更改它。您可以将此问题直接发布到 OpenCV 错误跟踪器。
是的,bgr.set("nmixtures",3);
应该可以工作。 BackgroundSubtractorMOG2
继承自 cv::Algorithm
,因此您可以使用 cv:Algorithm::get
和 cv::Algorithm::set
来访问这些参数。你试过吗,它不起作用?
由于这些参数受到保护,因此派生类可以访问它们。我创建了一个派生类来设置所有必要的参数。
struct BackgroundModel2ParameterBlock {
int nmixtures;
float backgroundRatio;
float varThresholdGen;
float fVarInit;
float fVarMin;
float fVarMax;
BackgroundModel2ParameterBlock(void) :
nmixtures(3),
backgroundRatio(0.6),
varThresholdGen(6.25),
fVarInit(256),
fVarMin(256),
fVarMax(9e2)
{ }
};
class BackgroundModel2 : public cv::BackgroundSubtractorMOG2 {
private:
BackgroundModel2ParameterBlock m_param;
};
BackgroundModel2::BackgroundModel2(BackgroundModel2ParameterBlock param):
BackgroundSubtractorMOG2(),
m_param(param)
{
nmixtures = m_param.nmixtures;
backgroundRatio = m_param.backgroundRatio;
varThresholdGen = m_param.varThresholdGen;
fVarInit = m_param.fVarInit;
fVarMin = m_param.fVarMin;
fVarMax = m_param.fVarMax;
}
在android
中,使用算法函数:setDouble
、setInt
、setBool
:这有效:
mBgMog2 = new BackgroundSubtractorMOG2(mHistory,mMog2Threshold );
mBgMog2.setInt("nmixtures" , 3);
mBgMog2.setDouble("fVarInit" , 80.0);
mBgMog2.setDouble("fTau" , 0.2);
mBgMog2.setDouble("fVarMin" , 200.0);
mBgMog2.setDouble("fVarMax" , 80.0);
mBgMog2.setBool("detectShadows",false);
- 如何设置默认参数以防用户不输入另一个参数
- 错误:调用'strcmp'没有匹配函数(尝试设置显示用户信息功能)
- 如何为用户设置仅输入 4 位代码的限制?
- 在 C++ 中设置的用户定义数据类型
- 有没有办法根据用户参数设置函数指针?
- 如何打印其值和尺寸由用户设置的数组
- 如何将用户输入数组值移动到数组末尾并将其设置为 0
- 将 c++ 代码中的当前用户环境变量设置为对其他进程(如 cmds)可见
- 如何基于用户设置的初始变量设置一组常数值
- C++获取用户首选的编码或区域设置编码
- 使用 C 在 Linux 中设置用户的补充组 ID
- 如何创建库代码可以使用且用户可以设置的变量
- 具有C++设置错误的用户对象
- Win32 中是否有一条消息或通知来检测用户何时更改区域设置?
- 如何为Qwidget设置用户数据
- 无法在后台减法器MOG2中设置用户参数
- 要在Windows上读取/设置用户屏幕保护程序,是否需要使用系统注册表或SystemParametersInfo API
- 提升堆设置用户定义的比较功能
- 在自动化服务部署时设置用户服务运行的最佳实践是什么?
- 如何在不知道旧密码的情况下设置用户帐号密码