bgslibrary视频前景提取算法之帧差法

2024-03-10 09:58

本文主要是介绍bgslibrary视频前景提取算法之帧差法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

BGSLibrary:A Background Subtraction Library
The BGSLibrary was developed by Andrews Sobral and provides an easy-to-use C++ framework based on OpenCV to perform background subtraction (BGS) in videos.
github介绍及下载地址 : https://github.com/andrewssobral/bgslibrary
现有30+种视频前景提取算法,不一定最优,但可以比较效果,准备研究其中部分。

第一次写,给出完整的头文件和main函数,以后仅给出要实现的算法
这次先实现 帧差法 (FrameDifferenceBGS) FrameDifference,总共4个文件
IBGS.h //IBGS是所有不同的视频前景提取算法的抽象类 ,我去掉其中saveConfig()和loadConfig()
FrameDifferenceBGS.h 帧差法
FrameDifferenceBGS.cpp
main.cpp //自己写的调用

文件名: IBGS.h

/*
This file is part of BGSLibrary.BGSLibrary is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.BGSLibrary is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.You should have received a copy of the GNU General Public License
along with BGSLibrary.  If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui_c.h>class IBGS
{
public:virtual void process(const cv::Mat &img_input, cv::Mat &img_foreground, cv::Mat &img_background) = 0;/*virtual void process(const cv::Mat &img_input, cv::Mat &img_foreground){process(img_input, img_foreground, cv::Mat());}*/virtual ~IBGS(){}private:/*virtual void saveConfig() = 0;virtual void loadConfig() = 0;*/
};

参考demo后自己写的main函数
main.cpp

#include <iostream>
#include "FrameDifferenceBGS.h"
#include "IBGS.h"
using namespace cv;
using namespace std;#define resizedHeight       480     
#define resizedWidth        600     
#define VIDEOFILE    "1.mp4"
#define frameTostart 20      //设置开始帧string inputPath = "E:\\2paperCode\\testVideo\\Crossroad\\229\\";
int main(int argc, char* argv[])
{VideoCapture capture(inputPath + VIDEOFILE);if (!capture.isOpened()){cerr << "No video input\n" << endl;return -1;}   IBGS *bgsFDiff;bgsFDiff = new FrameDifferenceBGS();//使用帧差法int pause = 0;Mat img_input;Mat img_input_resized(resizedHeight, resizedWidth, CV_8UC3);capture.set(CAP_PROP_POS_FRAMES, frameTostart); FrameDifferenceBGS fdiff;while (!pause){capture >> img_input;if (img_input.empty())break;resize(img_input, img_input_resized, img_input_resized.size());namedWindow("input", WINDOW_NORMAL);imshow("input", img_input_resized);Mat img_mask;Mat img_bkgmodel;bgsFDiff->process(img_input, img_mask, img_bkgmodel); // by default, it shows automatically the foreground mask imageif (cvWaitKey(10) == 'q')pause = !pause;}delete bgsFDiff;cvDestroyAllWindows();capture.release();return 0;
}

帧差法头文件,从 IBGS继承而来
FrameDifferenceBGS.h

#pragma once#include <iostream>
#include <opencv2/opencv.hpp>#include "IBGS.h"class FrameDifferenceBGS : public IBGS
{
private:bool firstTime;cv::Mat img_input_prev;cv::Mat img_foreground;bool enableThreshold;int threshold;bool showOutput;public:FrameDifferenceBGS();~FrameDifferenceBGS();void process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel);//private:
//  void saveConfig();
//  void loadConfig();
};

FrameDifferenceBGS.cpp

#include "FrameDifferenceBGS.h"FrameDifferenceBGS::FrameDifferenceBGS() : firstTime(true), enableThreshold(true), threshold(15), showOutput(true)
{std::cout << "FrameDifferenceBGS()" << std::endl;
}FrameDifferenceBGS::~FrameDifferenceBGS()
{std::cout << "~FrameDifferenceBGS()" << std::endl;
}void FrameDifferenceBGS::process(const cv::Mat &img_input, cv::Mat &img_output, cv::Mat &img_bgmodel)
{if (img_input.empty())return;enableThreshold = true;threshold = 15;showOutput = true;if (img_input_prev.empty()){img_input.copyTo(img_input_prev);//前一帧为空时,将当前帧复制给前一帧return;}cv::absdiff(img_input_prev, img_input, img_foreground);if (img_foreground.channels() == 3)cv::cvtColor(img_foreground, img_foreground, CV_BGR2GRAY);if (enableThreshold)cv::threshold(img_foreground, img_foreground, threshold, 255, cv::THRESH_BINARY);if (showOutput){namedWindow("Frame Difference", cv::WINDOW_NORMAL);cv::imshow("Frame Difference", img_foreground);}img_foreground.copyTo(img_output);img_input.copyTo(img_input_prev);firstTime = false;
}

PS:尽量使用 OpenCV 内置函数. 调用LUT 函数可以获得最快的速度. 这是因为OpenCV库可以通过英特尔线程架构启用多线程,下面的opencv矩阵操作均是优化的多线程并行处理,较高效
具体参考:快速对图像的像素进行操作 opencv 实战

这篇关于bgslibrary视频前景提取算法之帧差法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/793875

相关文章

Java如何获取视频文件的视频时长

《Java如何获取视频文件的视频时长》文章介绍了如何使用Java获取视频文件的视频时长,包括导入maven依赖和代码案例,同时,也讨论了在运行过程中遇到的SLF4J加载问题,并给出了解决方案... 目录Java获取视频文件的视频时长1、导入maven依赖2、代码案例3、SLF4J: Failed to lo

Python实现多路视频多窗口播放功能

《Python实现多路视频多窗口播放功能》这篇文章主要为大家详细介绍了Python实现多路视频多窗口播放功能的相关知识,文中的示例代码讲解详细,有需要的小伙伴可以跟随小编一起学习一下... 目录一、python实现多路视频播放功能二、代码实现三、打包代码实现总结一、python实现多路视频播放功能服务端开

Python实现视频转换为音频的方法详解

《Python实现视频转换为音频的方法详解》这篇文章主要为大家详细Python如何将视频转换为音频并将音频文件保存到特定文件夹下,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果5. 注意事项

使用Python在Excel中插入、修改、提取和删除超链接

《使用Python在Excel中插入、修改、提取和删除超链接》超链接是Excel中的常用功能,通过点击超链接可以快速跳转到外部网站、本地文件或工作表中的特定单元格,有效提升数据访问的效率和用户体验,这... 目录引言使用工具python在Excel中插入超链接Python修改Excel中的超链接Python

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

Java后端接口中提取请求头中的Cookie和Token的方法

《Java后端接口中提取请求头中的Cookie和Token的方法》在现代Web开发中,HTTP请求头(Header)是客户端与服务器之间传递信息的重要方式之一,本文将详细介绍如何在Java后端(以Sp... 目录引言1. 背景1.1 什么是 HTTP 请求头?1.2 为什么需要提取请求头?2. 使用 Spr

Python视频处理库VidGear使用小结

《Python视频处理库VidGear使用小结》VidGear是一个高性能的Python视频处理库,本文主要介绍了Python视频处理库VidGear使用小结,文中通过示例代码介绍的非常详细,对大家的... 目录一、VidGear的安装二、VidGear的主要功能三、VidGear的使用示例四、VidGea

Python中的随机森林算法与实战

《Python中的随机森林算法与实战》本文详细介绍了随机森林算法,包括其原理、实现步骤、分类和回归案例,并讨论了其优点和缺点,通过面向对象编程实现了一个简单的随机森林模型,并应用于鸢尾花分类和波士顿房... 目录1、随机森林算法概述2、随机森林的原理3、实现步骤4、分类案例:使用随机森林预测鸢尾花品种4.1