1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
void TImgBinarization:: Otsu_Threshold()
{
// We need for that method the weighted histogram of the image
// We will also calculate the following statistcal elemetns of the histo
// mean value and variance, we have also to sum the histo
// The idea from Otsu is to compare 2 class by it's intervariance maximization
// The folowing var represents here the inter-variance we have to calculate for each k
double Sum_1k ,Sum_kl ;
double Mean_Val_Low;
double Mean_Val_High;
double intervar=0;
double myval=0;
ThresholdLevel = 0;
for( int k = 0; k< 256; k++) //We scan all the possible levels
{
// Background calculation ( below treshold )
Sum_1k =oImgBinStat-> Histo_Sum_Calc(pMat1ImgHistoWeight, 0, k);
Mean_Val_Low = oImgBinStat->Histo_MeanVal_Calc(pMat1ImgHistoWeight, 0, k);
// Forreground calculation ( above treshold )
Sum_kl = oImgBinStat-> Histo_Sum_Calc( pMat1ImgHistoWeight, k, 256);
Mean_Val_High = oImgBinStat-> Histo_MeanVal_Calc(pMat1ImgHistoWeight, k, 256);
intervar = Sum_1k * Sum_kl *pow((Mean_Val_Low- Mean_Val_High),2);
// If equivalent to maximize the intervariance
if (myval <= intervar)
{
myval= intervar;
ThresholdLevel = k;
}
}
printf( " ThresholdLevel pour Otsu: %f \n ", ThresholdLevel);
// image binarization
Image_Binarization(ThresholdLevel);
} |
Partager