OpenCV中最基础的就是对于各种图像的处理,今天就检测物体总结几个常用的方法。
(1)背景建模法
1. Single Gaussian (单高斯模型)
Real-time tracking of the human body
2. 混合高斯模型(Mixture of Gaussian Model)
An improved adaptive background mixture model for real-time tracking with shadow detection
3. 滑动高斯平均(Running Gaussian average)---Single Gaussian
Real-time tracking of the human body
4. 码本 (CodeBook)
Real-time foreground–background segmentation using codebook model
Real-time foreground-background segmentation using a modified codebook model
5. 自组织背景检测( SOBS-Self-organization background subtraction)
A self-Organizing approach to background subtraction for+visual surveillance
6. 样本一致性背景建模算法 (SACON)
A consensus-based method for tracking
A consensus-based method for tracking-Modelling background scenario and foreground appearance
SACON-Background subtraction based on a robust consensus method
7. VIBE算法
vibe
ViBe-A Universal Background Subtraction
8. 基于颜色信息的背景建模方法(Color)
A statistical approach for real-time robust background subtraction and shadow detection
9. 统计平均法
10. 中值滤波法( Temporal Median filter)
Automatic congestion detection system for underground platform
Detecting moving objects,ghost,and shadows in video streams
11. W4方法
W4.pdf
12. 本征背景法
A Bayesian computer vision system for modeling human interactions
13. 核密度估计方法
Non-parametric model for background subtraction
这里介绍常用的一种背景建模方法,大致的思路如下:
(1)打开视频文件
if( !(pCapture = cvCaptureFromFile("video.avi")) ){
fprintf(stderr, "Can not open video file video.avi\n");
return -2;
}
(2)逐帧读取视频,并且对第一帧进行内存申请,并初始化。
//逐帧读取视频
while(pFrame = cvQueryFrame( pCapture )){
nFrmNum++;
// 第一帧需要申请内存并初始化
if(nFrmNum == 1)
{
pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),IPL_DEPTH_8U,1);
pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),IPL_DEPTH_8U,1);
pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
// 转换为单通道图像再处理
cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
cvConvert(pFrImg, pFrMat);
cvConvert(pFrImg, pBkMat);
}else
{
cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
cvConvert(pFrImg, pFrameMat);
(3)高斯滤波平滑图像
cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
(4)当前帧与背景图相减
cvAbsDiff(pFrameMat, pBkMat, pFrMat);
(5)二值化背景,并进行更新
cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
// 将背景转换为图像格式,用于显示
cvConvert(pBkMat, pBkImg);
cvShowImage("video", pFrame);
cvShowImage("background", pBkImg);
cvShowImage("foreground", pFrImg);
if( cvWaitKey(2) >= 0 )
break;
(6)最终处理
cvDestroyWindow("video");
cvDestroyWindow("background");
cvDestroyWindow("foreground");
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg);
cvReleaseMat(&pFrameMat);
cvReleaseMat(&pFrMat);
cvReleaseMat(&pBkMat);