vibe的matlab实现,Matlab调用VIBE算法
Matlab调用VIBE算法
Matlab调用VIBE算法
从参考[1]中下载源码,VIBE的算法实现由vibe-background-sequential.c和vibe-background-sequential.h构成,为了mex编译方便将vibe-background-sequential.c文件的后缀改为cpp。
mex的接口文件为VIBE_.cpp:
// VIBE_.cpp
// ------------------------------------------------------------------ -
// Reference: your-main-file-sequential.c and mexopencv-master/BackgroundSubtractorMOG2_.cpp
// Authors : LSS
// Date : 26 / 04 / 2017
// Last modified : 28 / 04 / 2017
// ------------------------------------------------------------------ -
#include "mex.h"
#include "matrix.h" // for the mxArray
#include
#include
#include
#include
#include "vibe-background-sequential.h"
#include "vibe-background-sequential.cpp"
using namespace std;
class VIBE
{
private:
vibeModel_Sequential_t *model;
bool bfirstFrame;
public:
VIBE()
{
model = (vibeModel_Sequential_t*)libvibeModel_Sequential_New();
bfirstFrame = true;
};
VIBE(const VIBE & vibe)
{
model = (vibeModel_Sequential_t*)libvibeModel_Sequential_New();
/* Default parameters values. */
model->numberOfSamples = vibe.model->numberOfSamples;
model->matchingThreshold = vibe.model->matchingThreshold;
model->matchingNumber = vibe.model->matchingNumber;
model->updateFactor = vibe.model->updateFactor;
/* Storage for the history. */
model->historyImage = vibe.model->historyImage;
model->historyBuffer = vibe.model->historyBuffer;
model->lastHistoryImageSwapped = vibe.model->lastHistoryImageSwapped;
/* Buffers with random values. */
model->jump = vibe.model->jump;
model->neighbor = vibe.model->neighbor;
model->position = vibe.model->position;
bfirstFrame = true;
};
~VIBE()
{
libvibeModel_Sequential_Free(model);
};
VIBE & operator=(const VIBE & vibe)
{
if (this == &vibe)
return *this;
/* Default parameters values. */
model->numberOfSamples = vibe.model->numberOfSamples;
model->matchingThreshold = vibe.model->matchingThreshold;
model->matchingNumber = vibe.model->matchingNumber;
model->updateFactor = vibe.model->updateFactor;
/* Storage for the history. */
model->historyImage = vibe.model->historyImage;
model->historyBuffer = vibe.model->historyBuffer;
model->lastHistoryImageSwapped = vibe.model->lastHistoryImageSwapped;
/* Buffers with random values. */
model->jump = vibe.model->jump;
model->neighbor = vibe.model->neighbor;
model->position = vibe.model->position;
bfirstFrame = true;
return *this;
};
void GetfrImageC3R(uint8_t * image_data, uint8_t * frimg, int width, int height) // image_data is stored as RGBRGBRGB... or BGRBGRBGR... Three consecutives bytes per pixel thus.
{
if (bfirstFrame)
{
libvibeModel_Sequential_AllocInit_8u_C3R(model, image_data, width, height);
bfirstFrame = false;
}
else
{
libvibeModel_Sequential_Segmentation_8u_C3R(model, image_data, frimg);
libvibeModel_Sequential_Update_8u_C3R(model, image_data, frimg);
}
}
};
/// Last object id to allocate
int last_id = 0;
/// Object container
map obj_;
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
// Checking number of arguments
if (nrhs<2 || nlhs>1)
mexErrMsgIdAndTxt("VIBE : error", "Wrong number of arguments");
int id = 0;
string method;
if (nrhs > 1 && mxIsNumeric(prhs[0]) && mxIsChar(prhs[1]))
{
id = mxGetScalar(prhs[0]);
method = mxArrayToString(prhs[1]);
}
else
mexErrMsgIdAndTxt("VIBE : error", "Invalid arguments");
if (method == "new")
{
obj_[last_id] = VIBE(); // 调用了两次默认构造函数,和一次赋值函数
plhs[0] = mxCreateDoubleScalar(last_id++);
return;
}
VIBE& obj = obj_[id];
if (method == "delete")
{
if (nrhs != 2 || nlhs != 0)
mexErrMsgIdAndTxt("VIBE : error", "Output not assigned");
obj_.erase(id);
}
else if (method == "GetfrImageC3R")
{
if (nrhs != 3 || nlhs != 1)
mexErrMsgIdAndTxt("VIBE : error", "Wrong number of arguments");
if (!mxIsClass(prhs[2], "uint8"))
mexErrMsgIdAndTxt("VIBE : error", "Only image arrays of the uint8 class are allowed");
uint8_t* imMatlab = (uint8_t*)mxGetPr(prhs[2]);
int nrDims = (int)mxGetNumberOfDimensions(prhs[2]);
if (nrDims != 3)
{
mexErrMsgIdAndTxt("VIBE : error", "The input image should be RGB");
return;
}
int width = (mxGetDimensions(prhs[2]))[0];
int height = (mxGetDimensions(prhs[2]))[1];
uint8_t *image_data = NULL;
image_data = (uint8_t*)mxMalloc((3 * width) * height);
for (int i = 0; i < width; i++) // image_data is stored should as RGBRGBRGB
{
for (int j = 0; j < height; j++)
{
image_data[j * 3 * width + i*3] = imMatlab[j*width + i];
image_data[j * 3 * width + i*3 + 1] = imMatlab[width*height + j*width + i];
image_data[j * 3 * width + i*3 + 2] = imMatlab[2*width*height + j*width + i];
}
}
plhs[0] = mxCreateNumericMatrix(width, height, mxUINT8_CLASS, mxREAL);
uint8_t * frimg = (uint8_t *) mxGetPr(plhs[0]);
memset(frimg, 0, sizeof(uint8_t)*width*height);
obj.GetfrImageC3R(image_data, frimg, width, height);
mxFree(image_data);
}else
mexErrMsgIdAndTxt("VIBE : error", "Unrecognized operation");
return;
}
并在Matlab环境下定义VIBE类(VIBE.m):
classdef VIBE < handle
% VIBE.m
% -------------------------------------------------------------------
% Reference: BackgroundSubtractorMOG2.m
% Authors: LSS
% Date: 27/04/2017
% Last modified: 28/04/2017
% -------------------------------------------------------------------
properties (SetAccess = private)
id % Object ID
end
methods
function this = VIBE()
this.id = VIBE_(0, 'new');
end
function delete(this)
%DELETE Destructor
%
VIBE_(this.id, 'delete');
end
function fgmask = GetfrImageC3R(this, im)
%
fgmask = VIBE_(this.id, 'GetfrImageC3R', im);
end
end
end
mex的编译代码为:
cmd = ['mex -largeArrayDims VIBE_.cpp'];
eval(cmd)
调用VIBE的示例代码为:
% demo_camera.m
% -------------------------------------------------------------------
% Reference:
% Authors: LSS
% Date: 27/04/2017
% Last modified: 28/04/2017
% -------------------------------------------------------------------
videoObj = VideoReader('E:\code\vcproject\FireFogVS2013-2017-24-camera\FireFogVS2013\out3.avi');
numFrames = get(videoObj, 'NumberOfFrames');
FRAME_HEIGHT = 480;
FRAME_WIDTH = 640;
vibe = VIBE();
for ii = 1:numFrames-1,
im = read(videoObj, ii);
im = double(imresize(im, [FRAME_HEIGHT, FRAME_WIDTH]));
frimg = vibe.GetfrImageC3R(uint8(im));
figure(50), imshow(frimg)
end
实验结果图像为:
上面的所有代码可以在链接[2]中下载。
参考:
【1】http://www.telecom.ulg.ac.be/research/vibe/doc/
【2】VIBE-MEX-- http://download.csdn.net/detail/lsxpu/9828310
Matlab调用VIBE算法相关教程
vibe的matlab实现,Matlab调用VIBE算法相关推荐
- 2021-01-28 粒子群优化算法-Python版本和Matlab函数 particleswarm 调用
粒子群优化算法-Python版本和Matlab函数 particleswarm 调用 前两天分享了粒子群优化算法的原理和Matlab原理实现,本文分享一下Python代码下的PSO实现以及Matlab ...
- 【matlab】matlab算法封装成工具包提供给程序调用
说明: 1.非进程通讯协议,无需在电脑上安装完整版的matlab开发环境. 2.本项目以C#为案例,调用的语言不限,操作流程基本相同. 一.准备工作 1.安装MATLABWebAppServerSet ...
- vb调用matlab工具箱,Matlab与VB集成 - 关于VB的经验之谈 - VB爱好者乐园(VBGood) - 关于VB的经验,电子教程,代码,控件,论坛,博客,微博等....
工业生产中经常遇到复杂的数据信息处理问题,需要大运算量的矩阵计算及对分析结果进行实时.直观的图形化显示.Matlab是集数值分析.矩阵运算.信号处理和图形显示于一体的高性能数学软件,将其强大的计算功能 ...
- matlab 压缩感知矩阵_【精读】基于MATLAB的钢筋下料优化算法
基于MATLAB的钢筋下料优化算法 摘要:运用MATLAB软件求解实际工程中一维钢筋下料优化的问题,提出了首先列举出单根原料分割的所有可行解,其次采用线性规划的方法求出理想条件下最优方案,最后通过整数 ...
- 【数字图像处理matlab】(HSI变换融合算法)
[数字图像处理matlab](HSI变换融合算法) 输入一张高分辨率的全色影像HR,一张低分辨率的多光谱影像MS,采用HSI变换融合算法实现影像融合,其中RGB与HSI影像的相互转换调用自定义函数RG ...
- matlab adaptfilt.rls,基于RLS算法的多麦克风降噪
<信息处理课群综合训练与设计> 课程设计任务书 学生姓名: 专业班级: 指导教师: 工作单位: 题 目: 基于RLS算法的多麦克风降噪 设计任务: 给定主麦克风录制的受噪声污染的语音信号和 ...
- 基于matlab GUI Powell+蚁群算法图像配准
基于matlab GUI Powell+蚁群算法图像配准 一.简介 1 蚁群算法(ant colony algorithm,ACA)起源和发展历程 Marco Dorigo等人在研究新型算法的过程中, ...
- MATLAB 中的机械臂算法——运动学
MATLAB 中的机械臂算法--运动学 机械臂算法 MATLAB 在 2016 年就推出了 Robotics System Toolbox(RST),其中有很多关于机械臂方面的算法.而且随着客户需求的 ...
- MATLAB基于LSB的数字水印算法
摘要-LSB是一种简单传统的信息隐藏算法,属于数字水印技术中的一种.本文首先介绍了LSB技术的原理和特点,然后讨论了基于LSB的数字水印算法.最后利用MATLAB 2010 b2对这一算法的加密过程进 ...
- 基于MATLAB有噪声语音信号处理算法设计
获取项目源文件,联系Q:1415736481,可指导毕设,课设 摘要 滤波器设计在数字信号处理中占有极其重要的地位,FIR数字滤波器和IIR滤波器是滤波器设计的重要组成部分.利用MATLAB信号处理工 ...
最新文章
- linux的sort如何对时间排序,Linux中用Sort和Tsort对文件进行排序
- YTU 2586: 填空题B-字画鉴别
- MySQL中的自适应哈希索引
- SDL2:封装媒体显示播放Csdl2
- linux 基础学习之常用命令
- ocp最新题库之052新题带答案整理-36题
- java 军工_为什么军工行业不用java而是选择继续用c(对于业务系统Java是非常合适的而不带操作系统的板子甚至可以做到微秒级别的实时控制)...
- 响应activex事件
- 理解 Zend 框架 第 1 部分: 基础
- 使用idea搭建Maven+SSM(Spring+SpringMVC+Mybatis)框架(一、使用Maven创建新工程)
- UnityWebPlayer打开文件
- mac下webstorm 汉化解决方案
- CR网络和主网络的认知无线电切换算法
- php全套之七,php程序员工具箱
- 540s inter 固件_Intel SSD Firmware Update Tool(英特尔ssd固件更新工具)下载 v2.1.6官方版...
- @Autowired与@Resource区别
- jenkins 命令执行 (CVE-2018-1000861)复现
- LR推导及其与SVM的区别
- android studio marvin 配置
- 一头扎进SpringBoot视频教程(附源码与文档)