OpenCV_基于自适应背景更新的运动目标检测
2014-07-21 01:01
931 查看
转自/article/2271962.html
下面这段代码利用OpenCV实现了最为简单的基于自适应背景更新的运动目标检测算法。
即:
运动前景提取——背景减除
foreground = |frame - background| > threshold
更新背景模型——滑动平均滤波
background = background + (frame - background) * alpha = background * (1 - alpha) + frame * alpha
[cpp] view
plaincopy
//运动前景检测——基于自适应背景更新
//Author: www.icvpr.com
//Blog: http://blog.csdn.net/icvpr
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
std::string videoFileName = "../test.avi";
int threshold = 25 ; // 二值化阈值
float alpha = 0.01; // 更新速度 [0, 1]
cv::VideoCapture capture;
capture.open(videoFileName);
if (!capture.isOpened())
{
std::cout<<"cannot open video"<<std::endl;
return -1;
}
cv::Mat foregroundImg;
cv::Mat foregroundMat;
cv::Mat backgroundImg;
cv::Mat backgroundMat;
cv::Mat frame;
cv::Mat grayImg;
cv::Mat grayMat;
while (capture.read(frame))
{
cv::cvtColor(frame, grayImg, CV_BGR2GRAY);
grayImg.convertTo(grayMat, CV_32FC1);
if (backgroundMat.empty())
{
grayImg.copyTo(backgroundImg);
grayImg.convertTo(backgroundMat, CV_32FC1);
}
// 背景减除
cv::absdiff(grayMat, backgroundMat, foregroundMat);
// 自适应背景更新
cv::addWeighted(backgroundMat, alpha, foregroundMat, 1-alpha, 0, backgroundMat);
// 二值化,获取前景像素点
cv::threshold(foregroundMat, foregroundMat, threshold, 255, CV_THRESH_BINARY);
// 为了显示用,将CV_32FC1转换为CV_8U
cv::convertScaleAbs(foregroundMat, foregroundImg);
cv::convertScaleAbs(backgroundMat, backgroundImg);
cv::imshow("frame", frame);
cv::imshow("foreground", foregroundImg);
cv::imshow("background", backgroundImg);
if (cv::waitKey(25) > 0)
{
break;
}
}
return 0;
}
下面这段代码利用OpenCV实现了最为简单的基于自适应背景更新的运动目标检测算法。
即:
运动前景提取——背景减除
foreground = |frame - background| > threshold
更新背景模型——滑动平均滤波
background = background + (frame - background) * alpha = background * (1 - alpha) + frame * alpha
[cpp] view
plaincopy
//运动前景检测——基于自适应背景更新
//Author: www.icvpr.com
//Blog: http://blog.csdn.net/icvpr
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
std::string videoFileName = "../test.avi";
int threshold = 25 ; // 二值化阈值
float alpha = 0.01; // 更新速度 [0, 1]
cv::VideoCapture capture;
capture.open(videoFileName);
if (!capture.isOpened())
{
std::cout<<"cannot open video"<<std::endl;
return -1;
}
cv::Mat foregroundImg;
cv::Mat foregroundMat;
cv::Mat backgroundImg;
cv::Mat backgroundMat;
cv::Mat frame;
cv::Mat grayImg;
cv::Mat grayMat;
while (capture.read(frame))
{
cv::cvtColor(frame, grayImg, CV_BGR2GRAY);
grayImg.convertTo(grayMat, CV_32FC1);
if (backgroundMat.empty())
{
grayImg.copyTo(backgroundImg);
grayImg.convertTo(backgroundMat, CV_32FC1);
}
// 背景减除
cv::absdiff(grayMat, backgroundMat, foregroundMat);
// 自适应背景更新
cv::addWeighted(backgroundMat, alpha, foregroundMat, 1-alpha, 0, backgroundMat);
// 二值化,获取前景像素点
cv::threshold(foregroundMat, foregroundMat, threshold, 255, CV_THRESH_BINARY);
// 为了显示用,将CV_32FC1转换为CV_8U
cv::convertScaleAbs(foregroundMat, foregroundImg);
cv::convertScaleAbs(backgroundMat, backgroundImg);
cv::imshow("frame", frame);
cv::imshow("foreground", foregroundImg);
cv::imshow("background", backgroundImg);
if (cv::waitKey(25) > 0)
{
break;
}
}
return 0;
}
相关文章推荐
- OpenCV_基于自适应背景更新的运动目标检测
- 【OPENCV】基于背景差法的运动目标检测
- 基于opencV的动态背景下运动目标检测及跟踪(修改版)
- 【opencv】动态背景下运动目标检测 FAST+SURF+FLANN配准差分 17/12/13更新图片
- OpenCV实现静止背景下运动目标的检测
- OpenCV_基于混合高斯模型GMM的运动目标检测
- 基于样本一致性的背景减除运动目标检测算法(SACON)
- OpenCV_基于混合高斯模型GMM的运动目标检测
- 基于codebook背景建模的运动目标检测
- 【计算机视觉】基于自组织背景减除的运动目标检测算法
- 【opencv】动态背景下运动目标检测 SURF配准差分
- OpenCV之基于GMM的运动目标检测
- OpenCV_基于混合高斯模型GMM的运动目标检测
- 【计算机视觉】基于样本一致性的背景减除运动目标检测算法(SACON)
- 运动物体目标检测实现—基于OpenCV
- opencv程序十七:运动目标检测之背景减除法
- OpenCV_基于混合高斯模型GMM的运动目标检测
- OpenCV实现静止背景下运动目标的检测
- 基于像素自适应分割的运动目标检测算法
- OpenCV实现静止背景下运动目标的检测