背景减除(1)--bgslibrary Windows编译和使用

入侵监控领域中,在固定场景下,需要检测和监控的入侵物体种类繁多,无法具体穷尽。传统的CV算法提取的特征应用场景有限,无法完成大量物体的监控;深度学习目标检测方法没法收集到无穷无尽的物体种类,因此监督效果仅限于编著物体,且适应性因场景变化而变化;异常检测方案,因为场景随着天气、光照的变化,无法行之有效的判断出异常物体的位置,并有时甚至出现误判断。

背景减除算法,通过学习固定场景下不变的背景,利用当前帧的的数据与背景的差分,可以很容易得到前景,在无需分类的场景下得到广泛应用。

这里推荐一个宝藏github主的分享,对于学习背景建模相关的东西很有帮助。https://github.com/murari023/awesome-background-subtraction/blob/master/README.md#projects

GitHub - andrewssobral/bgslibrary: A C++ Background Subtraction Library with wrappers for Python, MATLAB, Java and GUI on QT

一、bgslibrary库的下载

使用背景建模基本都会使用到两个库,一个是opencv里面关于background subtraction相关的库,另一个就是bgslibrary,其链接地址如下: 

https://github.com/andrewssobral/bgslibrary 

该库是由Andrews Sobral 于2012年开始编写的,主要使用C++语言,结合opencv进行编写完成,当前还适配python、java、matlab等语言。最新release的算法版本总计实现了43个算法,针对不同版本的opencv,所能适配的和使用的背景建模算法均不一致,下图为部分示例。

 可以通过上述github链接直接下载bgslibrary,然后解压到自己的盘符中存放

二、bgslibrary库的编译

编译的具体步骤可以参考如下链接:https://github.com/andrewssobral/bgslibrary/wiki/Installation-instructions---Windows

首先打开cmd,确认电脑已经安装cmake相关的软件。

解压已经下载到本地的bgslibrary库,然后cd进入bgslibrary

 进入C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build,并点击运行vcvars64.bat文件,需要管理员权限才能正常运行

 设置opencv库的安装目录:

切换到编译目录bgslibrary/build下;

调用cmake命令进行安装编译,编译命令如下所示:

cmake -DOpenCV_DIR="D:\personal\personal\opencv\build" -G "Visual Studio 16 2019" ..

配置成功出现如下所示显示结果。

最后使用visual studio 2019 打开稀土中的sln文件,选择你需要的编译库的类型(例如X64+debug或者X64+release),直接点击build编译即可。编译完成后可以在build文件夹中生成bgslibrary_core.lib以及bgslibrary_core.dll文件。

三、bgslibrary库的使用

首先,新建一个工程,取一个工程名称,将bgslibrary库下面的src文件夹拷贝到本工程目录下,将编译生成的lib以及dll拷贝到本工程下的lib文件以及工程下,具体如下图所示。

接着,在包含目录中配置opencv和bgs库的头文件目录,库目录中包含opencv和bgs库的库目录所在位置,如下所示:

在链接器中增加opencv_world470d.lib以及bgslibrary_core.lib

新建一个main.cpp,加入如下代码:

 

#include <iostream>
#include <algorithm>
#include <iterator>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include"../src/algorithms/algorithms.h"


auto algorithmsName = BGS_Factory::Instance()->GetRegisteredAlgorithmsName();

int main() {
    std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;

    std::cout << "Number of available algorithms: " << algorithmsName.size() << std::endl;
    std::cout << "List of available algorithms:" << std::endl;
    std::copy(algorithmsName.begin(), algorithmsName.end(), std::ostream_iterator<std::string>(std::cout, "\n"));

    /*
     List of all algorithms:
     (Note that some of these algorithms are available only for a specific version of OpenCV, see algorithms.h)
     AdaptiveBackgroundLearning,AdaptiveSelectiveBackgroundLearning,CodeBook,DPAdaptiveMedian,DPEigenbackground,
     DPGrimsonGMM,DPMean,DPPratiMediod,DPTexture,DPWrenGA,DPZivkovicAGMM,FrameDifference,FuzzyChoquetIntegral,
     FuzzySugenoIntegral,GMG,IndependentMultimodal,KDE,KNN,LBAdaptiveSOM,LBFuzzyAdaptiveSOM,LBFuzzyGaussian,
     LBMixtureOfGaussians,LBP_MRF,LBSimpleGaussian,LOBSTER,MixtureOfGaussianV2,MixtureOfGaussianV1,MultiCue,
     MultiLayer,PAWCS,PixelBasedAdaptiveSegmenter,SigmaDelta,StaticFrameDifference,SuBSENSE,T2FGMM_UM,T2FGMM_UV,
     T2FMRF_UM,T2FMRF_UV,TwoPoints,ViBe,VuMeter,WeightedMovingMean,WeightedMovingVariance
    */
    std::string algorithmName = "KNN";
    //int cameraIndex = 0;
    //if (argc > 1) algorithmName = argv[1];
    //if (argc > 2) cameraIndex = std::stoi(argv[2]);
    std::string video_path = "./00010000684000000_4.mp4";
    cv::VideoCapture capture;
    capture.open(video_path);

    if (!capture.isOpened()) {
        std::cerr << "Cannot initialize web camera!" << std::endl;
        return -1;
    }

    std::cout << "Running " << algorithmName << std::endl;
    auto bgs = BGS_Factory::Instance()->Create(algorithmName);

    cv::Mat img_input;
    auto key = 0;
    std::cout << "Press 's' to stop:" << std::endl;
    while (key != 's') {
        // Capture frame-by-frame
        capture >> img_input;

        if (img_input.empty()) break;

        // Resize input frame for better visualization
        cv::resize(img_input, img_input, cv::Size(380, 240), 0, 0, CV_INTER_LINEAR);
        cv::imshow("input", img_input);

        cv::Mat img_mask;
        cv::Mat img_bkgmodel;
        try {
            // by default, bgs->process(...) shows automatically the foreground mask image
            // or you can disable it by: bgs->setShowOutput(false);
            bgs->process(img_input, img_mask, img_bkgmodel);

            if(!img_mask.empty())
              cv::imshow("Foreground", img_mask);
            //  ....do something else...
        }
        catch (std::exception& e) {
            std::cout << "Exception occurred" << std::endl;
            std::cout << e.what() << std::endl;
        }

        key = cv::waitKey(33);
    }

    cv::destroyAllWindows();
    capture.release();

    return 0;

}

运行本程序,可以看到结果如下所示:

---------------------------------------------------END----------------------------------------------------- 

相关推荐

  1. oj 1.8编程基础之多维数组 07:矩阵归零消序列

    2024-03-26 00:26:09       58 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 00:26:09       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 00:26:09       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 00:26:09       87 阅读
  4. Python语言-面向对象

    2024-03-26 00:26:09       96 阅读

热门阅读

  1. 洛谷 1443.马的遍历

    2024-03-26 00:26:09       40 阅读
  2. js一些底层

    2024-03-26 00:26:09       45 阅读
  3. 软件设计原则

    2024-03-26 00:26:09       47 阅读
  4. 基于单片机的洗衣机自动化控制电路设计与仿真

    2024-03-26 00:26:09       44 阅读
  5. N诺刷刷题

    2024-03-26 00:26:09       42 阅读
  6. 数据结构之栈

    2024-03-26 00:26:09       49 阅读
  7. ABAP中的内表(看这一篇就够了)

    2024-03-26 00:26:09       39 阅读
  8. HarmonyOS系统开发ArkTS常用组件编程技巧

    2024-03-26 00:26:09       36 阅读
  9. KMP算法

    KMP算法

    2024-03-26 00:26:09      63 阅读