www.pudn.com > 人体步态跟踪识别bate版.rar > humantrackDoc.cpp
// humantrackDoc.cpp : implementation of the CHumantrackDoc class // #include "stdafx.h" #include "humantrack.h" #include#include #include "shlwapi.h" #include "humantrackDoc.h" #include "hh.h" #include "math.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CHumantrackDoc IMPLEMENT_DYNCREATE(CHumantrackDoc, CDocument) BEGIN_MESSAGE_MAP(CHumantrackDoc, CDocument) //{{AFX_MSG_MAP(CHumantrackDoc) ON_COMMAND(ID_CaptureBackGround, OnCaptureBackGround) ON_COMMAND(ID_MotionTrack, OnMotionTrack) ON_COMMAND(ID_KalmanFilter, OnKalmanFilter) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CHumantrackDoc construction/destruction CHumantrackDoc::CHumantrackDoc() { // TODO: add one-time construction code here m_hDIB = NULL; m_palDIB = NULL; m_sizeDoc = CSize(1,1); } CHumantrackDoc::~CHumantrackDoc() { if (m_hDIB != NULL) { ::GlobalFree((HGLOBAL) m_hDIB); } if (m_palDIB != NULL) { delete m_palDIB; } } BOOL CHumantrackDoc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // TODO: add reinitialization code here // (SDI documents will reuse this document) return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CHumantrackDoc serialization void CHumantrackDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here } else { // TODO: add loading code here } } ///////////////////////////////////////////////////////////////////////////// // CHumantrackDoc diagnostics #ifdef _DEBUG void CHumantrackDoc::AssertValid() const { CDocument::AssertValid(); } void CHumantrackDoc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CHumantrackDoc commands BOOL CHumantrackDoc::OnSaveDocument(LPCTSTR lpszPathName) { // TODO: Add your specialized code here and/or call the base class CFile file; CFileException fe; if (!file.Open(lpszPathName, CFile::modeCreate | CFile::modeReadWrite | CFile::shareExclusive, &fe)) { ReportSaveLoadException(lpszPathName, &fe, TRUE, AFX_IDP_INVALID_FILENAME); return FALSE; } // 重新调用SaveDIB函数 BOOL bSuccess = FALSE; TRY { BeginWaitCursor(); bSuccess = ::SaveDIB(m_hDIB, file); file.Close(); } CATCH (CException, eSave) { file.Abort(); EndWaitCursor(); ReportSaveLoadException(lpszPathName, eSave, TRUE, AFX_IDP_FAILED_TO_SAVE_DOC); return FALSE; } END_CATCH EndWaitCursor(); SetModifiedFlag(FALSE); // 修改标记 if (!bSuccess) { // 其它格式的DIB CString strMsg; AfxMessageBox("其他格式的数据,不能打开"); } return bSuccess; } BOOL CHumantrackDoc::OnOpenDocument(LPCTSTR lpszPathName) { CFile file; CFileException fe; if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe)) { ReportSaveLoadException(lpszPathName, &fe, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); return FALSE; } DeleteContents(); BeginWaitCursor(); // 重新调用ReadDIBFile函数 TRY { m_hDIB = ::ReadDIBFile(file); LPSTR pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); } CATCH (CFileException, eLoad) { file.Abort(); EndWaitCursor(); ReportSaveLoadException(lpszPathName, eLoad, FALSE, AFX_IDP_FAILED_TO_OPEN_DOC); m_hDIB = NULL; return FALSE; } END_CATCH InitDIBData(); EndWaitCursor(); if (m_hDIB == NULL) { // 非DIB格式 CString strMsg; AfxMessageBox("非DIB格式文件不能打开"); return FALSE; } SetPathName(lpszPathName); SetModifiedFlag(FALSE); // 修改标记 return TRUE; } void CHumantrackDoc::InitDIBData() { if (m_palDIB != NULL) { delete m_palDIB; m_palDIB = NULL; } if (m_hDIB == NULL) { return; } //创建文档尺寸 LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX) { ::GlobalUnlock((HGLOBAL) m_hDIB); ::GlobalFree((HGLOBAL) m_hDIB); m_hDIB = NULL; CString strMsg; AfxMessageBox("文件太大,不能打开"); return; } m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB)); ::GlobalUnlock((HGLOBAL) m_hDIB); //创建拷贝调色板 m_palDIB = new CPalette; if (m_palDIB == NULL) { // 释放内存 ::GlobalFree((HGLOBAL) m_hDIB); m_hDIB = NULL; return; } if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL) { delete m_palDIB; m_palDIB = NULL; return; } } void CHumantrackDoc::ReplaceHDIB(HDIB hDIB) { if (m_hDIB != NULL) { ::GlobalFree((HGLOBAL) m_hDIB); } m_hDIB = hDIB; } ////////////////////////////////////////////////////////////////////////// //根据运动图象提取其中的静止背景。其中运动图象要求为bmp文件,并按顺序排列。 ////////////////////////////////////////////////////////////////////////// void CHumantrackDoc::OnCaptureBackGround() { // TODO: Add your command handler code here if(FramePathDlg.DoModal()!=IDOK) { AfxMessageBox("没有选择图像文件途径,无法打开文件"); return ; } // 获得当前打开文件的文件路径名 CString strPath(_T(FramePathDlg.m_StrFilePath)); char strPathName[80]; strcpy(strPathName,strPath); strcat(strPathName,"\\"); //如果文件文字不相同,则要修改一下语句 strcat(strPathName,"1 0"); strcat(strPathName,FramePathDlg.m_beginframe); strcat(strPathName,".bmp"); CFile file; file.Open(strPathName,CFile::modeRead); m_hDIB = ::ReadDIBFile(file); LPSTR pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); InitDIBData(); ::GlobalUnlock((HGLOBAL) m_hDIB); file.Close(); int nImageHeight=::DIBHeight(pDIB); int nImageWidth=::DIBWidth(pDIB); // 序列的总帧数 int nTotalFrameNum =FramePathDlg.m_FrameNumber; //图像的路径 unsigned char * pUnchBackGround=new unsigned char[nImageHeight*nImageWidth]; //设置起始帧和当前帧序号 char currentframe[80]; CString beginframe(_T(FramePathDlg.m_beginframe)); strcpy(currentframe,beginframe); int hh=atoi(currentframe); bg.SetCurrentFrame(hh); //设置文件路径 bg.SetFileName(strPathName); //设置终止帧序号 bg.m_nFrameTotalNum=FramePathDlg.m_FrameNumber; CString endframe(_T(FramePathDlg.m_endframe)); strcpy(currentframe,endframe); hh=atoi(currentframe); bg.SetEndFrame(hh); bg.m_ImageHeight=nImageHeight; bg.m_ImageWidth=nImageWidth; //获得背景图像 bg.GetBackground(nTotalFrameNum,pUnchBackGround); //显示背景图像 LPSTR lpDIBBits; // 指向一个DIB位 pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); lpDIBBits = ::FindDIBBits(pDIB); memcpy(lpDIBBits,pUnchBackGround,nImageHeight*nImageWidth*sizeof(unsigned char)); ::GlobalUnlock((HGLOBAL) m_hDIB); SetModifiedFlag(FALSE); // 修改标记 UpdateAllViews(NULL); delete []pUnchBackGround; } //响应腿部跟踪按钮 void CHumantrackDoc::OnMotionTrack() { // TODO: Add your command handler code here if(FramePathDlg.DoModal()!=IDOK) { AfxMessageBox("没有选择图像文件途径,无法打开文件"); return ; } // 获得当前打开文件的文件路径名 CString strPath(_T(FramePathDlg.m_StrFilePath)); char strPathName[80]; strcpy(strPathName,strPath); strcat(strPathName,"\\"); //strcat(strPathName,"1 0"); strcat(strPathName,"dh1_"); strcat(strPathName,FramePathDlg.m_beginframe); strcat(strPathName,".bmp"); CFile file; file.Open(strPathName,CFile::modeRead); m_hDIB = ::ReadDIBFile(file); LPSTR pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); InitDIBData(); ::GlobalUnlock((HGLOBAL) m_hDIB); file.Close(); int nImageHeight=::DIBHeight(pDIB); int nImageWidth=::DIBWidth(pDIB); // 序列的总帧数 int nTotalFrameNum =FramePathDlg.m_FrameNumber; //图像的路径 //设置起始帧和当前帧序号 char currentframe[80]; CString beginframe(_T(FramePathDlg.m_beginframe)); strcpy(currentframe,beginframe); int hh1=atoi(currentframe); tm.SetCurrentFrame(hh1); //设置文件路径 tm.SetFileName(strPathName); //设置终止帧序号 tm.m_nFrameTotalNum=FramePathDlg.m_FrameNumber; CString endframe(_T(FramePathDlg.m_endframe)); strcpy(currentframe,endframe); int hh2=atoi(currentframe); tm.SetEndFrame(hh2); tm.m_ImageHeight=nImageHeight; tm.m_ImageWidth=nImageWidth; LPSTR lpDIBBits; // 指向一个DIB位 pDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB); lpDIBBits = ::FindDIBBits(pDIB); unsigned char * p=new unsigned char[nImageHeight*nImageWidth]; //读入关节位置值 tm.ReadData(); CArray m_left_thigh_angle; CArray m_right_thigh_angle; CArray m_left_crus_angle; CArray m_right_crus_angle; CArray m_thigh_angle; double tmp,tmpx,tmpy; double tmplta,tmprta,tmplca,tmprca,tmplrca; double d1,d2,d3; CPoint p1,p2,p3,p4; if (m_left_thigh_angle.GetSize()!=0) { m_left_thigh_angle.RemoveAll(); } if (m_right_thigh_angle.GetSize()!=0) { m_right_thigh_angle.RemoveAll(); } if (m_left_crus_angle.GetSize()!=0) { m_left_crus_angle.RemoveAll(); } if (m_right_crus_angle.GetSize()!=0) { m_right_crus_angle.RemoveAll(); } if (m_thigh_angle.GetSize()!=0) { m_thigh_angle.RemoveAll(); } for(int i=hh1-1;i SelectObject(&pen); //kalman滤波器初始化 int pointnum=human.m_JointPoint.GetSize(); CPoint TmpPos; for(int i=0;i SetPixel(pos,RGB(0,0,0)); } delete []X0; delete []XPRE; delete []ZIN; delete []P0; } */ }