From: Cristy Date: Thu, 13 Jul 2017 16:14:53 +0000 (-0400) Subject: ... X-Git-Tag: 7.0.6-1~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6f01ac78d33b7252f9eb5f22554c7cd4ff6c7058;p=imagemagick ... --- diff --git a/MagickCore/threshold.c b/MagickCore/threshold.c index f465e94d4..77bd176c6 100644 --- a/MagickCore/threshold.c +++ b/MagickCore/threshold.c @@ -435,7 +435,7 @@ static double OTSUThreshold(const Image *image,const double *histogram, Calculate probability density. */ for (i=0; i <= (ssize_t) MaxIntensity; i++) - probability[i]=histogram[i]/(double) (image->columns*image->rows); + probability[i]=histogram[i]; /* Generate probability of graylevels and mean value for separation. */ @@ -574,12 +574,17 @@ MagickExport MagickBooleanType AutoThresholdImage(Image *image, property[MagickPathExtent]; double + gamma, *histogram, + sum, threshold; MagickBooleanType status; + register ssize_t + i; + ssize_t y; @@ -617,6 +622,18 @@ MagickExport MagickBooleanType AutoThresholdImage(Image *image, } } image_view=DestroyCacheView(image_view); + /* + Normalize histogram. + */ + sum=0.0; + for (i=0; i <= (ssize_t) MaxIntensity; i++) + sum+=histogram[i]; + gamma=PerceptibleReciprocal(sum); + for (i=0; i <= (ssize_t) MaxIntensity; i++) + histogram[i]=gamma*histogram[i]; + /* + Discover threshold from histogram. + */ switch (method) { case KapurThresholdMethod: @@ -641,6 +658,9 @@ MagickExport MagickBooleanType AutoThresholdImage(Image *image, status=MagickFalse; if (status == MagickFalse) return(MagickFalse); + /* + Threshold image. + */ (void) FormatLocaleString(property,MagickPathExtent,"%g%%",threshold); (void) SetImageProperty(image,"auto-threshold:threshold",property,exception); return(BilevelImage(image,QuantumRange*threshold/100.0,exception));