void __fastcall TForm1::OpenImage1Click(TObject *Sender)
{
OpenPictureDialog1->Execute();
resim->LoadFromFile(OpenPictureDialog1->FileName);
Canvas->Draw(20,20,resim);
}//--------------------------resim alındı.
void __fastcall TForm1::Method21Click(TObject *Sender)
{
byte *ptr;
double UT[3]={0};
double W1[3]={0};
double W2[3]={0};
double u1[3]={0};
double u2[3]={0};
double max=-1.0;
double S[3]={0};
double toplam1[3]={0};
double Top[3]={0};
double esikdegeri[3]={0};
int esik=0;
int **Hist;
Hist=new int*[3];
for(int i=0;i<3;i++)
Hist[i]=new int[256];
for(int i=0;i<3;i++)
for(int j=0;j<256;j++)
Hist[i][j]=0;
for(int i=0;i<resim->Height;i++){
ptr=(Byte*)resim->ScanLine[i];
for(int j=0;j<resim->Width;j++){
ptr[3*j]=(0.299*ptr[3*j]+0.587*ptr[3*j+1]+0.114*ptr[3*j+2]);
ptr[3*j+1]=ptr[3*j];
ptr[3*j+2]=ptr[3*j];
}}
//--------------------------------------------------------------
for(int k=0;k<256;k++){
for(int i=0;i<resim->Height;i++){
ptr=(Byte*)resim->ScanLine[i];
for(int j=0;j<resim->Width;j++){
if(k==ptr[3*j])
Hist[0][k]++;
if(k==ptr[3*j+1])
Hist[1][k]++;
if(k==ptr[3*j+2])
Hist[2][k]++;
} } }
//---------------------------------------------------------------
/* histograf degerlerini hesapladı*/
//---------------------------------------------------------------
for(int i=0;i<3;i++){
for(int k=0;k<256;k++){
UT[i]+=(k*Hist[i][k]);
Top[i]+=Hist[i][k];
}}
//---------------------------------------------------
/*UT hesaplandı*/
//-------------------------------------------------------------------------
for(int i=0;i<3;i++){
for(int j=0;j<256;j++){
W1[i]+=Hist[i][j];
if(W1[i]==0)
continue;
W2[i]=Top[i]-W1[i];
if(W2[i]==0)
break;
toplam1[i]+=(j*Hist[i][j]);
u1[i]=toplam1[i]/W1[i];
u2[i]=(UT[i]-toplam1[i])/W2[i];
S[i]=W1[i]*W2[i]*(u1[i]-u2[i])*(u1[i]-u2[i]);
if(S[i]>max){
max=S[i];
esikdegeri[i]=j;
}
}
max=-1.0;
}
for(int t=0;t<3;t++){
for(int i=0;i<resim->Height;i++){
ptr=(Byte*)resim->ScanLine[i];
for(int j=0;j<resim->Width;j++){
if(esikdegeri[t]>ptr[3*j+t])
ptr[3*j+t]=0;
else ptr[3*j+t]=255;
}} }
}