当前位置:首页 » python教程 » 正文

opencv3/C++实现视频背景去除建模(BSM)

看: 784次  时间:2021-01-14  分类 : python教程

视频背景建模主要使用到:

高斯混合模型(Mixture Of Gauss,MOG)

createBackgroundSubtractorMOG2(int history=500, double varThreshold=16,bool detectShadows=true);```

<p>K最近邻(k-NearestNeighbor,kNN)</p>

createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0, bool detectShadows=true);```

history:history的长度。

varThreshold:像素和模型之间马氏距离的平方的阈值。

detectShadows:默认为true,检测阴影并标记它们(影子会被标记为灰色)。 会降低了部分速度。

实例:

#include<opencv2/opencv.hpp>
using namespace cv;

int main()
{
  VideoCapture capture;
  capture.open("E:/image/01.avi");
  if(!capture.isOpened())
  {
    printf("can not open video file  \n");
    return -1;
  }
  Mat frame;
  namedWindow("input", CV_WINDOW_AUTOSIZE);
  namedWindow("MOG2", CV_WINDOW_AUTOSIZE);
  namedWindow("KNN", CV_WINDOW_AUTOSIZE);
  Mat maskMOG2, maskKNN;
  Ptr<BackgroundSubtractor> pMOG2 = createBackgroundSubtractorMOG2(500,25,true);
  Ptr<BackgroundSubtractor> pKNN = createBackgroundSubtractorKNN();

  Mat kernel = getStructuringElement(MORPH_RECT, Size(5,5));
  while (capture.read(frame))
  {
    imshow("input", frame);

    pMOG2->apply(frame, maskMOG2);
    pKNN->apply(frame, maskKNN);
    //对处理后的帧进行开操作,减少视频中较小的波动造成的影响
    morphologyEx(maskMOG2,maskMOG2, MORPH_OPEN, kernel, Point(-1,-1));
    morphologyEx(maskKNN,maskKNN, MORPH_OPEN, kernel, Point(-1,-1));

    imshow("MOG2", maskMOG2);
    imshow("KNN", maskKNN);
    waitKey(3);
  }

  capture.release();
  return 0;

}

视频中移动的玻璃球:

MOG分离出的小球区域:

KNN分离出的小球区域:

以上这篇opencv3/C++实现视频背景去除建模(BSM)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持python博客。

<< 上一篇 下一篇 >>

搜索

推荐资源

  Powered By python教程网   鲁ICP备18013710号
python博客 - 小白学python最友好的网站!