// AutoThreshhold.cpp: implementation of the CAutoThreshhold class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FaceRecognition.h"
#include "AutoThreshhold.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAutoThreshhold::CAutoThreshhold()
{
}
CAutoThreshhold::~CAutoThreshhold()
{
}
BOOL CAutoThreshhold::LoadImage(char *filename)
{
return TRUE;
}
BOOL CAutoThreshhold::LoadImage(BYTE *Array, long width, long height)
{
if (!Array)
return FALSE;
m_Array=(BYTE*)new BYTE[width*height];
for (long i=0;i<width*height;i++)
m_Array[i]=Array[i];
m_Height=height;
m_Width=width;
return TRUE;
}
BYTE CAutoThreshhold::CalcBestThreshold()
{
if (!m_Array)
return 0;
GetGrayTable();
GetPiTable();
int i,Maxi;
double max=0;
for (i=0;i<256;i++)
{
GetUk(i);
deltak[i]=(u*wk0-uk0)*(u*wk0-uk0)/(wk0*wk1);
if (deltak[i]>=max)
{
max=deltak[i];
Maxi=i;
}
}
return Maxi;
}
void CAutoThreshhold::GetGrayTable()
{
for (long i=0;i<256;i++)
Gray[i]=0;
for (i=0;i<m_Height*m_Width;i++)
Gray[m_Array[i]]++;
}
void CAutoThreshhold::GetPiTable()
{
u=0;
for (long i=0;i<256;i++)
{
Pi[i]=(double)Gray[i]/(double)(m_Height*m_Width);
u+=i*Pi[i];
}
}
void CAutoThreshhold::GetUk(int k)
{
uk0=0;
wk0=0;
for (long i=0;i<=k;i++)
{
uk0+=i*Pi[i];
wk0+=Pi[i];
}
uk1=u-uk0;
wk1=1-wk0;
}
void CAutoThreshhold::Destroy()
{
if (m_Array)
{
delete [] m_Array;
m_Array=0;
}
m_Height=0;
m_Width=0;
}
评论0