Entropy Thresholding is a means of thresholding an image that selects an optimum threshold value by choosing the pixel intensity from the image's histogram that exhibits the maximum entropy over the entire image.
Algorithm[]
C++ code[]
ImageSpace ImageSpace::EntropyThreshold(void) { ImageSpace *ReturnImageSpace = new ImageSpace; BYTE *b1 = new BYTE[ XMax*YMax ]; int ThresholdLevel, *hist; double prob[256], Sum_prob_1k = 0, Sum_prob_kl = 0, Sum_prob_ln_1k = 0, Sum_prob_ln_kl = 0, Entropy[256]; //gets histogram of image hist = Histogram(); // probability distribution for( int i = 0; i< 256; i++) prob[i] = (double) hist[i] / (XMax * YMax); // k is trial threshold level for( int k = 0; k< 255; k++) { Sum_prob_1k = 0; Sum_prob_kl = 0; Sum_prob_ln_1k = 0; Sum_prob_ln_kl = 0; //From 1 to k for(i = 1; i < k; i++) { //Sums probablities to k Sum_prob_1k += prob[i]; //Sums prob times Log of prob to k if(prob[i]!=0) Sum_prob_ln_1k += (prob[i] * log(prob[i])); } for(i = k; i < 256; i++) //From k to end { //Sums prob of k to end Sum_prob_kl += prob[i]; //Sums prob times log of prob if(prob[i]!=0) Sum_prob_ln_kl += (prob[i] * log(prob[i])); } //Final equation of entropy for each k Entropy[k] = log(Sum_prob_1k) + log(Sum_prob_kl) - (Sum_prob_ln_1k / Sum_prob_1k) - (Sum_prob_ln_kl / Sum_prob_kl); //protects against divide by zero if(Entropy[k]<0) Entropy[k]=0; } ThresholdLevel = 0; for(k = 0; k<256; k++) //Finds Maximum if(Entropy[k] > Entropy[ThresholdLevel]) ThresholdLevel = k; //Thresholds there cout << "Thresholded at :" << ThresholdLevel << endl; for( int y=0; y < YMax; y++ ) for( int x=0; x < XMax; x++ ) b1[ x + y * XMax ] = ( b0[ x + y * XMax ] > ThresholdLevel ) ? 255 : 0; ReturnImageSpace->Load(b1); IsThreshold = TRUE; return *ReturnImageSpace; }