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

📄 palmprintdlg.cpp

📁 基于傅里叶变换的掌纹识别!该方法的基本思想是先将掌纹图像应用傅立叶变换转换到频域,然后在频域中进行特征提取和描述.提取出来的特征备用来索引掌纹数据库,以便当一个新的掌纹图像被输入时,可以很快确定该手掌
💻 CPP
字号:
// PalmPrintDlg.cpp : implementation file
//

#include "stdafx.h"
#include "PalmPrint.h"
#include "PalmPrintDlg.h"
#include "dibapi.h"
#include "math.h"
#include <complex>
#define PI 3.14159
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPalmPrintDlg dialog

CPalmPrintDlg::CPalmPrintDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPalmPrintDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPalmPrintDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
	m_hDIB = NULL;
	
	m_palDIB = NULL;
}

void CPalmPrintDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPalmPrintDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPalmPrintDlg, CDialog)
	//{{AFX_MSG_MAP(CPalmPrintDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPalmPrintDlg message handlers

BOOL CPalmPrintDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPalmPrintDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CPalmPrintDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPalmPrintDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPalmPrintDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	CFile file;
	CFileException fe;

	char  szFilter[] = "Bitmap Files (*.bmp)|*.bmp|All Files (*.*)|*.*||";
	CFileDialog fdlg(true, "BMP", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
				szFilter);
	if(fdlg.DoModal() != IDOK)
		return;	

	CString lpszPathName = fdlg.GetPathName();
	// 打开文件
	if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
	{
		
		return;
	}

		
	// 更改光标形状
	BeginWaitCursor();
	
	// 尝试调用ReadDIBFile()读取图像
	TRY
	{
		m_hDIB = ::ReadDIBFile(file);
	

	}
	CATCH (CFileException, eLoad)
	{
		// 读取失败
		file.Abort();
		
		// 恢复光标形状
		EndWaitCursor();
		
		// 设置DIB为空
		m_hDIB = NULL;
		return;
	}
	END_CATCH
	
	// 初始化DIB
	::InitDIBData(m_hDIB,m_palDIB);
	
	// 恢复光标形状
	EndWaitCursor();
	
	// 判断读取文件是否成功
	if (m_hDIB == NULL)
	{
		// 失败,可能非BMP格式
		CString strMsg;
		strMsg = "读取图像时出错!可能是不支持该类型的图像文件!";
		
		// 提示出错
		MessageBox(strMsg, "系统提示", MB_ICONINFORMATION | MB_OK);
		
		// 返回FALSE
		return;
	}
// 	m_hDIBOrigin = (HDIB)CopyHandle((HANDLE)m_hDIB);
	DrawBMP(IDC_STATIC,m_hDIB);//绘制原图像于控件IDC_PIC之上
//	DrawBMP(IDC_PIC2,m_hDIBOrigin);	//绘制图像于控件IDC_PIC2之上
	// TODO: Add your control notification handler code here
}

void CPalmPrintDlg::DrawBMP(int nID, HDIB ndib)
{
	BeginWaitCursor();

	// 获取DIB
	HDIB hDIB = ndib;
	
	// 判断DIB是否为空
	if (hDIB != NULL)
	{
		LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
		
		// 获取DIB宽度
		int cxDIB = (int) ::DIBWidth(lpDIB);
		
		// 获取DIB高度
		int cyDIB = (int) ::DIBHeight(lpDIB);
		
		::GlobalUnlock((HGLOBAL) hDIB);
		
		CRect rcDIB;
		rcDIB.top = rcDIB.left = 0;
		rcDIB.right = cxDIB;
		rcDIB.bottom = cyDIB;
		
		CWnd* pWnd=GetDlgItem(nID);
		CDC* pDC=pWnd->GetDC();
		RECT rect;
		pWnd->GetClientRect(&rect);//得到控件矩形区域
		CRect rcDest(rect);
		
		// 输出DIB
		::PaintDIB(pDC->m_hDC, &rcDest, hDIB,&rcDIB,m_palDIB);
	}
	// 恢复正常光标
	EndWaitCursor();	
}







void CPalmPrintDlg::OnButton3() 
{
	// TODO: Add your control notification handler code here
		// 图像付立叶变换
	
	// 指向DIB的指针
	LPSTR	lpDIB;
	
	// 指向DIB象素指针
	LPSTR    lpDIBBits;
	
	// 锁定DIB
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)m_hDIB);
	
	// 找到DIB图像象素起始位置
	lpDIBBits = ::FindDIBBits(lpDIB);
	
	// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的付立叶变换,其它的可以类推)
	if (::DIBNumColors(lpDIB) != 256)
	{
		// 提示用户
		MessageBox("目前只支持256色位图的付立叶变换!", "系统提示" ,
			MB_ICONINFORMATION | MB_OK);
		
		// 解除锁定
		::GlobalUnlock((HGLOBAL)m_hDIB);
		
		// 返回
		return;
	}
	
	// 更改光标形状
	BeginWaitCursor();
	
	// 调用Fourier()函数进行付立叶变换
	if (::Fourier(lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB)))
	{
		
		// 设置脏标记
//		pDoc->SetModifiedFlag(TRUE);
		
		// 更新视图
//		pDoc->UpdateAllViews(NULL);
	}
	else
	{
		// 提示用户
		MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
	}
	
	// 解除锁定
	::GlobalUnlock((HGLOBAL)m_hDIB);
	
	// 恢复光标
	EndWaitCursor();

	DrawBMP(IDC_STATIC,m_hDIB);//绘制原图像于控件IDC_PIC之上

}

void CPalmPrintDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
		int h , w ;
	int Gmin = 255;
	int Gmax = 0;
	double t ;
	// 图像每行的字节数
	LONG	lLineBytes;

	// 指向DIB的指针
	LPSTR	lpDIB;
	
	// 指向DIB象素指针
	LPSTR    lpDIBBits;
	
	// 锁定DIB
	lpDIB = (LPSTR) ::GlobalLock((HGLOBAL)m_hDIB);
	
	// 找到DIB图像象素起始位置
	lpDIBBits = ::FindDIBBits(lpDIB);
	// 计算图像每行的字节数
	lLineBytes = WIDTHBYTES(::DIBWidth(lpDIB) * 8);

	for (h=0; h<::DIBHeight(lpDIB); h++)
	{
		for (w=0; w<::DIBWidth(lpDIB); w++)
		{
			if(lpDIBBits[h*lLineBytes+w] > Gmax)
				Gmax = lpDIBBits[h*lLineBytes+w];
			if(lpDIBBits[h*lLineBytes+w] < Gmin)
				Gmin = lpDIBBits[h*lLineBytes+w];
		}
	}

	for (h=0; h<::DIBHeight(lpDIB); h++)
	{
		for (w=0; w<::DIBWidth(lpDIB); w++)
		{
			t = (double)(lpDIBBits[h*lLineBytes+w]-Gmin)/(double)(Gmax-Gmin);
			lpDIBBits[h*lLineBytes+w] = (BYTE)(t*255);
		}
	}/**/	
	DrawBMP(IDC_STATIC,m_hDIB);//绘制原图像于控件IDC_PIC之上

}

⌨️ 快捷键说明

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