#include "PCA.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include <windows.h>
#include <winuser.h>
#include <commdlg.h>
#define thamsotontaianh 365000000
#define MAX_LOADSTRING 100
using namespace std;
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// MyGlobal Variables
typedef struct tagMYCOLOR {
BYTE R;
BYTE G;
BYTE B;
} MYCOLOR;
MYCOLOR **data;
FILE *stream;
int n, m, i, j, k, k2;
//float **data, **symmat,
float **symmat2, *evals, *interm;
float *dis;
float in_value;
char option;
// variable xu ly
float **dataSrc,**luu;
float *mean;
float *ve_im_test;
int daianh;
int Hind,Wind;
float *tem;
float **eigenface;
float **symmat;//, **symmat2, *evals, *inter;
float temp1=0;
float ** a1;
float **proimage;
MYCOLOR **Alloc2d(int m, int n)
{
MYCOLOR **aa;
MYCOLOR *a;
int i;
aa = (MYCOLOR **)calloc(m,sizeof(MYCOLOR *));
a = (MYCOLOR *)calloc(m*n,sizeof(MYCOLOR));
for (i=0; i<m; i++)
aa[i] = a + i*n;
return aa;
}
void Free2d(MYCOLOR **aa)
{
free(*aa);
free(aa);
return;
}
BYTE **Alloc2dByte(int m, int n)
{
BYTE **aa;
BYTE *a;
int i;
aa = (BYTE **)calloc(m,sizeof(BYTE *));
a = (BYTE *)calloc(m*n,sizeof(BYTE));
for (i=0; i<m; i++)
aa[i] = a + i*n;
return aa;
}
void Free2dByte(BYTE **aa)
{
free(*aa);
free(aa);
return;
}
float **Alloc2dFloat(int m, int n)
{
float **aa;
float *a;
int i;
aa = (float **)calloc(m,sizeof(float *));
a = (float *)calloc(m*n,sizeof(float));
for (i=0; i<m; i++)
aa[i] = a + i*n;
return aa;
}
void Free2dFloat(float **aa)
{
free(*aa);
free(aa);
return;
}
float *vector(int n)
/* Allocates a float vector with range [1..n]. */
{
float *v;
v = (float *)calloc(n,sizeof(float));
return v;
}
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void ReadPPM(HWND hWnd);
MYCOLOR **Alloc2d(int m, int n);
void Free2d(MYCOLOR **aa);
void ReadImage(HWND hWnd);
void training();
void xuly( float **dataN, HDC hdc,HWND hWnd, int n ,int m);
void xuat(float so,int x,int y,HDC hdc);
float **matrix(int n, int m);
void corcol(float **data, int n, int m, float **symmat);
void covcol(float **data, int n, int m, float **symmat);
void scpcol(float **data, int n, int m, float **symmat);
void free_vector(float *v,int n);
void free_matrix(float **mat,int n,int m);
void erhand(char *err_msg,HWND hWnd);
void xuly1( float **dataN, int n ,int m);
void tinh_eigenface(float **dataSrc, float **symmat, int n,int m,float **eigenface);
void chieu(float **eigenface, float **dataSrc,float **proimage,int n,int m);
void chieu_anhtest(float **eigenface,float *ve_im_test,float **proimage,int n,int m);
float NguongMax(float **proimage,int m);
int sosanh(float **proimage,int m);
void sapxep_vector(float **symmat,int n,int m);
void ghifile2(FILE *fp,int m,float *dulieu);
void ghifile(FILE *fp,int n,int m,float **dulieu);
int *knn(float **proimage,int n,int m);
void Huy();
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE,szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DOCANH, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DOCANH);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DOCANH);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_DOCANH;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass,TEXT ("Nhan Dang")/* szTitle*/,SW_MAXIMIZE| WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
// HWND hWnd;
char fsFile[999];
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case ID_RECOGNIZE_KETQUA:
ReadPPM(hWnd);
//DeleteObject(hWnd);
// DeleteObject(hdc);
break;
case ID_FILE_TRAINING:
training();
//TextOut(hdc,400,100,"Quá trình huấn luyện hoàn thành", ARRAYSIZE("Quá trình huấn luyện hoành thành"));
MessageBox(NULL, "hoan thanh", "Finish", MB_OK);
break;
case ID_RECOGNIZE_KHONGGIANVECTOCUACHUSO:
// khong gian vector
break;
case ID_RECOGNIZE_ANHTRUNGBINH:
MessageBox(NULL, "Anh trung binh(qui tam)", "Finish", MB_OK);
break;
case ID_OPEN:
ReadPPM(hWnd);
//TextOut(hdc,400,100,"Quá trình huấn luyện hoàn thành", ARRAYSIZE("Quá trình huấn luyện hoành thành"));
MessageBox(NULL, "hoan thanh", "Finish", MB_OK);
break;
case ID_FILE_EXIT:
Huy();
DestroyWindow(hWnd);
break;
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
//if (*data != NULL)
// Free2d(data);
PostQuitMessage(0);
break;
default:
return DefWindo