www.pudn.com > MotionEstimation.rar > MotionEstimationDoc.cpp


// MotionEstimationDoc.cpp : implementation of the CMotionEstimationDoc class 
// 
 
#include "stdafx.h" 
#include "MotionEstimation.h" 
 
#include "MotionEstimationDoc.h" 
#include "Motion.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMotionEstimationDoc 
 
IMPLEMENT_DYNCREATE(CMotionEstimationDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CMotionEstimationDoc, CDocument) 
	//{{AFX_MSG_MAP(CMotionEstimationDoc) 
	ON_COMMAND(IDM_OpenImage1, OnOpenImage1) 
	ON_COMMAND(IDM_OpenImage2, OnOpenImage2) 
	ON_COMMAND(IDM_MotionEstimate, OnMotionEstimate) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMotionEstimationDoc construction/destruction 
 
CMotionEstimationDoc::CMotionEstimationDoc() 
{ 
	// TODO: add one-time construction code here 
    orgframe = NULL; 
	curframe = NULL; 
} 
 
CMotionEstimationDoc::~CMotionEstimationDoc() 
{ 
	delete []orgframe; 
	orgframe = NULL  ; 
	delete []curframe ; 
	curframe = NULL   ; 
} 
 
BOOL CMotionEstimationDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
 
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CMotionEstimationDoc serialization 
 
void CMotionEstimationDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMotionEstimationDoc diagnostics 
 
#ifdef _DEBUG 
void CMotionEstimationDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CMotionEstimationDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMotionEstimationDoc commands 
 
//--------------------------------------------------------------------- 
// 
// Function:   IsBmp() 
// 
// Purpose:    判断图象是否为位图 
// 
// Parameters: 
//   CString lpszPathName -所打开图象的路径名 
// 
// Returns:     
//   BOOL      -若是位图,则返回TRUE;否则返回FALSE. 
// 
//--------------------------------------------------------------------- 
BOOL CMotionEstimationDoc::IsBmp(CString lpszPathName) 
{ 
	CFile file; 
	if( !file.Open( lpszPathName, CFile::modeRead ) ) 
		return 0; 
	 
	UINT nCount; 
	BITMAPFILEHEADER bmfh; 
	 
	nCount = file.Read((LPVOID) &bmfh, sizeof(BITMAPFILEHEADER)); 
	if(nCount != sizeof(BITMAPFILEHEADER)) 
		return FALSE; 
	if(bmfh.bfType != 0x4d42) 
		return FALSE; 
	file.Close( ); 
	return TRUE; 
} 
 
 
void CMotionEstimationDoc::OnOpenImage1()  
{ 
	// TODO: Add your command handler code here 
	CFileDialog fileopenbox(TRUE,NULL,"*.bmp",OFN_HIDEREADONLY,"files(*.bmp)|*.bmp|",NULL);	 
    fileopenbox.m_ofn.lpstrTitle="打开参考帧图象"; 
	if(fileopenbox.DoModal()!=IDOK) return; 
	CString strPathname=fileopenbox.GetPathName(); 
	 
	if(IsBmp(strPathname)) 
	{ 
		 
		Image1.Read(strPathname); 
		 
	} 
    else 
	{ 
		AfxMessageBox("不是bmp文件!"); 
	} 
	 
	//将图象Image1的灰度值赋给一维指针orgframe 
	int i,j;//循环变量 
    CSize m_size; 
	m_size.cx = Image1.Width(); 
	m_size.cy = Image1.Height(); 
	 
	orgframe=new unsigned char[m_size.cx * m_size.cy]; 
	 
	 
	for( i = 0; i < m_size.cy; i++ ) 
		for( j = 0; j < m_size.cx; j++ ) 
		{ 
		orgframe[i*m_size.cx+j]=(unsigned char)Image1.GetXY8(j,i); 
				//若是彩色图象,用 Image.GETXY_Y(j,i)-128, 
				//即用图象的灰度分量Y计算 
		} 
			 
	UpdateAllViews(FALSE); 
	return ; 
} 
 
void CMotionEstimationDoc::OnOpenImage2()  
{ 
	// TODO: Add your command handler code here 
	CFileDialog fileopenbox(TRUE,NULL,"*.bmp",OFN_HIDEREADONLY,"files(*.bmp)|*.bmp|",NULL);	 
    fileopenbox.m_ofn.lpstrTitle="打开后一帧分割图象"; 
	if(fileopenbox.DoModal()!=IDOK) return; 
	CString strPathname=fileopenbox.GetPathName(); 
	 
	if(IsBmp(strPathname)) 
	{ 
		 
		Image2.Read(strPathname); 
		 
	} 
    else 
	{ 
		AfxMessageBox("不是bmp文件!"); 
	} 
	 
	//将图象Image2的灰度值赋给二维指针Y2 
	int i,j;//循环变量 
	CSize m_size; 
	m_size.cx = Image2.Width(); 
	m_size.cy = Image2.Height(); 
	 
	curframe=new unsigned char[m_size.cx * m_size.cy]; 
	 
	 
	for( i = 0; i < m_size.cy; i++ ) 
		for( j = 0; j < m_size.cx; j++ ) 
		{ 
		curframe[i*m_size.cx+j]=(unsigned char)Image2.GetXY8(j,i); 
			//若是彩色图象,用 Image.GETXY_Y(j,i)-128, 
			//即用图象的灰度分量Y计算 
		} 
	UpdateAllViews(NULL); 
	SetModifiedFlag(); 
	return ; 
} 
 
void CMotionEstimationDoc::OnMotionEstimate()  
{ 
	// TODO: Add your command handler code here 
	int i,j;//循环变量 
    CSize m_size; 
	m_size.cx = Image1.Width(); 
	m_size.cy = Image1.Height(); 
	ImageEstimation = Image1;//要将对象ImageEstimation初始化*** 
	 
	unsigned char *reconframe =  
		new unsigned char[m_size.cx * m_size.cy]; 
	unsigned char *cancha =  
		new unsigned char[m_size.cx * m_size.cy]; 
	 
	for( i = 0; i < m_size.cy; i++ ) 
		for( j = 0; j < m_size.cx; j++ ) 
		{ 
			reconframe[i*m_size.cx+j]=0; 
			cancha[i*m_size.cx+j]=0; 
		} 
 
	 
    CMotion motion; 
	mbinfo *mbi; 
// 	mbi = NULL; 
     mbi = new mbinfo[(m_size.cx/16)*(m_size.cy/16)]; 
	 
	 
	 motion.motion_estimation(orgframe,m_size.cx,m_size.cy,curframe, 
		reconframe,cancha,4,4,mbi); 
     
	for(i=0;i