《基于帧间差分与背景差分相融合下的运动目标检测》 opencv平台下 vc++6.0 我读不大懂 求指导 求注释啊神
#include <stdio.h>#include <tchar.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvaux.h>
#include <cvcam.h>
#define max(a, b) ((a)>(b)?(a):(b))
//调整矩形框B,使其在A的范围
CvRect my_ChangeRect(CvRect A,CvRect B)
{
if(B.x<A.x)
B.x=A.x;
if(B.x+B.width>A.width)
B.width=A.width-B.x;
if(B.y<A.y)
B.y=A.y;
if(B.y+B.height>A.height)
B.height=A.height-B.y;
return(B);
}
int main( int argc, char** argv )
{
//声明IplImage指针
IplImage* pFrame = NULL;//图像帧
IplImage* pFrImg = NULL;//前景图像
IplImage* pBkImg = NULL;//背景图像
IplImage* pBackproject = NULL;//反向投影图像
IplImage* pImage = NULL;//视频帧图像
IplImage* pTraje = NULL;//轨迹图
IplImage* hsv = NULL;
IplImage* hue = NULL;
IplImage* mask = NULL;
//声明点集变量
CvPoint lastpoint;//上一帧的中心坐标
CvPoint predictpoint;//预测坐标
CvPoint measurepoint;//当前帧的坐标
CvPoint origin;
CvPoint point_text;
//声明矩形框变量
CvRect origin_box;
CvRect track_window;
CvRect search_window;
CvBox2D track_box;//跟踪返回的区域box,带有角度
CvConnectedComp track_comp;
//声明文本变量
CvFont font;//显示的文本字体
char nchar[10];//显示数字字符串
//声明矩阵类型变量
CvMat* measurement;
CvMat* realposition;
const CvMat* prediction;
CvCapture* pCapture = NULL;
CvBGStatModel *bg_model = NULL;
CvMemStorage * m_storage = cvCreateMemStorage(0);
CvSeq *m_contour = NULL;
int nFrmNum = 0;
int i = 0;
cvNamedWindow("视频图像", 1);
cvNamedWindow("背景图像", 1);//该背景图像指的是经Gassian(简单背景差分,帧间差分)得到的当前综合背景
cvNamedWindow("前景图像", 1);
cvMoveWindow("视频图像", 30, 0);
cvMoveWindow("背景图像", 450, 0);
cvMoveWindow("前景图像", 900, 0);
pCapture = cvCaptureFromFile("001.avi"); //换视频文件的地方,改引号里面的文件名就好
while(pFrame = cvQueryFrame(pCapture))
{
pImage = cvCreateImage( cvGetSize(pFrame), 8, 3);
pImage->origin = pFrame->origin;
hsv = cvCreateImage(cvGetSize(pFrame), 8, 3);
hue = cvCreateImage(cvGetSize(pFrame), 8, 1);
mask = cvCreateImage(cvGetSize(pFrame), 8, 1);
pBackproject = cvCreateImage(cvGetSize(pFrame), 8, 1);
pBackproject->origin = pFrame->origin;
pTraje = cvCreateImage( cvGetSize(pFrame), 8, 1);
pTraje->origin = pFrame->origin;
cvCopy(pFrame, pImage, 0);
cvCvtColor(pImage, hsv, CV_BGR2HSV);
nFrmNum++;
i++;
//高斯建模部分
if(nFrmNum==1)
{
pBkImg = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 3);
pFrImg = cvCreateImage(cvGetSize(pFrame), IPL_DEPTH_8U, 1);
bg_model = (CvBGStatModel*)cvCreateGaussianBGModel(pFrame, 0);
}
else
{
cvUpdateBGStatModel( pFrame, bg_model);
cvErode(bg_model->foreground, bg_model->foreground,0,1); //腐蚀
cvDilate(bg_model->foreground, bg_model->foreground,0,1);//膨胀
cvCopy(bg_model->foreground, pFrImg);
cvCopy(bg_model->background, pBkImg);
pBkImg->origin=1;
pFrImg->origin=1;
//高级形态学变换
cvMorphologyEx( pFrImg, pFrImg, 0, 0, CV_MOP_CLOSE, 1);//闭运算
cvMorphologyEx( pFrImg, pFrImg, 0, 0, CV_MOP_OPEN, 1 );//开运算
}
cvShowImage("视频图像", pFrame);
cvShowImage("背景图像", pBkImg);
//提取轮廓 到contour序列中
cvFindContours( pFrImg,m_storage, &m_contour, sizeof(CvContour),
CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
for( ; m_contour != 0; m_contour = m_contour->h_next)
{
CvScalar color = CV_RGB( 100, 0, 100 );
CvScalar color_rect = CV_RGB( 0, 255, 255);
CvPoint PointArray[1250];
CvPoint* pt;
pt = PointArray;
CvRect m_contour_rect = cvBoundingRect(m_contour, 1);
if((30 < m_contour_rect.width < 100) && (30 < m_contour_rect.height < 100) && (m_contour_rect.height < m_contour_rect.width < (1.5 * m_contour_rect.height)))
{
cvRectangle(pFrImg, cvPoint(m_contour_rect.x, m_contour_rect.y),
cvPoint((m_contour_rect.x + m_contour_rect.width),(m_contour_rect.y + m_contour_rect.height)),
color_rect, 2, 8, 0);
cvDrawContours( pFrImg, m_contour, color, color, -1, CV_FILLED, 8, cvPoint(0,0));
cvShowImage("前景图像", pFrImg);
}
}
int c = cvWaitKey(33);
if(c == 'p')
c = cvWaitKey(0);
if(c == 's')
c = cvWaitKey(33);
if(c == 'q')
break;
}
cvReleaseBGStatModel( &bg_model );
cvReleaseCapture(&pCapture);
cvReleaseImage(&pFrImg);
cvReleaseImage(&pBkImg);
cvReleaseImage(&pImage);
cvReleaseImage(&pTraje);
if(m_storage) cvClearMemStorage(m_storage);
cvDestroyWindow("视频图像");
cvDestroyWindow("背景图像");
cvDestroyWindow("前景图像");
return 0;