static int get1dmaxentropythreshold (const int* histgram, int const in_start, int const in_end) {int X, Y, Amou NT = 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; Only one color in the image if (MinValue + 1 = = MaxValue) return MinValue; There are only two colors in the image for (Y = MinValue; Y <= MaxValue; y++) Amount + = Histgram[y]; Total number of pixels 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; }
One-dimensional maximum entropy method of binary value