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

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

相关文章

详解Tensorflow数据读取有三种方式(next_batch)

详解Tensorflow数据读取有三种方式(next_batch)

Tensorflow数据读取有三种方式: Preloaded data: 预加载数据 Feeding: Python产生数据,再把数据喂给后端。 Reading from...

利用python求积分的实例

python的numpy库集成了很多的函数。利用其中的函数可以很方便的解决一些数学问题。本篇介绍如何使用python的numpy来求解积分。 代码如下: # -*- coding:...

在python image 中安装中文字体的实现方法

如果一些应用需要到中文字体(如果pygraphviz,不安装中文字体,中文会显示乱码),就要在image 中安装中文字体。 默认 python image 是不包含中文字体的: ma...

详解Golang 与python中的字符串反转

详解Golang 与python中的字符串反转 在go中,需要用rune来处理,因为涉及到中文或者一些字符ASCII编码大于255的。 func main() { fmt.Pr...

django框架面向对象ORM模型继承用法实例分析

本文实例讲述了django框架面向对象ORM模型继承用法。分享给大家供大家参考,具体如下: Django ORM对模型继承的支持,将python面向对象的编程方法与数据库面向关系表的数据...