1、概述

  案例:使用MOG和KNN实现视频背景消除建模,使用OpenCV中的createBackgroundSubtractorMOG()和createBackgroundSubtractorKNN()来实现

  1.createBackgroundSubtractorMOG()参数介绍:

Ptr<BackgroundSubtractorMOG2>
    createBackgroundSubtractorMOG2(int history=500, double varThreshold=16,
                                   bool detectShadows=true);
history:用于训练背景的帧数,默认帧数为500帧,如果不动手设置learingRate,history就被用于计算当前的learningRate, 此时history越大,learningRate越小,背景更新越慢
varThreshold:方差阈值,用于判断当前像素是前景还是背景。一般默认为16,如果光照变化明显,如阳光下的水面,建议设为25,值越大灵敏度越低。
deteShadows:是否检测影子,设为true为检测,false为不检测,检测影子会增加程序时间复杂度,一般设置为false


BackgroundSubstractorMOG2->apply(src, fmask, learningRate);
src:源图像
fmask:差分图像
learningRate:学习速率,值为0-1,为0时背景不更新,为1时逐帧更新,默认为-1,即算法自动更新;

2.createBackgroundSubtractorKNN()参数介绍:

createBackgroundSubtractorKNN(int history=500, double dist2Threshold=400.0,
                                   bool detectShadows=true);
history:用于训练背景的帧数,默认帧数为500帧,如果不动手设置learingRate,history就被用于计算当前的learningRate, 此时history越大,learningRate越小,背景更新越慢
dist2Threshold:方差阈值,用于判断当前像素是前景还是背景。一般默认为400,值越大灵敏度越低。
detectShadows:是否检测影子,设为true为检测,false为不检测,检测影子会增加程序时间复杂度,一般设置为false
 BackgroundSubstractorMOG2->apply(src, fmask, learningRate);
 src:源图像
 fmask:差分图像
 learningRate:学习速率,值为0-1,为0时背景不更新,为1时逐帧更新,默认为-1,即算法自动更新;

  实现此算法的步骤:

  1.创建VideoCapture

  2.使用open方法打开视频

  3.创建creteBackgroundSubtractorKNN()/createBackgrondSubtractorMOG()

  4.while循环读取视频数据

  5.在循环体内执行apply方法生成差分

  6.显示差分图像

  7.完成

2、代码示例

1.MOG算法实现差分:

 VideoCapture capture;
    capture.open(filePath);
    if(!capture.isOpened()){
        qDebug()<<"无法打开视频文件";
        return;
    }
    Mat frame;
    Mat bsMaskMOG;
    Ptr<BackgroundSubtractor> mogSub = createBackgroundSubtractorMOG2(100,25,false);
    Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));
    while(capture.read(frame)){
        mogSub->apply(frame,bsMaskMOG);
        morphologyEx(bsMaskMOG,bsMaskMOG,MORPH_OPEN,kernel);//使用形态学操作消除白点
        imshow("mog",bsMaskMOG);
        waitKey(100);
    }
    capture.release();

2.KNN算法实现差分:

VideoCapture capture;
    capture.open(filePath);
    if(!capture.isOpened()){
        qDebug()<<"无法打开视频文件";
        return;
    }
    Mat frame;
    Mat knnMask;
    Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));
    Ptr<BackgroundSubtractor> knnSub = createBackgroundSubtractorKNN();
    while(capture.read(frame)){
        knnSub->apply(frame,knnMask);
        morphologyEx(knnMask,knnMask,MORPH_OPEN,kernel);
        imshow("knn",knnMask);
        waitKey(100);
    }
    capture.release();

3、演示图片

ps:图片如何侵权请联系我,我立马删除

本文福利,费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击费领取↓↓

GitHub 加速计划 / opencv31 / opencv
156
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:4 个月前 )
4d26e16a Speed up and reduce memory consumption for findContours 2 天前
1db98278 Add jxl (JPEG XL) codec support #26379 ### Pull Request Readiness Checklist Related CI and Docker changes: - https://github.com/opencv/ci-gha-workflow/pull/190 - https://github.com/opencv-infrastructure/opencv-gha-dockerfile/pull/44 See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work https://github.com/opencv/opencv/issues/20178 - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake 2 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐