背景减法器MOG2的掩码



如何告诉背景减法器MOG2哪些像素要更新到背景模型,哪些像素不应该更新。

我遇到问题,当有一个物体进入场景并停止几十秒钟时,该物体将被吸收到背景模型中。

我想降低学习率或停止围绕特定停止对象的学习,但我该怎么做呢?背景减法器MOG2是否支持在其更新函数中使用掩码?

我正在使用OpenCV 2.4.1。

BackgroundSubtractorMOG2不支持

屏蔽输入。但是,如果你知道要屏蔽哪些像素,你可以屏蔽输出:假设你已经调用了subtractor(input, fg, learningRate);并且你以某种方式知道物体现在在哪里(可能是你一直在使用平均偏移或模式识别来跟踪它),只需fg |= mask; mask在哪里,正如你从一些不同的来源知道的那样, 对象是。

您可以通过将学习率调得很低来实现此目的

即:

mog(input, output, 0.00000001);
您可以将

蒙版部分替换为背景图像:

BackgroundSubtractorMOG2 mog_bgs;
.
.
void my_apply(const cv::Mat& img, cv::Mat& fg, const cv::Mat& mask){
  cv::Mat last_bg;
  cv::Mat masked_img = img.clone();
  mog_bgs.getBackgroundImage(last_bg);
  last_bg.copyTo(masked_img, mask);
  mog_bgs.apply(masked_img, fg);
}

或重量屏蔽部件:

BackgroundSubtractorMOG2 mog_bgs;
.
.
void my_apply(const cv::Mat& img, cv::Mat& fg, const cv::Mat& mask){
  cv::Mat last_bg;
  cv::Mat masked_img = img.clone();
  mog_bgs.getBackgroundImage(last_bg);
  masked_img.forEach<Vec3b>([&](Vec3b& p, const int* position) -> void
    {
        if (mask.at<uchar>(position[0], position[1]) > 0) {
            auto b = last_bg.at<Vec3b>(position[0], position[1]);
            p[0] = p[0]*0.2 + b[0]*0.8;
            p[1] = p[1]*0.2 + b[1]*0.8;
            p[2] = p[2]*0.2 + b[2]*0.8;
        }
    });
  mog_bgs.apply(masked_img, fg);
}

最新更新