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

📄 facialfeaturedlg.cpp

📁 是一个人脸识别特征提取的Vc代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// FacialFeatureDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FacialFeature.h"
#include "FacialFeatureDlg.h"
#include "ReplaceDlg.h"
#include "AddSampleDlg.h"

////////////////////////////adaboost
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <malloc.h>

#define ORIG_WIN_SIZE  24
static CvMemStorage* storage = 0;
static CvHidHaarClassifierCascade* hid_cascade = 0;
#define WINNAME  "Result"

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

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

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()

/////////////////////////////////////////////////////////////////////////////
// CFacialFeatureDlg dialog

CFacialFeatureDlg::CFacialFeatureDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFacialFeatureDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFacialFeatureDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFacialFeatureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFacialFeatureDlg)
	DDX_Control(pDX, IDC_BMPSHOW, m_sizeofarea);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFacialFeatureDlg, CDialog)
	//{{AFX_MSG_MAP(CFacialFeatureDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_OPENFILE, OnBtnOpenfile)
	ON_BN_CLICKED(IDC_FACE_DETECT, OnFaceDetect)
	ON_BN_CLICKED(IDC_EYE_CENTER, OnEyeCenter)
	ON_BN_CLICKED(IDC_MOUTH_CENTER, OnMouthCenter)
	ON_BN_CLICKED(IDC_NOSE_CENTER, OnNoseCenter)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFacialFeatureDlg message handlers

BOOL CFacialFeatureDlg::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
	//////////////////// by zzl
	m_pMainDibBmp = new CDib();
	image   = NULL;
	grayimg = NULL;
	edgeimg = NULL;
	yuvimg  = NULL;
	m_nBmpWidth = 0;
	m_nBmpHeigh = 0;
	m_sBmpName = "";
	m_tPixelOfOriBmpArray = NULL;
	m_tPixelOfRefBmpArray = NULL;
	m_pRefMap = NULL;


	CWnd *pWnd0 = GetDlgItem(IDC_BMPSHOW);
	pDCShow = pWnd0->GetDC();
	m_bFaceOK = false;
	m_bShowFace = false;

	m_rFaceArea = CRect(0,0,0,0);
	m_rAdaFaceArea = CRect(0,0,0,0);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFacialFeatureDlg::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 CFacialFeatureDlg::OnPaint() 
{
/*
	CWnd* pWnd = GetDlgItem(IDC_BMPSHOW);
	CDC* pControlDC = pWnd->GetDC();
	pWnd->Invalidate();
	pWnd->UpdateWindow();
	pControlDC->SelectStockObject(BLACK_BRUSH);
	pControlDC->Rectangle(0,0,100,100);
*/
	

	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);
		if (m_tPixelOfRefBmpArray == NULL) return;
	}
	else
	{
		CDialog::OnPaint();
	}
}

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

void CFacialFeatureDlg::OnBtnOpenfile() 
{
	// TODO: Add your control notification handler code here
	CAddSampleDlg FileDlg(TRUE, "", NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT,
		"Color Face Image(*.bmp)|*.bmp|所有文件(*.*)|*.*||",
		AfxGetMainWnd());
	CString strFile;
	if (FileDlg.DoModal() != IDOK)
		return;
	POSITION pos = FileDlg.GetStartPosition();
	strFile = FileDlg.GetNextPathName(pos);
	m_pMainDibBmp->Open(strFile);
	m_nBmpWidth = m_pMainDibBmp->GetWidth();
	m_nBmpHeigh = m_pMainDibBmp->GetHeight();
	m_sBmpName = strFile;
	m_rFaceArea.left = 0;
	m_rFaceArea.right = 0;
	m_rFaceArea.top = 0;
	m_rFaceArea.bottom = 0;
	m_rAdaFaceArea = CRect(0,0,0,0);

	m_bLeftEyeOK = m_bRightEyeOK = m_bLeftNostrilOK = m_bRightNostrilOK =
	m_bLeftEyeLeftCornerOK = m_bLeftEyeRightCornerOK = m_bRightEyeLeftCornerOK = 
	m_bRightEyeRightCornerOK = m_bLeftMouthCornerOK = m_bRightMouthCornerOK = false;

	m_bMidMouthOK = m_bMidNoseOK = false;

	m_LeftEye = m_RightEye = m_LeftEyeLeftCorner = m_LeftEyeRightCorner = 
	m_LeftNostril = m_RightNostril = m_RightEyeLeftCorner = m_RightEyeRightCorner =
	m_LeftMouthCorner = m_RightMouthCorner = m_MidMouth = m_MidNose = CPoint(-1,-1);

	SetCursor(LoadCursor(NULL, IDC_WAIT));
	LoadPixelOfBmp(m_pMainDibBmp);
	CreateBitMap();
	SetCursor(LoadCursor(NULL, IDC_ARROW));
	
}

void CFacialFeatureDlg::LoadPixelOfBmp(CDib *pDib)
{
	BYTE *colorTable;
	colorTable = (BYTE *)pDib->m_pDibBits;
	int byteBitCount = pDib->GetBiBitCount()/8;

	m_tPixelOfOriBmpArray = new RGBQUAD*[m_nBmpHeigh];
	m_tPixelOfRefBmpArray = new RGBQUAD*[m_nBmpHeigh];

	for (int i = 0; i<m_nBmpHeigh; i++)
	{
		m_tPixelOfOriBmpArray[i] = new RGBQUAD[m_nBmpWidth];
		m_tPixelOfRefBmpArray[i] = new RGBQUAD[m_nBmpWidth];
	}

	int count = 0;
	for (int m=m_nBmpHeigh - 1; m>=0 ; m--)
	{
		for(int n = 0; n<m_nBmpWidth; n++)
		{
			m_tPixelOfOriBmpArray[m][n].rgbBlue = colorTable[count++];
			m_tPixelOfOriBmpArray[m][n].rgbGreen = colorTable[count++];
			m_tPixelOfOriBmpArray[m][n].rgbRed = colorTable[count++];
			m_tPixelOfRefBmpArray[m][n] = m_tPixelOfOriBmpArray[m][n];
			m_tPixelOfOriBmpArray[m][n].rgbReserved = 0;
			count += byteBitCount - 3;
		}
		count += (4-(m_nBmpWidth * byteBitCount)%4)%4;
	}
}

void CFacialFeatureDlg::CreateBitMap()
{
	CClientDC ClientDC(pDCShow->GetWindow());
	if (m_pRefMap != NULL)
		delete m_pRefMap;
	m_pRefMap = new CBitmap();
	m_pRefMap->CreateCompatibleBitmap(&ClientDC, m_nBmpWidth, m_nBmpHeigh);

	CDC dc;
	dc.CreateCompatibleDC(&ClientDC);
	dc.SelectObject(m_pRefMap);
	for (int i=0; i<m_nBmpHeigh; i++)
		for (int j=0; j<m_nBmpWidth; j++)
			dc.SetPixelV(j,i,RGB(m_tPixelOfRefBmpArray[i][j].rgbRed,m_tPixelOfRefBmpArray[i][j].rgbGreen,m_tPixelOfRefBmpArray[i][j].rgbBlue));
	if (m_bFaceOK && m_bShowFace)
	{
		CBrush Pen;
		Pen.CreateSolidBrush(RGB(255,255,0));
		dc.FrameRect(m_rAdaFaceArea, &Pen);
		Pen.DeleteObject();
	}

	if(m_bLeftEyeOK)					DrawCross(&dc,m_LeftEye,RGB(255,0,0));
	if(m_bRightEyeOK)					DrawCross(&dc,m_RightEye,RGB(255,0,0));
	if(m_bLeftEyeLeftCornerOK)			DrawCross(&dc,m_LeftEyeLeftCorner,RGB(255,0,255));
	if(m_bLeftEyeRightCornerOK)			DrawCross(&dc,m_LeftEyeRightCorner,RGB(255,255,0));
	if(m_bRightEyeLeftCornerOK)			DrawCross(&dc,m_RightEyeLeftCorner,RGB(255,0,255));
	if(m_bRightEyeRightCornerOK)		DrawCross(&dc,m_RightEyeRightCorner,RGB(255,255,0));
	if(m_bLeftNostrilOK)				DrawCross(&dc,m_LeftNostril,RGB(0,255,0));
	if(m_bRightNostrilOK)				DrawCross(&dc,m_RightNostril,RGB(0,255,0));
	if(m_bMidNoseOK)					DrawCross(&dc,m_MidNose,RGB(0,255,0));
	if(m_bLeftMouthCornerOK)			DrawCross(&dc,m_LeftMouthCorner,RGB(0,0,255));
	if(m_bRightMouthCornerOK)			DrawCross(&dc,m_RightMouthCorner,RGB(0,0,255));
	if(m_bMidMouthOK)					DrawCross(&dc,m_MidMouth,RGB(0,0,255));

	dc.DeleteDC();
	MyDraw();

}

void CFacialFeatureDlg::MyDraw()
{
	CRect rc;
	pDCShow->GetWindow()->GetClientRect(&rc);
	pDCShow->Rectangle(&rc);
	CDC dc;
	CBitmap *pOldBitmap;
	dc.CreateCompatibleDC(pDCShow);
	pOldBitmap = dc.SelectObject(m_pRefMap);
	pDCShow->StretchBlt(0,0,m_nBmpWidth,m_nBmpHeigh,&dc,0,0,m_nBmpWidth,m_nBmpHeigh,SRCCOPY);
	dc.SelectObject(pOldBitmap);
	dc.DeleteDC();

}


bool CFacialFeatureDlg::CopyBitMap(RGBQUAD **dest, RGBQUAD **source)
{
	if(source==NULL || dest==NULL) 
		return false;
	for(int i=0; i<m_nBmpHeigh; i++)
		for(int j=0; j<m_nBmpWidth; j++)
			dest[i][j]=source[i][j];
	

⌨️ 快捷键说明

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