You are on page 1of 7

// face.cpp : Defines the entry point for the console application.

#include
#include
#include
#include
#include

"stdafx.h"
<stdio.h>
<conio.h>
<math.h>
<string.h>

#define qm 320
#define qn 240
// ?c ?nh
void doc(unsigned char s[qm][qn], int m, int n, FILE *fpt)
{
int i, j;
if(fpt==NULL)
printf("\t Loi: ko dung ");
else {
for(i=0; i<m; i++)
for (j=0; j<n; j++)
{
s[i][j] = fgetc(fpt);
}
}
}
//

ham viet file

void ghianh(unsigned char s[qm][qn], int m, int n, FILE *fpt)


{
int i, j;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
fputc(s[i][j], fpt);
}
// Tru ma tran
void tru_mt(unsigned char s[qm][qn], unsigned char s0[qm][qn], unsigned char s1[
qm][qn], int m, int n)
{
int i, j;
for(i=0; i<m; i++)
{
for(j=0; j<n; j++)
{
int A= s0[i][j]-s[i][j];
s[i][j] = abs(A);
}
}
}
// Loc LBP
void LBP(unsigned char Q[qm][qn], unsigned char D[qm][qn], int m, int n, int a)
{

int i, j, u, v, x, y;
int dem=0;
for (i=0; i<m; i++)
for(j=0; j<n; j++)
{
dem = 1;
Q[i][j] = 0;
for(u=i-1; u<=i+1; u++)
for(v=j-1; v<=j+1; v++)
{
if((u<0) || (v<0)|| (u>=m)|| (v>=n))
{
x=i;
y=j;
}
else
{
x=u;
y=v;
}
if ((x != i) || (y != j))
{
if (D[x][y]+a < D[i][j])
Q[i][j] = Q[i][j] + dem;
//a chnh l m?t thng s? thm vo d? ch?c ch?n hon.
dem = dem*2;
//T?c pixel gi?a ph?i l?n hon pixel xung quanh + a.
//V? m?t l tu?ng th a s? b?ng 0.
}
//Vi?c cho a vo l d? h?n ch? nhi?u ?nh tc d?ng lm cho k?t qu? tnh LBP sai l?ch.
}
}
}
void xuatfile(unsigned char F0[qm][qn], int m, int n)
{
FILE *f = fopen("H2.txt", "w");
if(!f)
return;
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
{
printf("%4d", F0[i][j]);
fprintf(f, "%4d", F0[i][j]);
}
printf("\n");
fprintf(f, "\n\n");
}
fclose(f);
}
// Ham cong tat ca cac phan tu trong ma tran
long tongptu(unsigned char F0[qm][qn], int m, int n)
{
long kq = 0;
int i, j;
for(i=0; i<m; i++)

for(j=0; j<n; j++)


kq += F0[i][j];
return kq;
}
int a = 0;
void thuchienLBP(unsigned char E0[qm][qn], int m, int n, FILE *ftp)
{
unsigned char F0[qm][qn];
doc(F0, m, n, ftp);
LBP(E0, F0, m, n, a);
}
/*void thuchienLBPn(unsigned char Eh[qm][qn], int m, int n, FILE *ftp)
{
for(int h=0; h<5; h++)
thuchienLBP(Eh,m,n,ftp);
}*/
// Ham chinh
/*void dinhnghia(unsigned char E[][240], int m, int n)
{
for( m=1; m<5; m++)
for(int n=1; n<5; n++)
{
printf("E[%c][%c]",m,n);
scanf("%c%c",&E[m][n]);
}
}*/
void main()
{
FILE *fpt;
int m=qm, n=qn; // m: so dong, n: cot;
unsigned char E[qm][qn], H[qm][qn],E1[qm][qn], E2[qm][qn], Es[qm][qn], E
3[qm][qn], E4[qm][qn],E5[qm][qn], Fs[qm][qn], H1[qm][qn], H2[qm][qn], H3[qm][qn]
, H4[qm][qn],H5[qm][qn]/*, E6[qm][qn],H6[qm][qn]*/;
//unsigned char E6[qm][qn], E7[qm][qn], E8[qm][qn], E9[qm][qn],E10[qm][q
n], H6[qm][qn], H7[qm][qn], H8[qm][qn], H9[qm][qn],H10[qm][qn];
int tong[50];
char *s[]={"mau1.raw","mau2.raw","mau3.raw","mau4.raw","mau5.raw","mau6.
raw","mau7.raw","mau8.raw","mau9.raw","mau10.raw"};
int i=0;
//doc du lieu test
fpt=fopen("mausua2-300.raw","rb");
thuchienLBP(Es, m, n, fpt);
fclose(fpt);
//
for(i=0;i<10;i++)
{
fpt=fopen(s[i],"rb");
//doc(H1, m, n, fpt);
//xuatfile(H1,m,n);
thuchienLBP(E, m, n, fpt);
//xuatfile(E1,m,n);
fclose(fpt);

tru_mt(H, E, Es, m, n);


tong[i]=tongptu(H, m, n);
}
// doc anh
fpt=fopen("mau1.raw","rb");
//doc(H1, m, n, fpt);
//xuatfile(H1,m,n);
thuchienLBP(E1, m, n, fpt);
//xuatfile(E1,m,n);
fclose(fpt);
//doc(H2, m, n, fpt);
fpt=fopen("mau2.raw","rb");
thuchienLBP(E2, m, n, fpt);
fclose(fpt);
//doc(H3, m, n, fpt);
fpt=fopen("mau3.raw","rb");
thuchienLBP(E3, m, n, fpt);
fclose(fpt);
//doc(H4, m, n, fpt);
fpt=fopen("mau4.raw","rb");
thuchienLBP(E4, m, n, fpt);
fclose(fpt);
//doc(H5, m, n, fpt);
fpt=fopen("mau5.raw","rb");
thuchienLBP(E5, m, n, fpt);
fclose(fpt);
/*fpt=fopen("mau6.raw","rb");
thuchienLBP(E6, m, n, fpt);
fclose(fpt);
/*fpt=fopen("mau7.raw","rb");
thuchienLBP(E7, m, n, fpt);
fclose(fpt);
fpt=fopen("mau8.raw","rb");
thuchienLBP(E8, m, n, fpt);
fclose(fpt);
fpt=fopen("mau9.raw","rb");
thuchienLBP(E9, m, n, fpt);
fclose(fpt);
fpt=fopen("mau10.raw","rb");
thuchienLBP(E10, m, n, fpt);
fclose(fpt);*/
fpt=fopen("mausua2-300.raw","rb");
thuchienLBP(Es, m, n, fpt);
fclose(fpt);
// tru ma tran cua anh chua biet voi 2 ma tran cua anh trong database
tru_mt(H1, E1, Es, m, n);
//xuatfile(H1,m,n);

tru_mt(H2, E2, Es, m,


//xuatfile(H2,m,n);
tru_mt(H3, E3, Es, m,
tru_mt(H4, E4, Es, m,
tru_mt(H5, E5, Es, m,
//

n);
n);
n);
n);

tru_mt(H6, E6, Es, m, n);


/*tru_mt(H7, E7, Es, m, n);
tru_mt(H8, E8, Es, m, n);
tru_mt(H9, E9, Es, m, n);
tru_mt(H10, E10, Es, m, n);*/

// cong tat ca cac phan tu co trong ma tran hieu H1 va H2


long tong1 = tongptu(H1, m, n);
long tong2 = tongptu(H2, m, n);
long tong3 = tongptu(H3, m, n);
long tong4 = tongptu(H4, m, n);
long tong5 = tongptu(H5, m, n);
//long tong6 = tongptu(H6, m, n);
/*long tong7 = tongptu(H7, m, n);
long tong8 = tongptu(H8, m, n);
long tong9 = tongptu(H9, m, n);
long tong10 = tongptu(H10, m, n);*/
printf("tong1 = %ld", tong1);
printf("\n");
printf("tong2 = %ld", tong2);
printf("\n");
printf("tong3 = %ld", tong3);
printf("\n");
printf("tong4 = %ld", tong4);
printf("\n");
printf("tong5 = %ld", tong5);
printf("\n");
/*
printf("tong6 = %ld", tong6);
printf("\n");
/*printf("tong7 = %ld", tong7);
printf("\n");
printf("tong8 = %ld", tong8);
printf("\n");
printf("tong9 = %ld", tong9);
printf("\n");
printf("tong10 = %ld", tong10);
printf("\n");
*/
if(tong1 < tong2 && tong1 < tong3 && tong1 < tong4 &&
ong1/* < tong6 /*&& tong1 < tong7 && tong1 < tong8 && tong1 <
ong10*/)
{
printf("\nAnh dua vao chinh la anh 1\n");
fpt = fopen("ketqua_mau1.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong2 < tong1 && tong2 < tong3 && tong2 < tong4 &&
ong2/* < tong6 /*&& tong2 < tong7 /*&& tong2 < tong8 && tong2
tong10*/)
{

tong1 < tong5 && t


tong9 && tong1 < t

tong2 < tong5 && t


< tong9 && tong2 <

printf("\nAnh dua vao chinh la anh 2\n");


fpt = fopen("ketqua_mau2.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong3 < tong1 && tong3 < tong2 && tong3 < tong4 && tong3 < tong5 && t
ong3/* < tong6 /*&& tong3 < tong7 /*&& tong3 < tong8 && tong3 < tong9 && tong3 <
tong10*/)
{
printf("\nAnh dua vao chinh la anh 3\n");
fpt = fopen("ketqua_mau3.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong4 < tong1 && tong4 < tong2 && tong4 < tong3 && tong4 < tong5 && t
ong4/* < tong6 /*&& tong4 < tong7 /*&& tong4 < tong8 && tong4 < tong9 && tong4 <
tong10*/)
{
printf("\nAnh dua vao chinh la anh 4\n");
fpt = fopen("ketqua_mau4.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong5 < tong1 && tong5 < tong2 && tong5 < tong3 && tong5 < tong4 && t
ong5/* < tong6 /*&& tong5 < tong7 /*&& tong5 < tong8 && tong5 < tong9 && tong5 <
tong10*/)
{
printf("\nAnh dua vao chinh la anh 5\n");
fpt = fopen("ketqua_mau5.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
//if(tong6 < tong1 && tong6 < tong2 && tong6 < tong3 && tong6 < tong4 &&
tong6 < tong5 /*&& tong6 < tong7/* && tong6 < tong8 && tong6 < tong9 && tong6 <
tong10*/)
/*{
printf("\nAnh dua vao chinh la anh 6\n");
fpt = fopen("ketqua_mau6.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
/*if(tong7 < tong1 && tong7 < tong2 && tong7 < tong3 && tong7 < tong4 &&
tong7 < tong6 && tong7 < tong6 && tong7 < tong8 && tong7 < tong9 && tong7 < ton
g10)
{
printf("\nAnh dua vao chinh la anh 7\n");
fpt = fopen("ketqua_mau7.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong8 < tong1 && tong8 < tong2 && tong8 < tong3 && tong8 < tong4 && t
ong8 < tong5 && tong8 < tong6 && tong8 < tong7 && tong8 < tong9 && tong8 < tong1
0)
{
printf("\nAnh dua vao chinh la anh 8\n");
fpt = fopen("ketqua_mau8.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong9 < tong1 && tong9 < tong2 && tong9 < tong3 && tong9 < tong4 && t

ong9 < tong5 && tong9 < tong6 && tong9 < tong7 && tong9 < tong8 && tong9 < tong1
0)
{
printf("\nAnh dua vao chinh la anh 9\n");
fpt = fopen("ketqua_mau9.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}
if(tong10 < tong1 && tong10 < tong2 && tong10 < tong3 && tong10 < tong4
&& tong10 < tong5 && tong10 < tong6 && tong10 < tong7 && tong10 < tong8 && tong1
0 < tong9)
{
printf("\nAnh dua vao chinh la anh 10\n");
fpt = fopen("ketqua_mau10.raw", "wb");
ghianh(Fs, m, n, fpt);
fclose(fpt);
}*/
getch();
}