设为首页 加入收藏

TOP

OpenCV_基于自适应背景更新的运动目标检测
2014-11-24 08:32:15 来源: 作者: 【 】 浏览:0
Tags:OpenCV_ 基于 适应 背景 更新 运动 目标 检测

下面这段代码利用OpenCV实现了最为简单的基于自适应背景更新的运动目标检测算法。


即:
运动前景提取——背景减除
foreground = |frame - background| > threshold
更新背景模型——滑动平均滤波
background = background + (frame - background) * alpha = background * (1 - alpha) + frame * alpha


//运动前景检测——基于自适应背景更新
//Author: http://blog.csdn.net/icvpr
#include
#include


#include


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"< 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_基于HOG特征的行人检测 下一篇OpenCV_局部图像特征的提取与匹配..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)
·关于 MySQL 数据库学 (2025-12-26 23:20:16)
·SOLVED: Ubuntu 24.0 (2025-12-26 22:51:53)
·Linux 常用命令最全 (2025-12-26 22:51:50)