MainFrm.cpp
上传用户:smdfuse
上传日期:2015-11-06
资源大小:98k
文件大小:15k
- // MainFrm.cpp : implementation of the CMainFrame class
- //
- #include "stdafx.h"
- #include "Video Demo.h"
- #include "Video demodoc.h"
- #include "MainFrm.h"
- #include "AVIHandler.h" // AVI文件处理
- #include "colortrans.h"
- #include "StaticDetect.h"
- #include "ChafenMul.h"
- #include "POSDiag.h"
- #include "MotionDetectDiag.h"
- #include "GravityTrack.h"
- #include "GravityCenter.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame
- IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
- BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
- //{{AFX_MSG_MAP(CMainFrame)
- ON_WM_CREATE()
- ON_COMMAND(ID_FILE_OPENVIDEO, OnFileOpenvideo)
- ON_COMMAND(ID_MOTION_SD, OnMotionSd)
- ON_COMMAND(ID_MOTION_DD, OnMotionDd)
- ON_COMMAND(ID_TRACEK_MEANSHIFT, OnTracekMeanshift)
- ON_COMMAND(ID_TRACEK_GAV, OnTracekGav)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- static UINT indicators[] =
- {
- ID_SEPARATOR, // status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
- };
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame construction/destruction
- CMainFrame::CMainFrame()
- {
- // TODO: add member initialization code here
-
- }
- CMainFrame::~CMainFrame()
- {
- }
- int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
-
- //if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
- // | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
- // !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
- // {
- // TRACE0("Failed to create toolbarn");
- // return -1; // fail to create
- // }
- if (!m_wndStatusBar.Create(this) ||
- !m_wndStatusBar.SetIndicators(indicators,
- sizeof(indicators)/sizeof(UINT)))
- {
- TRACE0("Failed to create status barn");
- return -1; // fail to create
- }
- // TODO: Delete these three lines if you don't want the toolbar to
- // be dockable
- //m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
- // EnableDocking(CBRS_ALIGN_ANY);
- // DockControlBar(&m_wndToolBar);
- return 0;
- }
- BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
- {
- if( !CFrameWnd::PreCreateWindow(cs) )
- return FALSE;
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return TRUE;
- }
- /************************************************************************/
- /* OnOpenOpenVideo */
- /* 打开AVI视频文件 */
- /* 基于中央数据级控架构 */
- /************************************************************************/
- void CMainFrame::OnOpenOpenVideo()
- {
- AVIHandler *avi = NULL;
- //添加一路视频
- //考虑的问题是,如何将参数传递,到框架
- CString strFilter("Data Files (*.avi)|*.avi");
- CFileDialog diag(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,strFilter);
- //打开第一个文件
- int nID = diag.DoModal();
- if(nID == IDOK){
-
- // 新建一个子窗口
- POSITION pos=theApp.m_pDocManager->GetFirstDocTemplatePosition();
- // 取得一个文档模板,我取的是第一个,你可以去任何一个模板
- CDocTemplate* pDocTempl=theApp.m_pDocManager->GetNextDocTemplate(pos);
- // 通过打开文件的方式获得一个新窗口,并得到CDocument对象
- CVideoDemoDoc * doc = (CVideoDemoDoc *)pDocTempl->OpenDocumentFile(NULL);//theApp.m_pDocManager->OpenDocumentFile(NULL);
- // 建立视频数据源
- avi = new AVIHandler(diag.GetFileName()); // 打开AVI文件,AVI RGB
- theApp.curVideo = new VideoData(); // 当前视频数据源
- theApp.curVideo->doc = doc;
- theApp.curVideo->index = 0;
- theApp.curVideo->ms = (MediaSource*)avi;
- // 设置View的显示缓冲
- ImageData *id = new ImageData();
- id->bytes = 3;
- id->coreDoc = doc;
- id->format = IMAGE_FORMAT_RGB24;
- id->imgWidth = avi->GetFrameWidth();
- id->imgHeight = avi->GetFrameHeight();
- id->data = avi->GetSingleFrame(0); // 获得第一帧图像
- doc->SetData(id); // 设置第一帧图像
- // 设置文档和视图的视频源数据
- doc->SetVideoSource(theApp.curVideo); // 设置Document数据
- doc->RefreshView(); // 刷新显示View
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame diagnostics
- #ifdef _DEBUG
- void CMainFrame::AssertValid() const
- {
- CFrameWnd::AssertValid();
- }
- void CMainFrame::Dump(CDumpContext& dc) const
- {
- CFrameWnd::Dump(dc);
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMainFrame message handlers
- /************************************************************************/
- /* 打开视频文件AVI */
- /************************************************************************/
- void CMainFrame::OnFileOpenvideo()
- {
- AVIHandler *avi = NULL;
- //添加一路视频
- //考虑的问题是,如何将参数传递,到框架
- CString strFilter("Data Files (*.avi)|*.avi");
- CFileDialog diag(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,strFilter);
- //打开第一个文件
- int nID = diag.DoModal();
- if(nID == IDOK){
-
- // 新建一个子窗口
- POSITION pos=theApp.m_pDocManager->GetFirstDocTemplatePosition();
- // 取得一个文档模板,我取的是第一个,你可以去任何一个模板
- CDocTemplate* pDocTempl=theApp.m_pDocManager->GetNextDocTemplate(pos);
- // 通过打开文件的方式获得一个新窗口,并得到CDocument对象
- CVideoDemoDoc * doc = (CVideoDemoDoc *)pDocTempl->OpenDocumentFile(NULL);//theApp.m_pDocManager->OpenDocumentFile(NULL);
- // 建立视频数据源
- avi = new AVIHandler(diag.GetFileName()); // 打开AVI文件,AVI RGB
- if(!avi->isLoaded())
- {
- AfxMessageBox("打开文件错误!");
- return;
- }
- if(avi->GetLPBM()->biBitCount != 24)
- {
- delete avi;
- AfxMessageBox("AVI文件必须是24位非压缩彩色视频!");
- return;
- }
- theApp.curVideo = new VideoData(); // 当前视频数据源
- theApp.curVideo->doc = doc;
- theApp.curVideo->index = 0;
- theApp.curVideo->ms = (MediaSource*)avi;
- // 设置View的显示缓冲
- ImageData *id = new ImageData();
- id->bytes = 3;
- id->coreDoc = doc;
- id->format = IMAGE_FORMAT_RGB24;
- id->imgWidth = avi->GetFrameWidth();
- id->imgHeight = avi->GetFrameHeight();
- id->data = avi->GetSingleFrame(0); // 获得第一帧图像
- doc->SetData(id); // 设置第一帧图像
- // 设置文档和视图的视频源数据
- doc->SetVideoSource(theApp.curVideo); // 设置Document数据
- doc->RefreshView(); // 刷新显示View
- }
- }
- /************************************************************************/
- /* 静止背景目标检测 */
- /************************************************************************/
- DWORD WINAPI StaticDetectT(LPVOID data){
- CVideoDemoDoc *doc = (CVideoDemoDoc*)data;
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- unsigned char *frame = avi->GetSingleFrame(0);
- int frameWidth = avi->GetFrameWidth();
- int frameHeight = avi->GetFrameHeight();
- // 色彩变换
- ColorTrans ct;
- CStatsticDetect sd(frameWidth,frameHeight);
-
- // 输出缓冲
- unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];
- for(int i = 0; i < frameLength - 1; i++)
- {
- // 获得一帧图像
- frame = avi->GetSingleFrame(i);
- // 转换到灰度图像
- //ct.RGB2Gray8(frame,grayBuffer,frameWidth,frameHeight);
- sd.ReceiveFrame(i,frame,D_STATISTIC);
- // 添加一帧数据
-
- ct.gray2RGB2(sd.m_pDiffImage,outBuffer,frameWidth,frameHeight);
- doc->id->data = outBuffer;
-
- doc->RefreshView();
- }
- delete[] outBuffer;
- return 0;
- }
- /************************************************************************/
- /* 静止背景目标检测 */
- /************************************************************************/
- void CMainFrame::OnMotionSd()
- {
- DWORD exitCode;
-
- if(GetExitCodeThread(th,&exitCode)){
- TerminateThread(th,exitCode);
- }
- DWORD pID;
- //控制线程播放
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- if(!theApp.curVideo)
- {
- AfxMessageBox("没有打开视频文件 !");
- return;
- }
- // 创建线程
- th = CreateThread(NULL,0,StaticDetectT,theApp.curVideo->doc,0,&pID);
- }
- /************************************************************************/
- /* 动止背景目标检测 */
- /************************************************************************/
- DWORD WINAPI MotionDetectT(LPVOID data){
- CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;
-
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- unsigned char *frame = avi->GetSingleFrame(0);
- int frameWidth = avi->GetFrameWidth();
- int frameHeight = avi->GetFrameHeight();
- // 色彩变换
- ColorTrans ct;
- int *threshold = (int*)data;
-
- MotionDetectDiag diag;
- //if(diag.DoModal()){
- // threshold = diag.m_index;
- //}
- CStatsticDetect sd(frameWidth,frameHeight);
- //CStaticDetect sd(frameWidth,frameHeight);
- ChafenMul cm(frameWidth,frameHeight,*threshold);
- // 输出缓冲
- unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];
- unsigned char * gray = new unsigned char[frameWidth * frameHeight * 3];
- for(int i = 0; i < frameLength - 1; i++)
- {
- // 获得一帧图像
- frame = avi->GetSingleFrame(i);
-
- ct.RGB2Gray8(frame,gray,frameWidth,frameHeight);
- // 准备数据
- cm.PrepareData(gray,frameWidth,frameHeight);
-
-
- if(i % 4 == 0 && i != 0)
- {
- cm.process();
-
-
- sd.DeNoise(cm.frame1,frameWidth,frameHeight);
- ct.gray2RGB2(cm.frame1,outBuffer,frameWidth,frameHeight);
- doc->id->data = outBuffer;
-
- doc->RefreshView();
-
- }
- }
- delete[] outBuffer;
- return 0;
- }
- /************************************************************************/
- /* 动背景目标检测 */
- /************************************************************************/
- void CMainFrame::OnMotionDd()
- {
- DWORD exitCode;
-
- if(GetExitCodeThread(th,&exitCode)){
- TerminateThread(th,exitCode);
- }
-
- if(theApp.curVideo == NULL)
- AfxMessageBox("没有打开视频文件!");
- DWORD pID;
- //控制线程播放
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
-
- int threshold = 100;
- MotionDetectDiag mdd;
- if(mdd.DoModal())
- {
- threshold = mdd.m_index;
- }
-
- int * thre = (int*)malloc(sizeof(int));
- *thre = threshold;
- // 创建线程
- th = CreateThread(NULL,0,MotionDetectT,thre,0,&pID);
-
- }
- /************************************************************************/
- /* 动止背景目标检测 */
- /************************************************************************/
- DWORD WINAPI MSS(LPVOID data){
- CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;//(CVideoDemoDoc*)data;
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- unsigned char *frame = avi->GetSingleFrame(0);
- int frameWidth = avi->GetFrameWidth();
- int frameHeight = avi->GetFrameHeight();
-
- MeanShiftSegger* mms = (MeanShiftSegger*)data;
- // 输出缓冲
- unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];
- for(int i = 0; i < frameLength - 1; i++)
- {
- // 获得一帧图像
- frame = avi->GetSingleFrame(i);
-
- mms->MeanShiftTrackProcess(frame,i);
- doc->id->data = frame;
-
- doc->RefreshView();
- }
- delete[] outBuffer;
- return 0;
- }
- /************************************************************************/
- /* MeanShift跟踪 */
- /************************************************************************/
- void CMainFrame::OnTracekMeanshift()
- {
- DWORD exitCode;
-
- if(GetExitCodeThread(th,&exitCode)){
- TerminateThread(th,exitCode);
- }
- DWORD pID;
- if(theApp.curVideo == NULL){
- AfxMessageBox("没有打开视频!");
- return;
- }
-
- int x,y,width,height;
- POSDiag pd;
- if(pd.DoModal())
- {
- x= pd.m_x;
- y= pd.m_y;
- width = pd.m_width;
- height = pd.m_height;
- }
- else
- return;
-
- mss = new MeanShiftSegger();
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- unsigned char *frame = avi->GetSingleFrame(0);
- int frameWidth = avi->GetFrameWidth();
- int frameHeight = avi->GetFrameHeight();
- mss->InitMeanShiftTracker(frame,frameWidth,frameHeight,x,y,width,height);
-
- th = CreateThread(NULL,0,MSS,mss,0,&pID);
-
- }
- /************************************************************************/
- /* 重心 法 跟踪 */
- /************************************************************************/
- DWORD WINAPI GAV(LPVOID data){
- int* threshold = (int*)data;
- CVideoDemoDoc *doc = (CVideoDemoDoc*)theApp.curVideo->doc;
- int frameLength = theApp.curVideo->ms->GetFrameCount();
- MediaSource *avi = theApp.curVideo->ms;
- unsigned char *frame = avi->GetSingleFrame(0);
- int frameWidth = avi->GetFrameWidth();
- int frameHeight = avi->GetFrameHeight();
- // 输出缓冲
- unsigned char * outBuffer = new unsigned char[frameHeight * frameWidth * 3];
- unsigned char * grayBuffer =new unsigned char[frameWidth * frameHeight];
- ColorTrans ct;
- CGravityCenter* m_gravitycenter;
- TARGETPARA m_target;
- for(int i = 0; i < frameLength - 1; i++)
- {
- // 获得一帧图像
- frame = avi->GetSingleFrame(i);
-
- ct.RGB2Gray8(frame,grayBuffer,frameWidth,frameHeight);
- if(i == 0)
- {
- m_gravitycenter = new CGravityCenter(frameWidth,frameHeight);
- m_gravitycenter->InitalObjectTracker(T_GRACENTER,*threshold);
- m_gravitycenter->GravityCenter(i, frame);
- }
- else
- {
- m_gravitycenter->GravityCenter(i, frame);
- }
-
- m_target = m_gravitycenter->m_result;
- /*
- // 重心跟踪
- WININFO info = GravityTrack(grayBuffer,frameWidth,frameHeight,*threshold);
- if(!info.flag)
- {
- for(int j = max(info.pt.y - info.h / 2,0);j <= min(info.pt.y + info.h / 2,frameHeight); j ++)
- for(int i = max(info.pt.x - info.w / 2,0);i <= min(info.pt.x + info.w / 2,frameWidth); i ++)
- {
- frame[j * frameWidth * 3 + i * 3] = 255;
- }
-
- }*/
-
- doc->id->data = frame;
-
- doc->RefreshView();
- }
- delete[] grayBuffer;
- delete[] outBuffer;
- return 0;
- }
- /************************************************************************/
- /* 重心法跟踪 */
- /************************************************************************/
- void CMainFrame::OnTracekGav()
- {
- DWORD exitCode;
-
- if(GetExitCodeThread(th,&exitCode)){
- TerminateThread(th,exitCode);
- }
- DWORD pID;
- if(theApp.curVideo == NULL){
- AfxMessageBox("没有打开视频!");
- return;
- }
-
- int threshold = 100;
- MotionDetectDiag mdd;
- if(mdd.DoModal())
- {
- threshold = mdd.m_index;
- }
- int * thre = (int*)malloc(sizeof(int));
- *thre = threshold;
- th = CreateThread(NULL,0,GAV,thre,0,&pID);
- }