速度很慢 相比较比基于像素值稳定提取背景,效果好一些
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include "cxcore.h"
#include "cvaux.h"
#include <time.h>
#pragma comment(lib,"cv200d.lib")
#pragma comment(lib,"cvaux200d.lib")
#pragma comment(lib,"cxcore200d.lib")
#pragma comment(lib,"highgui200d.lib")
const double MHI_DURATION = 1;//0.5s为运动跟踪的最大持续时间
const double MAX_TIME_DELTA = 0.5;
const double MIN_TIME_DELTA = 0.05;
const int N = 3;
//
const int CONTOUR_MAX_AERA = 400;
// ring image buffer 圈出图像缓冲
IplImage **buf = 0;//指针的指针
int last = 0;
// temporary images临时图像
IplImage *mhi = 0; // MHI: motion history image
CvFilter filter = CV_GAUSSIAN_5x5;
CvConnectedComp *cur_comp, min_comp;
CvConnectedComp comp;
CvMemStorage *storage;
CvPoint pt[4];
// 参数:
// img – 输入视频帧
// dst – 检测结果
void update_mhi( IplImage* img, IplImage* dst, int diff_threshold )
{
double timestamp = clock()/100.; // get current time in seconds 时间戳
CvSize size = cvSize(img->width,img->height);
// get current frame size,得到当前帧的尺寸
int i, idx1, idx2;
IplImage* silh;
IplImage* pyr = cvCreateImage( cvSize((size.width & -2)/2, (size.height & -2)/2), 8, 1 );
CvMemStorage *stor;
CvSeq *cont;
if( !mhi || mhi->width != size.width || mhi->height != size.height )
{
if( buf == 0 ) //若尚没有初始化则分配内存给他
{
buf = (IplImage**)malloc(N*sizeof(buf[0]));
memset( buf, 0, N*sizeof(buf[0]));
}
for( i = 0; i < N; i++ )
{
cvReleaseImage( &buf[i] );
buf[i] = cvCreateImage( size, IPL_DEPTH_8U, 1 );
cvZero( buf[i] );// clear Buffer Frame at the beginning
}
cvReleaseImage( &mhi );
mhi = cvCreateImage( size, IPL_DEPTH_32F, 1 );
cvZero( mhi ); // clear MHI at the beginning
} // end of if(mhi)
cvCvtColor( img, buf[last], CV_BGR2GRAY ); // convert frame to grayscale
idx1 = last;
idx2 = (last + 1) % N; // index of (last - (N-1))th frame
last = idx2;
// 做帧差
silh = buf[idx2];//差值的指向idx2 |idx2-idx1|-->idx2(<-silh)
cvAbsDiff( buf[idx1], buf[idx2], silh ); // get difference between frames
// 对差图像做二值化
cvThreshold( silh, silh, 30, 255, CV_THRESH_BINARY ); //threshold it,二值化
cvUpdateMotionHistory( silh, mhi, timestamp, MHI_DURATION ); // update MHI
cvConvert( mhi, dst );//将mhi转化为dst,dst=mhi
// 中值滤波,消除小的噪声
cvSmooth( dst, dst, CV_MEDIAN, 3, 0, 0, 0 );
cvPyrDown( dst, pyr, CV_GAUSSIAN_5x5 );// 向下采样,去掉噪声,图像是原图像的四分之一
cvDilate( pyr, pyr, 0, 1 ); // 做膨胀操作,消除目标的不连续空洞
cvPyrUp( pyr, dst, CV_GAUSSIAN_5x5 );// 向上采样,恢复图像,图像是原图像的四倍
//
// 下面的程序段用来找到轮廓
//
// Create dynamic structure and sequence.
stor = cvCreateMemStorage(0);
cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
// 找到所有轮廓
cvFindContours( dst, stor, &cont, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
// 直接使用CONTOUR中的矩形来画轮廓
for(;cont;cont = cont->h_next)
{
CvRect r = ((CvContour*)cont)->rect;
if(r.height * r.width > CONTOUR_MAX_AERA) // 面积小的方形抛弃掉
{
cvRectangle( img, cvPoint(r.x,r.y),
cvPoint(r.x + r.width, r.y + r.height),
CV_RGB(255,0,255), -1, CV_AA,0);
}
}
// free memory
cvReleaseMemStorage(&stor);
cvReleaseImage( &pyr );
}
using namespace std;
#include <map>
typedef struct PF
{
int count;
int red;
int blue;
int green;
}PN;
typedef pair<int ,PN> pspair;
typedef map<int ,PN, less<int> > psmap;
#define OFFSET 8
#define OCC 20
int _tmain(int argc, _TCHAR* argv[])
{
IplImage* pBackGround = 0;
IplImage* pBackground_ColorImg = 0;
CvCapture* pCapture = 0;
pCapture = cvCaptureFromFile("10.7.14.60_05_20100409152629.avi");
IplImage* pFrame = NULL; //当前获得的彩色帧
cvNamedWindow( "Motion", 1 );//建立窗口
cvNamedWindow( "BackGround", 1 );//建立窗口
//逐帧读取视频
int nFrmNum=0;
psmap ps;
while(pFrame = cvQueryFrame( pCapture))
{
nFrmNum++;
if( pFrame )//若取到则判断motion是否为空
{
if( !pBackGround )
{
pBackGround = cvCreateImage( cvSize(pFrame->width,pFrame->height), 8, 1 );
//创建motion帧,八位,一通道
cvZero( pBackGround );
//零填充motion
pBackGround->origin = pFrame->origin;
//内存存储的顺序和取出的帧相同
}
}
update_mhi( pFrame, pBackGround, 60 );//更新历史图像
if (nFrmNum==1)
{
PN pf;
pf.count = 0;
pf.red = 0;
pf.green = 0;
pf.blue = 0;
for (int i = 0;i<pFrame->height;i++)
{
for (int j = 0;j<pFrame->width;j++)
{
ps.insert(pspair(i*pFrame->width+j,pf));
}
}
pBackground_ColorImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,3);
cvZero(pBackground_ColorImg);
}
for (int i = 0;i<pFrame->height;i++)
{
uchar *ptr = (uchar *)(pFrame->imageData+i*pFrame->widthStep);
uchar *bptr = (uchar *)(pBackground_ColorImg->imageData+i*pBackground_ColorImg->widthStep);
for (int j = 0;j<pFrame->width;j++)
{
if (ptr[3*j]==255&&ptr[3*j+1]==0&&ptr[3*j+2]==255)
{
}
else
{
if (ps[i*pFrame->width+j].count==0)
{
ps[i*pFrame->width+j].blue = ptr[3*j];
ps[i*pFrame->width+j].green = ptr[3*j+1];
ps[i*pFrame->width+j].red = ptr[3*j+2];
ps[i*pFrame->width+j].count=1;
}
else if(ps[i*pFrame->width+j].count<OFFSET)
{
if (abs(ps[i*pFrame->width+j].blue-ptr[3*j])<OCC&&abs(ps[i*pFrame->width+j].green-ptr[3*j+1])<OCC&&abs(ps[i*pFrame->width+j].red-ptr[3*j+2])<OCC)
{
ps[i*pFrame->width+j].count+=1;
}
else
{
ps[i*pFrame->width+j].blue = ptr[3*j];
ps[i*pFrame->width+j].green = ptr[3*j+1];
ps[i*pFrame->width+j].red = ptr[3*j+2];
ps[i*pFrame->width+j].count=1;
}
}
else if(ps[i*pFrame->width+j].count==OFFSET)
{
bptr[3*j] = ps[i*pFrame->width+j].blue;
bptr[3*j+1] = ps[i*pFrame->width+j].green;
bptr[3*j+2] = ps[i*pFrame->width+j].red;
ps[i*pFrame->width+j].count+=1;
}
else
{
}
}
}
}
cvShowImage( "Motion", pFrame );//显示处理过的图像
cvShowImage( "BackGround", pBackground_ColorImg );//显示处理过的图像
if( cvWaitKey(10) >= 0 )//10ms中按任意键退出
break;
}
return 0;
}