⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 earview.cpp

📁 人耳识别技术是20世纪90年代末开始兴起的一种生物特征识别技术
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// EARView.cpp : implementation of the CEARView class
//

#include "stdafx.h"
#include "EAR.h"

#include "EARDoc.h"
#include "EARView.h"

//添加头文件
#include "MainFrm.h"
#include "IntensityDlg.h"		// 直方图
#include "LinerParaDlg.h"		// 线性变换
#include "PointThreDlg.h"		// 阈值变换
#include "PointWinDlg.h"		// 窗口变换
#include "PointStreDlg.h"		// 灰度拉伸

#include "SmoothDlg.h"			// 图像平滑
#include "MidFilterDlg.h"		// 中值滤波
#include "SharpThreDlg.h"		// 图像梯度锐化

#include "MorphErosionDlg.h"	// 图像腐蚀
#include "MorphDilationDlg.h"	// 图像膨胀
#include "MorphOpenDlg.h"		// 开运算
#include "MorphCloseDlg.h"		// 闭运算

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CEARView

IMPLEMENT_DYNCREATE(CEARView, CScrollView)

BEGIN_MESSAGE_MAP(CEARView, CScrollView)
	//{{AFX_MSG_MAP(CEARView)
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
	ON_COMMAND(ID_VEIW_INTENSITY, OnVeiwIntensity)
	ON_COMMAND(ID_POINT_INVERT, OnPointInvert)
	ON_COMMAND(ID_POINT_LINER, OnPointLiner)
	ON_COMMAND(ID_POINT_THRE, OnPointThre)
	ON_COMMAND(ID_POINT_WIND, OnPointWind)
	ON_COMMAND(ID_POINT_STRE, OnPointStre)
	ON_COMMAND(ID_POINT_EQUA, OnPointEqua)
	ON_COMMAND(ID_ENHA_SMOOTH, OnEnhaSmooth)
	ON_COMMAND(ID_ENHA_GRADSHARP, OnEnhaGradsharp)
	ON_COMMAND(ID_ENHA_MIDIANF, OnEnhaMidianF)
	ON_COMMAND(ID_ENHA_SHARP, OnEnhaSharp)
	ON_COMMAND(ID_MORPH_EROSION, OnMorphErosion)
	ON_COMMAND(ID_MORPH_DILATION, OnMorphDilation)
	ON_COMMAND(ID_MORPH_OPEN, OnMorphOpen)
	ON_COMMAND(ID_MORPH_CLOSE, OnMorphClose)
	ON_COMMAND(ID_EDGE_SOBEL, OnEdgeSobel)
	ON_COMMAND(ID_EDGE_KIRSCH, OnEdgeKirsch)
	ON_COMMAND(ID_EDGE_CONTOUR, OnEdgeContour)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEARView construction/destruction

CEARView::CEARView()
{
	// TODO: add construction code here

}

CEARView::~CEARView()
{
}

BOOL CEARView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CEARView drawing

void CEARView::OnDraw(CDC* pDC)
{
	// TODO: add draw code for native data here
	BeginWaitCursor();

	CEARDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	HDIB hDIB = pDoc->GetHDIB();
	if (hDIB != NULL)
	{
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		int cxDIB = (int) pDoc->GetDibImage()->DIBWidth(lpDIB);
		int cyDIB = (int) pDoc->GetDibImage()->DIBHeight(lpDIB);

		::GlobalUnlock((HGLOBAL) hDIB);
		
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		
		CRect rcDest;
		
		if (pDC->IsPrinting())
		{
			// 是打印,计算输出图像的位置和大小,以便符合页面
			
			// 获取打印页面的水平宽度(象素)
			int cxPage = pDC->GetDeviceCaps(HORZRES);			
			// 获取打印页面的垂直高度(象素)
			int cyPage = pDC->GetDeviceCaps(VERTRES);			
			// 获取打印机每英寸象素数
			int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
			int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
			
			// 计算打印图像大小(缩放,根据页面宽度调整图像大小)
			rcDest.top = rcDest.left = 0;
			rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
					/ ((double)cxDIB * cxInch));
			rcDest.right = cxPage;
			
			// 计算打印图像位置(垂直居中)
			int temp = cyPage - (rcDest.bottom - rcDest.top);
			rcDest.bottom += temp/2;
			rcDest.top += temp/2;
		}
		else   
		{
			// 不必缩放图像
			rcDest = rcDIB;
		}
		
		pDoc->GetDibImage()->PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
			&rcDIB, pDoc->GetDocPalette());
	}
	
	EndWaitCursor();
}

void CEARView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	// TODO: calculate the total size of this view
}

/////////////////////////////////////////////////////////////////////////////
// CEARView printing

BOOL CEARView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	// 设置总页数为一
	pInfo->SetMaxPage(1);
	return DoPreparePrinting(pInfo);
}

void CEARView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CEARView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CEARView diagnostics

#ifdef _DEBUG
void CEARView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CEARView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CEARDoc* CEARView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEARDoc)));
	return (CEARDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CEARView message handlers

LRESULT CEARView::OnDoRealize(WPARAM wParam, LPARAM)
{
	ASSERT(wParam != NULL);

	CEARDoc* pDoc = GetDocument();	
	if (pDoc->GetHDIB() == NULL)
	{
		return 0L;
	}
	
	CPalette* pPal = pDoc->GetDocPalette();
	if (pPal != NULL)
	{
		CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
		ASSERT_KINDOF(CMainFrame, pAppFrame);
		
		CClientDC appDC(pAppFrame);

		// All views but one should be a background palette.
		// wParam contains a handle to the active view, so the SelectPalette
		// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
		CPalette* oldPalette = appDC.SelectPalette(pPal,((HWND)wParam)!=m_hWnd);	
		if (oldPalette != NULL)
		{
			UINT nColorsChanged = appDC.RealizePalette();
			if (nColorsChanged > 0)
			{
				pDoc->UpdateAllViews(NULL);
			}
			appDC.SelectPalette(oldPalette, TRUE);
		}
		else
		{
			TRACE0("\tCCh1_1View::OnPaletteChanged中调用SelectPalette()失败!\n");
		}
	}
	
	return 0L;
}

BOOL CEARView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	// 主要是为了设置子窗体默认的背景色
	// 背景色由文档成员变量m_refColorBKG指定

	CEARDoc* pDoc = GetDocument();
	CBrush brush(pDoc->m_refColorBKG);                                              
	CBrush* pOldBrush = pDC->SelectObject(&brush);
		
	CRect rectClip;
	pDC->GetClipBox(&rectClip);			// 获取重绘区域	
	pDC->PatBlt(rectClip.left, rectClip.top, rectClip.Width(), 
		rectClip.Height(), PATCOPY);

	pDC->SelectObject(pOldBrush);                                                  

	return TRUE;
}

void CEARView::OnEditCopy() 
{
	// TODO: Add your command handler code here
	// 复制当前图像

	CEARDoc* pDoc = GetDocument();
	
	if (OpenClipboard())
	{
		BeginWaitCursor();
		EmptyClipboard();
		
		// 复制当前图像到剪贴板
		SetClipboardData (CF_DIB, pDoc->GetDibImage()->
			CopyHandle((HANDLE) pDoc->GetHDIB()) );

		CloseClipboard();
		EndWaitCursor();
	}		
}

void CEARView::OnUpdateEditCopy(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	// 如果当前DIB对象不空,复制菜单项有效
	pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}

void CEARView::OnEditPaste() 
{
	// TODO: Add your command handler code here
	// 粘贴图像
	
	HDIB hNewDIB = NULL;
	CEARDoc* pDoc = GetDocument();
	
	if (OpenClipboard())
	{
		BeginWaitCursor();

		// 读取剪贴板中的图像
		hNewDIB = (HDIB)pDoc->GetDibImage()->CopyHandle(::GetClipboardData(CF_DIB));

		CloseClipboard();
		
		if (hNewDIB != NULL)
		{
			pDoc->ReplaceHDIB(hNewDIB);		// 替换DIB,同时释放旧DIB对象		
			pDoc->InitDIBData();			// 更新DIB大小和调色板		
			pDoc->SetModifiedFlag(TRUE);	// 设置脏标记	
			SetScrollSizes(MM_TEXT, pDoc->GetDocSize());	// 重新设置滚动视图大小		
			OnDoRealize((WPARAM)m_hWnd,0);	// 实现新的调色板		
			pDoc->UpdateAllViews(NULL);		// 更新视图
		}

		EndWaitCursor();
	}
}

void CEARView::OnUpdateEditPaste(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	// 如果当前剪贴板中有DIB对象,粘贴菜单项有效
	pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));	
}

void CEARView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
{
	// TODO: Add your specialized code here and/or call the base class
	CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
	
	if (bActivate)
	{
		ASSERT(pActivateView == this);
		OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE);
	}
}

void CEARView::CalcWindowRect(LPRECT lpClientRect, UINT nAdjustType) 
{
	// TODO: Add your specialized code here and/or call the base class
	CScrollView::OnInitialUpdate();
	ASSERT(GetDocument() != NULL);
	
	SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}

void CEARView::OnVeiwIntensity() 
{
	// TODO: Add your command handler code here
	// 查看当前图像灰度直方图

	CEARDoc* pDoc = GetDocument();
	
	LPSTR lpDIB;			// 指向DIB的指针	
	LPSTR    lpDIBBits;		// 指向DIB象素指针
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());	
	// 找到DIB图像象素起始位置
	lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图,其它的可以类推)
	if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
	{
		MessageBox("目前只支持查看256色位图灰度直方图!", "系统提示" , 
			MB_ICONINFORMATION | MB_OK);
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
		
		return;
	}
	
	BeginWaitCursor();

	CIntensityDlg dlgPara;
	dlgPara.m_lpDIBBits = lpDIBBits;
	dlgPara.m_lWidth = pDoc->GetDibImage()->DIBWidth(lpDIB);
	dlgPara.m_lHeight = pDoc->GetDibImage()->DIBHeight(lpDIB);
	dlgPara.m_iLowGray = 0;
	dlgPara.m_iUpGray = 255;
	
	// 显示对话框,提示用户设定平移量
	if (dlgPara.DoModal() != IDOK)
	{
		return;
	}

	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
	EndWaitCursor();	
}

void CEARView::OnPointInvert() 
{
	// TODO: Add your command handler code here
	// 图像反色
	
	CEARDoc* pDoc = GetDocument();
	
	LPSTR lpDIB;			// 指向DIB的指针	
	LPSTR    lpDIBBits;		// 指向DIB象素指针	
	FLOAT fA;				// 线性变换的斜率	
	FLOAT fB;				// 线性变换的截距	
	// 反色操作的线性变换的方程是-x + 255
	fA = -1.0;
	fB = 255.0;
	
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
	// 找到DIB图像象素起始位置
	lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的反色,其它的可以类推)
	if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
	{
		MessageBox("目前只支持256色位图的反色!", "系统提示" , 
			MB_ICONINFORMATION | MB_OK);
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
		
		return;
	}
	
	BeginWaitCursor();
	
	// 调用LinerTrans()函数反色
	pDoc->GetDibImage()->LinerTrans(lpDIBBits,pDoc->GetDibImage()->DIBWidth(lpDIB), 
		pDoc->GetDibImage()->DIBHeight(lpDIB), fA, fB);
	
	pDoc->SetModifiedFlag(TRUE);	// 设置脏标记	
	pDoc->UpdateAllViews(NULL);		// 更新视图
	
	::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
	EndWaitCursor();	
}

void CEARView::OnPointLiner() 
{
	// TODO: Add your command handler code here
	// 线性变换

	CEARDoc* pDoc = GetDocument();
		
	LPSTR lpDIB;				// 指向DIB的指针	
	LPSTR    lpDIBBits;			// 指向DIB象素指针	
	CLinerParaDlg dlgPara;		// 创建对话框	
	FLOAT fA;					// 线性变换的斜率	
	FLOAT fB;					// 线性变换的截距
	
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB());
	// 找到DIB图像象素起始位置
	lpDIBBits = pDoc->GetDibImage()->FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的线性变换,其它的可以类推)
	if (pDoc->GetDibImage()->DIBNumColors(lpDIB) != 256)
	{
		MessageBox("目前只支持256色位图的线性变换!", "系统提示" , 
			MB_ICONINFORMATION | MB_OK);
		::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());

		return;
	}
	
	dlgPara.m_fA = 2.0;
	dlgPara.m_fB = -128.0;
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -