int i,j;
int hist[256];
for(i=0;i<256;i++)
hist[i]=0;
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
hist[temp[i*w+j]]++;
}
float t0=0.0;
float t1=0.0;
float u=0.0;
float v=0.0;
float k=0.6;
float s=0.0;
for(i=0;i<256;i++)
{
t0+=i*hist[i];
u+=hist[i];
}
t0=t0/u;
while(true)
{
u=0;
v=0;
for(i=0;i<(int)(t0+1.5);i++)
{
v+=i*hist[i];
u+=hist[i];
}
t1=v/u;
u=0;
v=0;
for(i=(int)(t0+1.5);i<256;i++)
{
v+=i*hist[i];
u+=hist[i];
}
t1=k*(t1+v/u);
u=0;
v=0;
if((int)(t0+0.5)!=(int)(t1+0.5))
{
for(i=0;i<(int)(t0+1.5);i++)
{
v+=hist[i];
}
for(i=(int)(t0+1.5);i<256;i++)
{
u+=hist[i];
}
s=v/u;
if(s>2.5)
k-=0.001;
if(s<1.8)
k+=0.001;
t0=t1;
}
else
break;
}
for(i=0;i<h;i++)
for(j=0;j<w;j++)
{
if(temp[i*w+j]>t0)
temp[i*w+j]=255;
else
temp[i*w+j]=0;
}