// ObjectTracker.cpp : main source file for ObjectTracker.exe
//
#include "stdafx.h"
#include <atlframe.h>
#include <atlctrls.h>
#include <atldlgs.h>
#include "resource.h"
#include "aboutdlg.h"
#include "MainFrm.h"
#include <time.h>
#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <cmath>
#include <vector>
#include "atlstr.h"
///////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <cxcore.h>
#include <cvaux.h>//必须引此头文件
///////////////////////////////////////////////////////////////////////////
#pragma comment( lib, "opencv_legacy231d.lib" )
#pragma comment( lib, "opencv_legacy231.lib")
CAppModule _Module;
int Run(LPTSTR /*lpstrCmdLine*/ = NULL, int nCmdShow = SW_SHOWDEFAULT)
{
CMessageLoop theLoop;
_Module.AddMessageLoop(&theLoop);
CMainFrame wndMain;
if(wndMain.CreateEx() == NULL)
{
ATLTRACE(_T("Main window creation failed!\n"));
return 0;
}
wndMain.ShowWindow(nCmdShow);
int nRet = theLoop.Run();
_Module.RemoveMessageLoop();
return nRet;
}
#define FrameLen 704*576*3/2
#define FrameWidht 704
#define FrameHeight 576
void DoVideoTrack0(void* p)
{
/////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
//////////
////////// 前景检测模块 ////////////////////////
//////////
////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
//声明IplImage指针
IplImage* pFrame = NULL; //当前获得的彩色帧
IplImage* pBackground_ColorImg = NULL; //当前更新的背景,3色
IplImage* pDiff_ColorImg = NULL; //彩色帧差图像
IplImage* pLast_ColorImg = NULL; //上一帧彩色图像
IplImage* pFore_GrayImg = NULL; //灰度图前景,由当前帧获得
IplImage* pBackGround_GrayImg = NULL; //灰度图背景
IplImage* pLast_GrayImg = NULL; //上一帧
IplImage* pPreLast_GrayImg = NULL; //上上一帧
IplImage* pDiff_GrayImg = NULL; //灰度图帧间差
IplImage* pMask_GrayImg = NULL; //掩码模板
IplImage* pFrame_3COI[3] = { NULL , NULL , NULL }; //彩色图像3个通道
IplImage* pBackGround_3COI[3] ={ NULL , NULL , NULL }; //彩色背景3个通道
IplImage* pMask_3COI[3] = { NULL , NULL , NULL }; //用来分析3个彩色通道的掩码板
//图像数据处理矩阵 //对应上面IMG类
CvMat* pFrame_GrayMat = NULL;
CvMat* pBackground_GrayMat = NULL;
CvMat* pFore_GrayMat = NULL;
CvMat* pDiff_GrayMat = NULL;
CvMat* pMask_GrayMat = NULL;
CvMat* pFore_ColorMat = NULL;
CvMat* pBackground_ColorMat = NULL;
CvMat* pFrame_3COIMat[3] = { NULL , NULL , NULL };
CvMat* pBackGround_3COIMat[3] = { NULL , NULL , NULL };
//创建窗口
cvNamedWindow("video", 1);
cvNamedWindow("background",1);
cvNamedWindow("foreground",1);
cvNamedWindow("test",1);
//使窗口有序排列
cvMoveWindow("video", 90, 0);
cvMoveWindow("background", 450, 0);
cvMoveWindow("foreground", 810, 0);
cvMoveWindow("test", 810, 320);
int VideoChannelSum = 1;
//视频流
CvCapture *pCapture[10] = {NULL};
for (int nFile=1; nFile<=VideoChannelSum; nFile++)
{
// 打开一个视频文件
CString VideoFileName; //
VideoFileName.Format( _T("video%d.avi"), nFile );
//VideoFileName.Format( _T("E:\\PETS-09.avi"));
int len = VideoFileName.GetLength();
char *pstr = new char[len+1];
for (int i=0;i<len+1;i++)
{
*(pstr+i)= (char)(VideoFileName[i]);
}
CvCapture *pCapTemp = cvCaptureFromFile( pstr);
//CvCapture *pCapTemp = cvCaptureFromFile( "E:\\PETS-09.avi");
if ( !(pCapture[nFile-1] =pCapTemp) )
{
CString strMsg;
strMsg = "视频文件打开错误!";
// 提示用户
::MessageBox(NULL, strMsg, _T("系统提示"), MB_ICONINFORMATION | MB_OK);
// 返回FALSE
return ;
}
}
int nFrmNum=0;
//逐帧读取视频
while(pFrame = cvQueryFrame( *(pCapture+0) ))
{
nFrmNum++;
//cout<<nFrmNum<<endl;
//如果是第一帧,需要申请内存,并初始化
if(nFrmNum == 1) //做第一帧的处理
{
pBackground_ColorImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,3);
pDiff_ColorImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,3);
pLast_ColorImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,3);
pFore_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
pBackGround_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
pLast_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
pPreLast_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
pDiff_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
pMask_GrayImg = cvCreateImage(cvSize(pFrame->width, pFrame->height),
IPL_DEPTH_8U,1);
for (int i=0;i<3;i++)
{
pFrame_3COI[i] = cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
pFrame_3COI[i]->origin = 1;
}
for (int i=0;i<3;i++)
{
pBackGround_3COI[i] = cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
pBackGround_3COI[i]->origin = 1;
}
for (int i=0;i<3;i++)
{
pMask_3COI[i] = cvCreateImage(cvGetSize(pFrame),IPL_DEPTH_8U,1);
pMask_3COI[i]->origin = 1;
}
pBackground_ColorImg->origin = 1;
pDiff_ColorImg->origin = 1;
pLast_ColorImg->origin = 1;
pBackGround_GrayImg->origin = 1;
pFore_GrayImg->origin = 1;
pLast_GrayImg->origin = 1;
pPreLast_GrayImg->origin = 1;
pDiff_GrayImg->origin = 1;
pMask_GrayImg->origin = 1;
//图像数据处理矩阵 //对应上面IMG类
pFrame_GrayMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pBackground_GrayMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFore_GrayMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pDiff_GrayMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pMask_GrayMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
pFore_ColorMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC3);
pBackground_ColorMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC3);
for (int i=0;i<3;i++)
{
pFrame_3COIMat[i] = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
}
for (int i=0;i<3;i++)
{
pBackGround_3COIMat[i] = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
}
/////////////////////////////////////
///////////////////////////////////
/////////////////////////////////////
//转化成单通道图像再处理
cvCopy(pFrame,pBackground_ColorImg); //复制彩色帧
cvCopy(pFrame,pLast_ColorImg); //复制彩色帧
cvCvtColor(pFrame, pFore_GrayImg, CV_BGR2GRAY);
cvCvtColor(pFrame, pBackGround_GrayImg, CV_BGR2GRAY);
//转化为数组形式
cvConvert(pFrame, pFore_ColorMat);
cvConvert(pFrame, pBackground_ColorMat);
cvConvert(pFore_GrayImg, pFrame_GrayMat);
cvConvert(pFore_GrayImg, pFore_GrayMat);
cvConvert(pFore_GrayImg, pBackground_GrayMat);
}
else
{
cvCopy(pFore_GrayImg,pLast_GrayImg,0); //保存上一帧
//获取当前3个通道
for (int i=0; i<3; i++)
{
cvSetImageCOI(pFrame,i+1);
cvCopy(pFrame,pFrame_3COI[i]);
cvConvert(pFrame_3COI[i], pFrame_3COIMat[i]); //// 等同于 cvCopy, 但pCOIMat是浮点的数组
}
cvSetImageCOI(pFrame,0); //// 恢复全通道
cvConvert(pFrame,pFore_ColorMat);
cvCvtColor(pFrame, pFore_GrayImg, CV_BGR2GRAY);
cvConvert(pFore_GrayImg, pFrame_GrayMat);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//先做高斯滤波,以平滑图像
//cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);
//当前帧跟背景图相减获得彩色差分图像
cvAbsDiff(pFrame,pLast_ColorImg,pDiff_ColorImg);
//当前�