标签:
static int Get1DMaxEntropyThreshold(const int* HistGram, int const in_Start, int const in_End) { int X, Y, Amount = 0; double HistGramD[256]; double SumIntegral, EntropyBack, EntropyFore, MaxEntropy; int MinValue = in_Start, MaxValue = in_End; int Threshold = 0; for (MinValue = in_Start; MinValue < in_End && HistGram[MinValue] == 0; MinValue++) ; for (MaxValue = in_End; MaxValue > MinValue && HistGram[MinValue] == 0; MaxValue--) ; if (MaxValue == MinValue) return MaxValue; // 图像中只有一个颜色 if (MinValue + 1 == MaxValue) return MinValue; // 图像中只有二个颜色 for (Y = MinValue; Y <= MaxValue; Y++) Amount += HistGram[Y]; // 像素总数 for (Y = MinValue; Y <= MaxValue; Y++) HistGramD[Y] = (double)HistGram[Y] / Amount + 1e-17; MaxEntropy = -10000000; for (Y = MinValue + 1; Y < MaxValue; Y++) { SumIntegral = 0; for (X = MinValue; X <= Y; X++) SumIntegral += HistGramD[X]; EntropyBack = 0; for (X = MinValue; X <= Y; X++) EntropyBack += (-HistGramD[X] / SumIntegral * log(HistGramD[X] / SumIntegral)); EntropyFore = 0; for (X = Y + 1; X <= MaxValue; X++) EntropyFore += (-HistGramD[X] / (1 - SumIntegral) * log(HistGramD[X] / (1 - SumIntegral))); if (MaxEntropy < EntropyBack + EntropyFore) { Threshold = Y; MaxEntropy = EntropyBack + EntropyFore; } } return Threshold; }
标签:
原文地址:http://www.cnblogs.com/cv-pr/p/4599543.html