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

相关文章

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现word文档内容智能提取以及合成

《Python实现word文档内容智能提取以及合成》这篇文章主要为大家详细介绍了如何使用Python实现从10个左右的docx文档中抽取内容,再调整语言风格后生成新的文档,感兴趣的小伙伴可以了解一下... 目录核心思路技术路径实现步骤阶段一:准备工作阶段二:内容提取 (python 脚本)阶段三:语言风格调

一文详解如何在Python中从字符串中提取部分内容

《一文详解如何在Python中从字符串中提取部分内容》:本文主要介绍如何在Python中从字符串中提取部分内容的相关资料,包括使用正则表达式、Pyparsing库、AST(抽象语法树)、字符串操作... 目录前言解决方案方法一:使用正则表达式方法二:使用 Pyparsing方法三:使用 AST方法四:使用字

openCV中KNN算法的实现

《openCV中KNN算法的实现》KNN算法是一种简单且常用的分类算法,本文主要介绍了openCV中KNN算法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录KNN算法流程使用OpenCV实现KNNOpenCV 是一个开源的跨平台计算机视觉库,它提供了各

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

用js控制视频播放进度基本示例代码

《用js控制视频播放进度基本示例代码》写前端的时候,很多的时候是需要支持要网页视频播放的功能,下面这篇文章主要给大家介绍了关于用js控制视频播放进度的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言html部分:JavaScript部分:注意:总结前言在javascript中控制视频播放

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.