www.pudn.com > FaceRecognition.rar > FaceRecognitionDlg.cpp


// FaceRecognitionDlg.cpp : implementation file 
// 
 
 
#include "stdafx.h" 
#include "FaceRecognition.h" 
#include "FaceRecognitionDlg.h" 
#include  
//#include  
 
#include  
#include  
#include  
#include  
#include  
#include  
 
 
 
#include "AddSampleDlg.h" 
 
#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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFaceRecognitionDlg dialog 
 
CFaceRecognitionDlg::CFaceRecognitionDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CFaceRecognitionDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CFaceRecognitionDlg) 
	m_result = _T(""); 
	m_aaa = _T(""); 
	m_jiancha = _T(""); 
	m_jiancha1 = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CFaceRecognitionDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFaceRecognitionDlg) 
	DDX_Text(pDX, IDC_RESULT, m_result); 
	DDX_Text(pDX, IDC_AVE, m_aaa); 
	DDX_Text(pDX, IDC_JIANCHA, m_jiancha); 
	DDX_Text(pDX, IDC_JIANCHA1, m_jiancha1); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CFaceRecognitionDlg, CDialog) 
	//{{AFX_MSG_MAP(CFaceRecognitionDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BTN_OPENFILE, OnBtnOpenfile) 
	ON_BN_CLICKED(IDC_EXTRA, OnExtra) 
	ON_BN_CLICKED(IDC_RECOGNITION, OnRecognition) 
	ON_BN_CLICKED(IDC_TRAIN_FIRST, OnTrainFirst) 
	ON_BN_CLICKED(IDC_REC_FIRST, OnRecFirst) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFaceRecognitionDlg message handlers 
 
BOOL CFaceRecognitionDlg::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 
		CWnd *pWnd0= GetDlgItem(IDC_BMPSHOW); 
	pDCShow = pWnd0->GetDC(); 
 
	m_pMainDib = new CDib(); 
 
 
	m_tOriPixelArray = NULL; 
 
	m_pResMap = NULL; 
 
	m_nWndWidth = 0; 
	m_nWndHeight= 0; 
	m_sFileName = ""; 
 
 
//    for(ii=0; ii<6; ii++) 
//	{ 
//	    for(jj=0; jjOpen(strFile); 
	 
	m_nWndWidth = m_pMainDib->GetWidth(); 
	m_nWndHeight= m_pMainDib->GetHeight(); 
	m_sFileName = strFile; 
 
 
	SetCursor(LoadCursor(NULL,IDC_WAIT)); 
	//获取像素的值 
	LoadOriPixel(m_pMainDib); 
	MakeBitMap(); 
 
    size = m_nWndHeight*m_nWndWidth; 
    if(FLAG == TRUE) 
	{ 
	    if(count_add=0; i--) 
			{ 
		        for(j=0; j=0; i--) 
			{ 
			    for(j=0; j=0; i--) 
		{ 
		    for(j=0; jm_pDibBits; 
	int byteBitCount  = pDib->GetBiBitCount()/8; 
 
	m_tOriPixelArray  = new RGBQUAD*[m_nWndHeight]; 
	m_tPix = new double*[m_nWndHeight]; 
	for(int l=0 ; l=0; i--) 
	{ 
		for(int j=0; jGetWindow()); 
	if(m_pResMap!=NULL) delete m_pResMap; 
 
	m_pResMap=new CBitmap(); 
	m_pResMap->CreateCompatibleBitmap(&ClientDC,m_nWndWidth,m_nWndHeight); 
 
	CDC  dc; 
	dc.CreateCompatibleDC(&ClientDC); 
	dc.SelectObject(m_pResMap); 
 
	for(int i=0; iGetWindow()->GetClientRect(&rc); 
	pDCShow->Rectangle(&rc); 
	CDC dc; 
	CBitmap *pOldBitmap; 
	dc.CreateCompatibleDC(pDCShow); 
	pOldBitmap=dc.SelectObject(m_pResMap); 
	pDCShow->StretchBlt(0,0,m_nWndWidth,m_nWndHeight,&dc,0,0,m_nWndWidth,m_nWndHeight,SRCCOPY); 
	dc.SelectObject(pOldBitmap); 
	dc.DeleteDC(); 
} 
 
 
void CFaceRecognitionDlg::OnExtra()  
{ 
	int l,k,i,j; 
	double matrix[num_train_data][IMG_SIZE]; 
	double matrix_t[IMG_SIZE][num_train_data]; 
	double matrix_line[IMG_SIZE*num_train_data]; 
	double matrix_eigen_value[num_train_data]; 
	double matrix_eigen_ventor[num_train_data][num_train_data]; 
	double matrix_low[num_train_data][num_train_data]; 
	double matrix_eigen[num_train_data][num_ventor_data]; 
	double ave_pca[IMG_SIZE]; 
	double add_pca[IMG_SIZE]; 
 
	for(i=0; i=0; i--) 
		{ 
		    for(j=0; jmax) 
			{ 
			    max = matrix_eigen_value[l+1]; 
			 
			} 
		    else 
			{ 
		    	max = matrix_eigen_value[l]; 
		    	double temp; 
		    	temp = matrix_eigen_value[l]; 
		    	matrix_eigen_value[l] = matrix_eigen_value[l+1]; 
		    	matrix_eigen_value[l+1] = temp; 
 
		    	for(k=0; k=0; i--) 
//	{ 
//		for(int j=0; j=0 && exp_count<5) 
	{ 
        MessageBox("主成分分析完毕,请输入下一组表情训练样本",NULL,MB_OK); 
		exp_count++; 
	} 
 
 
 
 
     
 
 
 
 
 
 
 
 
 
 
 
 
	 
} 
 
 
 
 
 
void CFaceRecognitionDlg::tred2(double a[num_train_data+1][num_train_data+1],int n,double d[num_train_data+1],double e[num_train_data+1]) 
{ 
	int i,j,k,l; 
	double h,scale1,f,g,hh,t; 
	if (n>1) 
	{ 
		for (i=n; i>=2; i--) 
		{ 
			l=i-1; 
		    h=0.0; 
		    scale1=0.0; 
		    if (l>1) 
			{ 
				for (k=1; k<=l; k++) 
				scale1=scale1+fabs(a[i][k]); 
				if (scale1==0.0) 
					e[i]=a[i][l]; 
				else 
				{ 
					for (k=1; k<=l; k++) 
					{ 
						a[i][k]=a[i][k]/scale1; 
					    h=h+a[i][k]*a[i][k]; 
					} 
					f=a[i][l]; 
				    if (f>0.0)  
						t=1; 
				    if (f==0.0) 
					    t=0; 
				    if(f<0.0) 
					    t=-1; 
				    g=-sqrt(h)*t; 
				    e[i]=scale1*g; 
				    h=h-f*g; 
				    a[i][l]=f-g; 
				    f=0.0; 
					for (j=1; j<=l; j++) 
					{ 
						a[j][i]=a[i][j]/h; 
					    g=0.0; 
						for(k=1; k<=j; k++) 
							g=g+a[j][k]*a[i][k]; 
						if(l>j) 
						{ 
							for (k=j+1; k<=l; k++) 
								g=g+a[k][j]*a[i][k]; 
						} 
						e[j]=g/h; 
						f=f+e[j]*a[i][j]; 
					} 
					hh=f/(h+h); 
					for (j=1; j<=l; j++) 
					{ 
						f=a[i][j]; 
					    g=e[j]-hh*f; 
					    e[j]=g; 
						for (k=1; k<=j; k++) 
							a[j][k]=a[j][k]-f*e[k]-g*a[i][k]; 
					} 
				} 
			} 
			else 
			{ 
				e[i]=a[i][l]; 
			} 
			d[i]=h; 
		} 
	} 
	d[1]=0.0; 
    e[1]=0.0; 
    for (i=1; i<=n; i++) 
	{ 
		l=i-1; 
		if (d[i]!=0.0) 
		{ 
			for (j=1; j<=l; j++) 
			{ 
				g=0.0; 
				for (k=1; k<=l; k++) 
				{				 
					g=g+a[i][k]*a[k][j]; 
				}			 
				for (k=1; k<=l; k++) 
				{				 
					a[k][j]=a[k][j]-g*a[k][i]; 
				} 
			} 
		}	 
		d[i]=a[i][i]; 
	    a[i][i]=1.0;	 
		if(l>=1) 
		{		 
			for (j=1; j<=l; j++) 
			{			 
				a[i][j]=0.0; 
			    a[j][i]=0.0; 
			} 
		} 
	} 
} 
void CFaceRecognitionDlg::tqli(double d[num_train_data+1],double e[num_train_data+1],int n,double z[num_train_data+1][num_train_data+1]) 
{ 
	int i,l,m,iter,k,t; 
	double dd,g,r,s,c,p,f,b; 
    if (n > 1) 
	{ 
        for (i = 2; i<=n; i++) 
		{ 
            e[i - 1] = e[i]; 
        } 
        e[n] = 0.0; 
        for (l = 1; l<=n; l++) 
		{ 
            iter = 0; 
loop1:          for (m = l; m<=n - 1; m++) 
				{ 
                dd = fabs(d[m]) + fabs(d[m + 1]); 
                if ((fabs(e[m]) + dd) == dd ) goto  loop2; 
				} 
				m = n; 
loop2:			if (m != l)  
				{ 
                if (iter == 30 ) 
				{ 
					CString gr; 
				gr.Format("d"); 
				AfxMessageBox(gr); 
				} 
                iter = iter + 1; 
                g = (d[l + 1] - d[l]) / (2.0 * e[l]); 
                r = sqrt(g * g + 1.0); 
				if (g>0) t=1; 
				if (g==0) t=0; 
				if (g<0) t=-1; 
                g = d[m] - d[l] + e[l] / (g + fabs(r) * t); 
                s = 1.0; 
                c = 1.0; 
                p = 0.0; 
                for (i = m - 1 ; i>=l; i--) 
				{ 
                    f = s * e[i]; 
                    b = c * e[i]; 
                    if (fabs(f) >= fabs(g)) 
					{ 
                        c = g / f; 
                        r = sqrt(c *c + 1.0); 
                        e[i + 1] = f * r; 
                        s = 1.0 / r; 
                        c = c * s; 
					} 
                    else 
					{ 
                        s = f / g; 
                        r =sqrt(s *s + 1.0); 
                        e[i + 1] = g * r; 
                        c = 1.0 / r; 
						s=s*c; 
                    } 
                    g = d[i + 1] - p; 
                    r = (d[i] - g) * s + 2.0 * c * b; 
                    p = s * r; 
                    d[i + 1] = g + p; 
                    g = c * r - b; 
                    //omit lines from here ... 
                    for (k = 1 ;k<= n;k++) 
					{ 
                        f = z[k][i + 1]; 
                        z[k][i + 1] = s * z[k][i] + c * f; 
                        z[k][i] = c * z[k][i] - s * f; 
                    } 
                   //to here when finding only eigenvalues. 
                } 
                d[l] = d[l] - p; 
                e[l] = g; 
                e[m] = 0.0; 
                goto loop1; 
				} 
		} 
    } 
} 
 
 
void CFaceRecognitionDlg::OnRecognition()  
{ 
 
	int i,j,l,k; 
 
	if(FLAG == FALSE) 
	{ 
	    char string[1000]; 
	    fstream myfile; 
	    myfile.open("c:\\FaceRecognition\\data_save",ios::in); 
     
		k = 0; 
		j = 0; 
/*		while(!myfile.eof()) 
		{ 
			if(k<6*IMG_SIZE*num_ventor_data) 
			{ 
			    myfile.getline(string,sizeof(string)); 
			    save1[k] = atof(string); 
        	    m_jiancha.Format("%f",save1[0]); 
			    k++; 
				myfile.getline(string,1000); 
            } 
			else 
			{ 
				myfile.getline(string,sizeof(string)); 
				save2[j] = atof(string); 
			  	m_jiancha1.Format("%f",save2[1]); 
				j++; 
			} 
 
		}*/ 
		for(k=0; k<6*IMG_SIZE*num_ventor_data + 6*num_ventor_data; k++) 
		{ 
			myfile.getline(string,sizeof(string)); 
			if(k >= 6*IMG_SIZE*num_ventor_data && k<6*IMG_SIZE*num_ventor_data + 6*num_ventor_data) 
			{ 
				save2[j] = atof(string); 
			  	m_jiancha1.Format("%f",save2[0]); 
				j++; 
 
 
			} 
			else 
			{ 
			    save1[k] = atof(string); 
        	    m_jiancha.Format("%f",save1[1]); 
 
			} 
		} 
 
 
 
 
 
 
		myfile.close(); 
 
    	k = 0; 
    	for(l=0; l<6; l++) 
		{ 
    		for(i=0; i