www.pudn.com > 用形态学方法提取骨架线.rar > LogDoc.cpp


// LogDoc.cpp : implementation of the CLogDoc class 
// 
 
#include "stdafx.h" 
#include "Log.h" 
 
#include "LogDoc.h" 
 
#include "WrapBitmap.h" 
#include "Filter.h" 
#include "SettingDlg.h" 
#include "Morphology.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogDoc 
 
IMPLEMENT_DYNCREATE(CLogDoc, CDocument) 
 
BEGIN_MESSAGE_MAP(CLogDoc, CDocument) 
	//{{AFX_MSG_MAP(CLogDoc) 
	ON_COMMAND(ID_FILTER_LOG, OnFilterLog) 
	ON_COMMAND(ID_FILE_SAVE, OnFileSave) 
	ON_COMMAND(ID_FILTER_LAP, OnFilterLap) 
	ON_COMMAND(ID_FILTER_GAUSS, OnFilterGauss) 
	ON_COMMAND(ID_COLOR_YUZHI, OnColorYuzhi) 
	ON_COMMAND(ID_MORPH_DILATE, OnMorphDilate) 
	ON_COMMAND(ID_MORPH_ERODE, OnMorphErode) 
	ON_COMMAND(ID_MORPH_SKELETON, OnMorphSkeleton) 
	ON_COMMAND(ID_MORPH_OPEN, OnMorphOpen) 
	ON_COMMAND(ID_MORPH_THINNING, OnMorphThinning) 
	ON_COMMAND(ID_COLOR_SHAPE, OnColorShape) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogDoc construction/destruction 
 
CLogDoc::CLogDoc() 
{ 
	// TODO: add one-time construction code here 
} 
 
CLogDoc::~CLogDoc() 
{ 
} 
 
BOOL CLogDoc::OnNewDocument() 
{ 
	if (!CDocument::OnNewDocument()) 
		return FALSE; 
 
	// TODO: add reinitialization code here 
	// (SDI documents will reuse this document) 
 
	return TRUE; 
} 
 
 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogDoc serialization 
 
void CLogDoc::Serialize(CArchive& ar) 
{ 
	if (ar.IsStoring()) 
	{ 
		// TODO: add storing code here 
	} 
	else 
	{ 
		// TODO: add loading code here 
	} 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogDoc diagnostics 
 
#ifdef _DEBUG 
void CLogDoc::AssertValid() const 
{ 
	CDocument::AssertValid(); 
} 
 
void CLogDoc::Dump(CDumpContext& dc) const 
{ 
	CDocument::Dump(dc); 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CLogDoc commands 
 
CWrapBitmap* CLogDoc::GetWrapBitmap() 
{ 
	return &m_WrapBitmap; 
} 
 
BOOL CLogDoc::OnOpenDocument(LPCTSTR lpszPathName)  
{ 
	if (!CDocument::OnOpenDocument(lpszPathName)) 
		return FALSE; 
	 
	if(m_WrapBitmap.Load(lpszPathName)) 
		return TRUE; 
	else 
		return FALSE; 
} 
 
 
BOOL CLogDoc::OnSaveDocument(LPCTSTR lpszPathName)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	//return CDocument::OnSaveDocument(lpszPathName); 
	return m_WrapBitmap.Save(lpszPathName); 
} 
 
 
void CLogDoc::OnFilterLog()  
{ 
	StartFilter(TEMPLATE_LOG); 
} 
 
void CLogDoc::OnFilterLap()  
{ 
	StartFilter(TEMPLATE_LAPLACIAN); 
} 
 
void CLogDoc::OnFileSave()  
{ 
	m_WrapBitmap.Save("c:\\1.bmp");	 
} 
 
BOOL CLogDoc::StartFilter(const int nType) 
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
		return false; 
	 
	 
	CFilter filter; 
	if(!filter.Filter(nType,bmpBuffer,m_WrapBitmap.GetSize(),m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像滤波出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	// 
	} 
	TRACE("%ld",filter.GetCount()); 
	return true; 
} 
 
void CLogDoc::OnFilterGauss()  
{ 
	StartFilter(TEMPLATE_GAUSS);	 
} 
 
 
void CLogDoc::OnColorYuzhi()  
{ 
	m_WrapBitmap.YuZhi(125); 
	POSITION p=GetFirstViewPosition(); 
	CView* pView=GetNextView(p);//再打开一个view 
	pView->RedrawWindow(); 
 
} 
 
 
 
void CLogDoc::OnMorphDilate()  
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
	{ 
		TRACE("位图指针出错\n"); 
		return; 
	} 
		 
	CMorphology Morph; 
	if(!Morph.Dilate(bmpBuffer,NULL,SE_SQUARE,m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像形态学计算出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	//刷新 
	} 
 
} 
 
void CLogDoc::OnMorphErode()  
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
	{ 
		TRACE("位图指针出错\n"); 
		return; 
	} 
		 
	CMorphology Morph; 
	if(!Morph.Erode(bmpBuffer,NULL,SE_SQUARE,m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像形态学计算出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	//刷新 
	} 
 
	 
} 
 
void CLogDoc::OnMorphSkeleton()  
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
	{ 
		TRACE("位图指针出错\n"); 
		return; 
	} 
		 
	CMorphology Morph; 
	if(!Morph.Skeleton(bmpBuffer,SE_SQUARE,m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像形态学计算出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	//刷新 
	} 
	 
} 
 
void CLogDoc::OnMorphOpen()  
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
	{ 
		TRACE("位图指针出错\n"); 
		return; 
	} 
		 
	CMorphology Morph; 
	if(!Morph.Open(bmpBuffer,NULL,SE_SQUARE,m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像形态学计算出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	//刷新 
	} 
	 
} 
 
void CLogDoc::OnMorphThinning()  
{ 
	BYTE* bmpBuffer; 
	bmpBuffer=m_WrapBitmap.GetpBuffer(); 
	if(!bmpBuffer) 
	{ 
		TRACE("位图指针出错\n"); 
		return; 
	} 
		 
	CMorphology Morph; 
	if(!Morph.Thinning(bmpBuffer,m_WrapBitmap.GetWidthBytes(),m_WrapBitmap.GetHeight())) 
	{ 
		AfxMessageBox("图像形态学计算出错"); 
	} 
	else 
	{ 
		m_WrapBitmap.GetpBitmap()->SetBitmapBits(m_WrapBitmap.GetSize(),bmpBuffer); 
		POSITION p=GetFirstViewPosition(); 
		CView* pView=GetNextView(p);//再打开一个view 
		pView->RedrawWindow();	//刷新 
	} 
} 
 
void CLogDoc::OnColorShape()  
{ 
	 
}