void DibThreshold(CDib& dib)
{
int threshold =0; //current threshold
int tnew = 128; // new threshold
long cumtoltal = 0; //current accumulative total
double m1,m2; // two means
register int i,j;
int w=dib.GetWidth();
int h=dib.GetHeight();
BYTE** ptr = dib.GetPtr();
float hist[256];
DibHistogram(dib,hist);
float cdf[256]={0.0,};
cdf[0]=hist[0];
for(i=1;i<256;i++)
{
cdf[i]=cdf[i-1]+hist[i];
}
do
{
threshold = tnew;
m1,m2 =0.0;
for(int i =0;i<threshold;i++)
{
m1+= hist[i]*1;
m1 /=cdf[threshold-1];
}
for(int i =0;i<threshold;i++)
{
m2+= hist[i]*1;
m2 /=cdf[255];
}
tnew = (int)((m1 + m2) / 2.0);
}
while (tnew != threshold);
for(i=0;i<w;i++)
{
for(j=0;j<h;j++)
{
ptr[i][j] = (BYTE)tnew;
}
}
}