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

yipeiwu_com7年前Python基础

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

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

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

K最近邻(k-NearestNeighbor,kNN)

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设计】。

相关文章

Python实现按特定格式对文件进行读写的方法示例

Python实现按特定格式对文件进行读写的方法示例

本文实例讲述了Python实现按特定格式对文件进行读写的方法。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 class...

python、PyTorch图像读取与numpy转换实例

Tensor转为numpy np.array(Tensor) numpy转换为Tensor torch.Tensor(numpy.darray) PIL.Image.Image转换成nu...

快速查询Python文档方法分享

快速查询Python文档方法分享

Pydoc本地HTML形式查看 我们在编写Python代码时,常常会去查询某些模块及函数的使用,会选择dir()及help()函数、或查看CHM格式的Python帮助文档、或查看Pyth...

python之virtualenv的简单使用方法(必看篇)

什么是virtualenv? virtualenv可以创建独立Python开发环境,比如当前的全局开发环境是python3.6,现在我们有一个项目需要使用django1.3,另一个项目需...

关于pip的安装,更新,卸载模块以及使用方法(详解)

在Python的学习过程中,肯定会遇到很多安装模块的地方,可以使用easy_install安装,但是easy_install相对于pip而言,最大的缺陷就是它所安装的模块是不能够卸载的,...