Does anyone know how to implement'Optimal global and adaptive thresholding' algo?
If know, can please share with me.
Thanks.
Does anyone know how to implement'Optimal global and adaptive thresholding' algo?
If know, can please share with me.
Thanks.
public int OtsuThreshold(double[] histogram)
{
double pr1, pr2, pr;
int t = 0;
double[] vet = new double[255];
double major = -1;
//int idx;
for (int k = 1; k < Util.GrayLevels-1; k++)
{
pr1 = HistogramProbability(0, k, histogram);
pr2 = HistogramProbability(k + 1, Util.MaxGrayLevel, histogram);
pr=pr1*pr2;
if(pr==0) pr=1;
double result = Math.Pow((ImageMean(0, k, histogram) * pr2) - (ImageMean(k + 1, Util.MaxGrayLevel, histogram) * pr1), 2) / pr;
if (result > major)
{
major = result;
t = k;
}
}
//t = IMaior(vet,Util.GrayLevels);
return t;
}
public double HistogramProbability(int start, int end, double[] histogram)
{
if (start < 0 || end > histogram.Length)
return 0;
double p = 0;
for (int i = start; i < end; ++i)
p += histogram[i];
return p;
}
public double ImageMean(int start, int end, double[] histogram)
{
double mean = 0;
if (start < 0 || end > histogram.Length) return 0;
for (int i = start; i < end; i++)
{
mean += i * histogram[i];
}
return mean;
}
Sorry, I know there's a lot of source code here and some methods may be missing but if you follow it I'll see its not all that hard. Unfortunately I'm re-inventing the wheel (not using any library) but I hope the source here can help you write it in your language of choice.
This link can be useful
Also, if you have a library nearby (engineering or CS material) I recommend this book. It explain in a clear way how Otsu method works.
Regards