📄 global.cpp
字号:
#include "stdafx.h" //It is needed!!!!
#include "global.h"
#include "math.h"
//////////from xlchen's canny/////////////
#include <string.h>
#include <malloc.h>
double global_TriangleIndex[72]=
{
1.0000,0.0000,//0 ,360
0.9848,0.1736,//10
0.9396,0.3420,//20
0.8660,0.5000,//30
0.7660,0.6427,//40
0.6427,0.7660,//50
0.5000,0.8660,//60
0.3420,0.9396,//70
0.1736,0.9848,//80
0.0000,1.0000,//90
-0.1736,0.9848,//100
-0.3420,0.9396,//110
-0.5000,0.8660,//120
-0.6427,0.7660,//130
-0.7660,0.6427,//140
-0.8660,0.5000,//150
-0.9396,0.3420,//160
-0.9848,0.1736,//170
-1.0000,0.0000,//180
-0.9848,-0.1736,//190
-0.9396,-0.3420,//200
-0.8660,-0.5000,//210
-0.7660,-0.6427,//220
-0.6427,-0.7660,//230
-0.5000,-0.8660,//240
-0.3420,-0.9396,//250
-0.1736,-0.9848,//260
-0.0000,-1.0000,//270
0.1736,-0.9848,//280
0.3420,-0.9396,//290
0.5000,-0.8660,//300
0.6427,-0.7660,//310
0.7660,-0.6427,//320
0.8660,-0.5000,//330
0.9396,-0.3420,//340
0.9848,-0.1736//350
};
//////////from xlchen's canny end!////////
CString gWorkDir;
BYTE gFaceMask[4096];
BOOL gbMaskReadIn = false;
BOOL ReadInFaceMask()
{
CString oldpath;
GetCurrentDirectory(256,oldpath.GetBuffer(256));
SetCurrentDirectory(gWorkDir);
CString maskfilename = ".\\data\\facemask.bin";
CFile* pFile = new CFile;
if(!pFile->Open(maskfilename, CFile::modeRead))
{
SetCurrentDirectory(oldpath);
return false;
}
TRY {
pFile->Read(gFaceMask, 4096);
}
CATCH (CFileException, e) {
AfxMessageBox("Read error");
return FALSE;
}
END_CATCH
SetCurrentDirectory(oldpath);
pFile->Close();
delete pFile;
return true;
}
////////////////////////////////////////////////////////////////////////
//author: ssg
//Modified version based on the one from Liu Ming Bao
//Para1: BYTE* image: the source image data poiter
//Para2: BYTE* rimage: the resulting image data poiter
//Para3,4: int w, h: source image's width and height in pixel
//para5: float cscale: the scale to shrink
//////////////////////////////////////////////////////////////////////////
void ShrinkImage(BYTE *scrImage, BYTE* destImage, WORD w, WORD h, float cscale)
{
DWORD i, j, m, n, scrOff, destOff, destw, desth;
DWORD num,sum;
double step_w, step_h,tm, tn;
destw = (WORD) (w * cscale);
desth = (WORD) (h * cscale);
step_w = (double)w / destw;
step_h = (double)h / desth;
for (i=0; i < desth; i++)
{
tm=i*step_h;
destOff = destw * i;
for (j=0; j < destw; j++)
{
sum=0;
num = 0;
tn=j*step_w;
for (m = (WORD)tm + 1; m <= (WORD)(tm + step_h); m++)
{
scrOff=w*m;
for (n = (WORD)tn + 1; n <= (WORD)(tn + step_w); n++)
{
sum += scrImage[scrOff+n];
if(scrOff >= (DWORD)(w * h))
break;
num++;
}
}
if(num!=0)
destImage[destOff + j] = (BYTE)(sum/num);
else
destImage[destOff + j]=(BYTE)sum;
}
}
}
////////////////////////////////////////////////////////////////////////
//author: ssg Time: 1998.4.18
//Para1: BYTE* scrImage: the source image data poiter
//Para2: BYTE* destImage: the resulting image data poiter
//Para3,4: WORD scrW, scrH: source image's width and height in pixel
//para5,6: WORD destW,destH: destinate image's width and height in pixel
//////////////////////////////////////////////////////////////////////////
void ShrinkImage(BYTE *scrImage, BYTE* destImage, WORD scrW, WORD scrH,WORD destW, WORD destH)
{
DWORD i, j, m, n,scrOff, destOff;
DWORD num,sum;
double step_w, step_h,tm,tn;
step_w = (double)scrW / destW;
step_h = (double)scrH / destH;
for (i=0; i < destH; i++)
{
tm = i * step_h;
destOff = i * destW;
for (j=0; j < destW; j++)
{
sum = 0;
num = 0;
tn = j*step_w;
for (m = (DWORD)tm + 1; m <= (DWORD)(tm + step_h); m++)
{
scrOff = m * scrW;
for (n = (DWORD)tn + 1; n <= (DWORD)(tn + step_w); n++)
{
if(scrOff + n >= (DWORD)(scrW * scrH)) break;
sum += scrImage[scrOff + n];
num++;
}
}
if(num!=0)
destImage[destOff + j] = (BYTE)(sum / num);
else
destImage[destOff + j] = (BYTE)sum;
}
}
}
////////////////////////////////////////////////////////////////////////
//author: ssg Time: 1998.4.18
//Para1: BYTE* scrImage: the source image data poiter
//Para2: BYTE* destImage: the resulting image data poiter
//Para3,4: WORD scrW, scrH: source image's width and height in pixel
//para5,6: WORD destW,destH: destinate image's width and height in pixel
/*/////////////////////////////////////////////////////////////////////////
void MagnifyImage(BYTE *scrImage, BYTE* destImage, WORD scrW, WORD scrH,WORD destW, WORD destH)
{
DWORD i, j, scrRow, scrVol;
DWORD scrOffset, destOffset;
double xScale, yScale; //the magnify scale of x,y direction
ASSERT(destW >= scrW);
ASSERT(destH >= scrH);
xScale = (double)destW / scrW;
yScale = (double)destH / scrH;
for(i=0; i<destH; i++)
{
scrRow = (WORD)(i / yScale);
ASSERT(scrRow <= scrH);
if(scrRow > scrH)
scrRow = scrH;
scrOffset = scrRow * scrW;
destOffset = i * destW;
for(j=0; j<destW; j++)
{
scrVol = (WORD)(j / xScale);
ASSERT(scrVol <= scrW);
if(scrVol > scrW)
scrVol = scrW;
destImage[destOffset + j] = scrImage[scrOffset + scrVol];
}
}
}
*///////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//author: ssg Time: 1998.4.18
//Para1: BYTE* scrImage: the source image data poiter
//Para2: BYTE* destImage: the resulting image data poiter
//Para3,4: WORD scrW, scrH: source image's width and height in pixel
//para5,6: WORD destW,destH: destinate image's width and height in pixel
//////////////////////////////////////////////////////////////////////////
void MagnifyImage(BYTE *scrImage, BYTE* destImage, WORD scrW, WORD scrH,WORD destW, WORD destH)
{
DWORD i, j;
double fGrey;
double scrRow, scrVol;
double coefX, coefY;
DWORD scrOffset, destOffset;
double xScale, yScale; //the magnify scale of x,y direction
ASSERT(destW >= scrW);
ASSERT(destH >= scrH);
xScale = (double)destW * 1.0 / scrW;
yScale = (double)destH * 1.0 / scrH;
for(i=0; i<destH; i++)
{
scrRow = i * 1.0 / yScale;
if(((WORD)scrRow) >= scrH)
scrRow = scrH - 1.0;
//scrOffset = scrRow * scrW;
destOffset = i * destW;
for(j=0; j<destW; j++)
{
scrVol = j * 1.0 / xScale;
if(((WORD)scrVol) >= scrW)
scrVol = scrW - 1.0; //内插方法
coefX = scrVol - int(scrVol);
coefY = scrRow - int(scrRow);
scrOffset = ((int)scrRow) * scrW + (int)scrVol;
fGrey = (1-coefX) * (float)scrImage[scrOffset] + coefX * (float)scrImage[scrOffset + 1];
fGrey = (1-coefY) * fGrey + coefY * ((1-coefX) * (float)scrImage[scrOffset + scrW] + coefX * (float)scrImage[scrOffset + scrW + 1]);
destImage[destOffset + j] = int (fGrey + 0.5);
}
}
}
///////////////////////////////////////////////////////////////////
/*
Function: Calculate the eigenvalues of the matrix matrix;
double* matrix: a n*n_D-real-symmetry matrix;
long n the dimension of the matrix matrix;
doube* eigenvalue: the n_D vector that contains the n eigenvalues of the matrix that the function calculated;
double* c: The temporary n*n_D vector that used by the function;
Note:
When the function returns, the vector eigenvalue contains the eigenvalues!
*/
///////////////////////////////////////////////////////////////////
void CalEgvalue(double *matrix,long n,double *eigenvalue,double *c)
{
long i,j,k,u;
double h,f,g,h2;
for(i=n-1;i>=1;i--){
printf("\rloop1 times: %d ",i);
h=0.0;
if(i>1)
for(k=0;k<=i-1;k++){
u=i*n+k;
h=h+matrix[u]*matrix[u];
}
if(h+1.0==1.0){
c[i-1]=0.0;
if(i==1) c[i-1]=matrix[i*n+i-1];
eigenvalue[i]=0.0;
}
else{
c[i-1]=sqrt(h);
u=i*n+i-1;
if(matrix[u]>0.0) c[i-1]=-c[i-1];
h=h-matrix[u]*c[i-1];
matrix[u]=matrix[u]-c[i-1];
f=0.0;
for(j=0;j<=i-1;j++){//j=0;
matrix[j*n+i]=matrix[i*n+j]/h;
g=0.0;
for(k=0;k<=j;k++)
g=g+matrix[j*n+k]*matrix[i*n+k];
if(j+1<=i-1)
for(k=j+1;k<=i-1;k++)
g=g+matrix[k*n+j]*matrix[i*n+k];
c[j-1]=g/h;//////////////////////////////////////??????????????????????????
f=f+g*matrix[j*n+i];
}
h2=f/(h+h);
for(j=0;j<=i-1;j++){//j=0;
f=matrix[i*n+j];
g=c[j-1]-h2*f;//////////////////////////////////////??????????????????????????
c[j-1]=g;
for(k=0;k<=j;k++){//k=0;
u=j*n+k;
matrix[u]=matrix[u]-f*c[k-1]-g*matrix[i*n+k];//////////////////////////////////////??????????????????????????
}
}
eigenvalue[i]=h;
}
}
eigenvalue[0]=0.0;
for(i=0;i<=n-1;i++){
printf("\rloop2 times: %d ",i);
if((eigenvalue[i]!=0.0)&&(i-1>=0))
for(j=0;j<=i-1;j++){
g=0.0;
for(k=0;k<=i-1;k++) g=g+matrix[i*n+k]*matrix[k*n+j];
for(k=0;k<=i-1;k++){
u=k*n+j;
matrix[u]=matrix[u]-g*matrix[k*n+i];
}
}
u=i*n+i;
eigenvalue[i]=matrix[u];
matrix[u]=1.0;
if(i-1>=0)
for(j=0;j<=i-1;j++){
matrix[i*n+j]=0.0;
matrix[j*n+i]=0.0;
}
}
}
///////////////////////////////////////////////////////////////////
/*
Function: Calculate the eigenvalues of the matrix matrix;
double* matrix : a n*n_D-real-symmetry matrix;
long n : the dimension of the matrix matrix;
doube* eigenvalue: the n_D vector that contains the n eigenvalues of the matrix that the function calculated;
double* c : The temporary n*n_D vector that used by the function;
doube eps : The approximate of 00000000
Note:
When the function returns, the vector eigenvalue contains the eigenvalues!
and the matrix matrix contains the n eigenvectors.
*/
///////////////////////////////////////////////////////////////////
DWORD CalEgvector(double *matrix,long n,double *eigenvalue,double *c,double eps)
{
long i,j,k,m,u,v;
double d,f,h,g,p,r,e,s;
c[n-1]=0.0;
d=0.0;
f=0.0;
for(j=0;j<=n-1;j++){
printf("\rloop3 times: %d ",j);
h=eps*(fabs(eigenvalue[j])+fabs(c[j]));
if(h>d) d=h;
m=j;
while((m<=n-1)&&(fabs(c[m])>d)) m=m+1;
if(m!=j) do{
g=eigenvalue[j];
p=(eigenvalue[j+1]-g)/(2.0*c[j]);
r=sqrt(p*p+1.0);
if(p>=0.0) eigenvalue[j]=c[j]/(p+r);
else eigenvalue[j]=c[j]/(p-r);
h=g-eigenvalue[j];
for(i=j+1;i<=n-1;i++) eigenvalue[i]=eigenvalue[i]-h;
f=f+h;
p=eigenvalue[m];
e=1.0;
s=0.0;
for(i=m-1;i>=j;i--){
g=e*c[i];
h=e*p;
if(fabs(p)>=fabs(c[i])){
e=c[i]/p;
r=sqrt(e*e+1.0);
c[i+1]=s*p*r;
s=e/r;
e=1.0/r;
}
else{
e=p/c[i];
r=sqrt(e*e+1.0);
c[i+1]=s*c[i]*r;
s=1.0/r;
e=e/r;
}
p=e*eigenvalue[i]-s*g;
eigenvalue[i+1]=h+s*(e*g+s*eigenvalue[i]);
for(k=0;k<=n-1;k++){
u=k*n+i+1;
v=u-1;
h=matrix[u];
matrix[u]=s*matrix[v]+e*h;
matrix[v]=e*matrix[v]-s*h;
}
}
c[j]=s*p;
eigenvalue[j]=e*p;
}while(fabs(c[j])>d);
eigenvalue[j]=eigenvalue[j]+f;
}
for(i=0;i<=n-1;i++){
k=i;
p=eigenvalue[i];
if(i+1<=n-1){
j=i+1;
while((j<=n-1)&&(eigenvalue[j]<=p)){
k=j;
p=eigenvalue[j];
j=j+1;
}
}
if(k!=i){
eigenvalue[k]=eigenvalue[i];
eigenvalue[i]=p;
for(j=0;j<=n-1;j++){
u=j*n+i;
v=j*n+k;
p=matrix[u];
matrix[u]=matrix[v];
matrix[v]=p;
}
}
}
return(1);
}
double InnerProduct(double* p1,double* p2, DWORD N)
{
double* q1=p1;
double* q2=p2;
double sum=(double)0.0;
DWORD i;
for(i=0;i<N;i++)
sum += (*q1++) * (*q2++);
return sum;
}
double NormalVector(double* vector, DWORD N)
{
double *pv;
DWORD j;
double sum = 0.0;
pv = vector;
for(j=0; j < N; j++, pv++)
sum += (*pv) * (*pv);
sum = sqrt(sum); // it is ||vector||
pv = vector;
for(j=0; j<N; j++)
*pv++ /= sum;
return sum;
}
/////////order the values///////////////////
void OrderValues(double* ev, WORD* order, WORD M)
{
DWORD i,j,maxnum;
double* evs;
double mini,maxi;
evs=new double[M];
mini=*ev;
for(i=0;i<M;i++)
{
evs[i]=*(ev+i);
if(evs[i]<mini) mini=evs[i];
order[i]=(WORD)i;
}
for(i=0;i<M;i++)
{
maxi=evs[i];
maxnum=i;
for(j=0;j<M;j++)
{
if(evs[j]>maxi)
{
maxi=evs[j];
maxnum=j;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -