2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS EEEEE GGGG M M EEEEE N N TTTTT %
7 % SS E G MM MM E NN N T %
8 % SSS EEE G GGG M M M EEE N N N T %
9 % SS E G G M M E N NN T %
10 % SSSSS EEEEE GGGG M M EEEEE N N T %
13 % MagickCore Methods to Segment an Image with Thresholding Fuzzy c-Means %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % Segment segments an image by analyzing the histograms of the color
37 % components and identifying units that are homogeneous with the fuzzy
38 % c-means technique. The scale-space filter analyzes the histograms of
39 % the three color components of the image and identifies a set of
40 % classes. The extents of each class is used to coarsely segment the
41 % image with thresholding. The color associated with each class is
42 % determined by the mean color of all pixels within the extents of a
43 % particular class. Finally, any unclassified pixels are assigned to
44 % the closest class with the fuzzy c-means technique.
46 % The fuzzy c-Means algorithm can be summarized as follows:
48 % o Build a histogram, one for each color component of the image.
50 % o For each histogram, successively apply the scale-space filter and
51 % build an interval tree of zero crossings in the second derivative
52 % at each scale. Analyze this scale-space ``fingerprint'' to
53 % determine which peaks and valleys in the histogram are most
56 % o The fingerprint defines intervals on the axis of the histogram.
57 % Each interval contains either a minima or a maxima in the original
58 % signal. If each color component lies within the maxima interval,
59 % that pixel is considered ``classified'' and is assigned an unique
62 % o Any pixel that fails to be classified in the above thresholding
63 % pass is classified using the fuzzy c-Means technique. It is
64 % assigned to one of the classes discovered in the histogram analysis
67 % The fuzzy c-Means technique attempts to cluster a pixel by finding
68 % the local minima of the generalized within group sum of squared error
69 % objective function. A pixel is assigned to the closest class of
70 % which the fuzzy membership has a maximum value.
72 % Segment is strongly based on software written by Andy Gallo,
73 % University of Delaware.
75 % The following reference was used in creating this program:
77 % Young Won Lim, Sang Uk Lee, "On The Color Image Segmentation
78 % Algorithm Based on the Thresholding and the Fuzzy c-Means
79 % Techniques", Pattern Recognition, Volume 23, Number 9, pages
85 #include "MagickCore/studio.h"
86 #include "MagickCore/cache.h"
87 #include "MagickCore/color.h"
88 #include "MagickCore/colormap.h"
89 #include "MagickCore/colorspace.h"
90 #include "MagickCore/colorspace-private.h"
91 #include "MagickCore/exception.h"
92 #include "MagickCore/exception-private.h"
93 #include "MagickCore/image.h"
94 #include "MagickCore/image-private.h"
95 #include "MagickCore/memory_.h"
96 #include "MagickCore/monitor.h"
97 #include "MagickCore/monitor-private.h"
98 #include "MagickCore/pixel-accessor.h"
99 #include "MagickCore/quantize.h"
100 #include "MagickCore/quantum.h"
101 #include "MagickCore/quantum-private.h"
102 #include "MagickCore/segment.h"
103 #include "MagickCore/string_.h"
108 #define MaxDimension 3
109 #define DeltaTau 0.5f
110 #if defined(FastClassify)
111 #define WeightingExponent 2.0
112 #define SegmentPower(ratio) (ratio)
114 #define WeightingExponent 2.5
115 #define SegmentPower(ratio) pow(ratio,(double) (1.0/(weighting_exponent-1.0)));
120 Typedef declarations.
122 typedef struct _ExtentPacket
133 typedef struct _Cluster
148 typedef struct _IntervalTree
166 typedef struct _ZeroCrossing
177 Constant declarations.
189 static MagickRealType
190 OptimalTau(const ssize_t *,const double,const double,const double,
191 const double,short *);
194 DefineRegion(const short *,ExtentPacket *);
197 InitializeHistogram(const Image *,ssize_t **,ExceptionInfo *),
198 ScaleSpace(const ssize_t *,const MagickRealType,MagickRealType *),
199 ZeroCrossHistogram(MagickRealType *,const MagickRealType,short *);
202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
210 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
212 % Classify() defines one or more classes. Each pixel is thresholded to
213 % determine which class it belongs to. If the class is not identified it is
214 % assigned to the closest class based on the fuzzy c-Means technique.
216 % The format of the Classify method is:
218 % MagickBooleanType Classify(Image *image,short **extrema,
219 % const MagickRealType cluster_threshold,
220 % const MagickRealType weighting_exponent,
221 % const MagickBooleanType verbose)
223 % A description of each parameter follows.
225 % o image: the image.
227 % o extrema: Specifies a pointer to an array of integers. They
228 % represent the peaks and valleys of the histogram for each color
231 % o cluster_threshold: This MagickRealType represents the minimum number of
232 % pixels contained in a hexahedra before it can be considered valid
233 % (expressed as a percentage).
235 % o weighting_exponent: Specifies the membership weighting exponent.
237 % o verbose: A value greater than zero prints detailed information about
238 % the identified classes.
241 static MagickBooleanType Classify(Image *image,short **extrema,
242 const MagickRealType cluster_threshold,
243 const MagickRealType weighting_exponent,const MagickBooleanType verbose)
245 #define SegmentImageTag "Segment/Image"
276 register MagickRealType
289 cluster=(Cluster *) NULL;
290 head=(Cluster *) NULL;
291 (void) ResetMagickMemory(&red,0,sizeof(red));
292 (void) ResetMagickMemory(&green,0,sizeof(green));
293 (void) ResetMagickMemory(&blue,0,sizeof(blue));
294 while (DefineRegion(extrema[Red],&red) != 0)
297 while (DefineRegion(extrema[Green],&green) != 0)
300 while (DefineRegion(extrema[Blue],&blue) != 0)
303 Allocate a new class.
305 if (head != (Cluster *) NULL)
307 cluster->next=(Cluster *) AcquireMagickMemory(
308 sizeof(*cluster->next));
309 cluster=cluster->next;
313 cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster));
316 if (cluster == (Cluster *) NULL)
317 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
320 Initialize a new class.
324 cluster->green=green;
326 cluster->next=(Cluster *) NULL;
330 if (head == (Cluster *) NULL)
333 No classes were identified-- create one.
335 cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster));
336 if (cluster == (Cluster *) NULL)
337 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
340 Initialize a new class.
344 cluster->green=green;
346 cluster->next=(Cluster *) NULL;
350 Count the pixels for each cluster.
355 exception=(&image->exception);
356 image_view=AcquireCacheView(image);
357 for (y=0; y < (ssize_t) image->rows; y++)
359 register const Quantum
365 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
366 if (p == (const Quantum *) NULL)
368 for (x=0; x < (ssize_t) image->columns; x++)
370 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
371 if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) >=
372 (cluster->red.left-SafeMargin)) &&
373 ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) <=
374 (cluster->red.right+SafeMargin)) &&
375 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) >=
376 (cluster->green.left-SafeMargin)) &&
377 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) <=
378 (cluster->green.right+SafeMargin)) &&
379 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) >=
380 (cluster->blue.left-SafeMargin)) &&
381 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) <=
382 (cluster->blue.right+SafeMargin)))
388 cluster->red.center+=(MagickRealType) ScaleQuantumToChar(
389 GetPixelRed(image,p));
390 cluster->green.center+=(MagickRealType) ScaleQuantumToChar(
391 GetPixelGreen(image,p));
392 cluster->blue.center+=(MagickRealType) ScaleQuantumToChar(
393 GetPixelBlue(image,p));
397 p+=GetPixelChannels(image);
399 if (image->progress_monitor != (MagickProgressMonitor) NULL)
404 #if defined(MAGICKCORE_OPENMP_SUPPORT)
405 #pragma omp critical (MagickCore_Classify)
407 proceed=SetImageProgress(image,SegmentImageTag,progress++,
409 if (proceed == MagickFalse)
413 image_view=DestroyCacheView(image_view);
415 Remove clusters that do not meet minimum cluster threshold.
420 for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster)
422 next_cluster=cluster->next;
423 if ((cluster->count > 0) &&
424 (cluster->count >= (count*cluster_threshold/100.0)))
430 cluster->red.center/=cluster->count;
431 cluster->green.center/=cluster->count;
432 cluster->blue.center/=cluster->count;
434 last_cluster=cluster;
443 last_cluster->next=next_cluster;
444 cluster=(Cluster *) RelinquishMagickMemory(cluster);
446 number_clusters=(size_t) count;
447 if (verbose != MagickFalse)
450 Print cluster statistics.
452 (void) FormatLocaleFile(stdout,"Fuzzy C-means Statistics\n");
453 (void) FormatLocaleFile(stdout,"===================\n\n");
454 (void) FormatLocaleFile(stdout,"\tCluster Threshold = %g\n",(double)
456 (void) FormatLocaleFile(stdout,"\tWeighting Exponent = %g\n",(double)
458 (void) FormatLocaleFile(stdout,"\tTotal Number of Clusters = %.20g\n\n",
459 (double) number_clusters);
461 Print the total number of points per cluster.
463 (void) FormatLocaleFile(stdout,"\n\nNumber of Vectors Per Cluster\n");
464 (void) FormatLocaleFile(stdout,"=============================\n\n");
465 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
466 (void) FormatLocaleFile(stdout,"Cluster #%.20g = %.20g\n",(double)
467 cluster->id,(double) cluster->count);
469 Print the cluster extents.
471 (void) FormatLocaleFile(stdout,
472 "\n\n\nCluster Extents: (Vector Size: %d)\n",MaxDimension);
473 (void) FormatLocaleFile(stdout,"================");
474 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
476 (void) FormatLocaleFile(stdout,"\n\nCluster #%.20g\n\n",(double)
478 (void) FormatLocaleFile(stdout,
479 "%.20g-%.20g %.20g-%.20g %.20g-%.20g\n",(double)
480 cluster->red.left,(double) cluster->red.right,(double)
481 cluster->green.left,(double) cluster->green.right,(double)
482 cluster->blue.left,(double) cluster->blue.right);
485 Print the cluster center values.
487 (void) FormatLocaleFile(stdout,
488 "\n\n\nCluster Center Values: (Vector Size: %d)\n",MaxDimension);
489 (void) FormatLocaleFile(stdout,"=====================");
490 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
492 (void) FormatLocaleFile(stdout,"\n\nCluster #%.20g\n\n",(double)
494 (void) FormatLocaleFile(stdout,"%g %g %g\n",(double)
495 cluster->red.center,(double) cluster->green.center,(double)
496 cluster->blue.center);
498 (void) FormatLocaleFile(stdout,"\n");
500 if (number_clusters > 256)
501 ThrowBinaryException(ImageError,"TooManyClusters",image->filename);
503 Speed up distance calculations.
505 squares=(MagickRealType *) AcquireQuantumMemory(513UL,sizeof(*squares));
506 if (squares == (MagickRealType *) NULL)
507 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
510 for (i=(-255); i <= 255; i++)
511 squares[i]=(MagickRealType) i*(MagickRealType) i;
513 Allocate image colormap.
515 if (AcquireImageColormap(image,number_clusters) == MagickFalse)
516 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
519 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
521 image->colormap[i].red=ScaleCharToQuantum((unsigned char)
522 (cluster->red.center+0.5));
523 image->colormap[i].green=ScaleCharToQuantum((unsigned char)
524 (cluster->green.center+0.5));
525 image->colormap[i].blue=ScaleCharToQuantum((unsigned char)
526 (cluster->blue.center+0.5));
530 Do course grain classes.
532 exception=(&image->exception);
533 image_view=AcquireCacheView(image);
534 #if defined(MAGICKCORE_OPENMP_SUPPORT)
535 #pragma omp parallel for schedule(dynamic,4) shared(progress,status)
537 for (y=0; y < (ssize_t) image->rows; y++)
542 register const PixelPacket
551 if (status == MagickFalse)
553 q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception);
554 if (q == (const Quantum *) NULL)
559 for (x=0; x < (ssize_t) image->columns; x++)
561 SetPixelIndex(image,0,q);
562 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
564 if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,q)) >=
565 (cluster->red.left-SafeMargin)) &&
566 ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,q)) <=
567 (cluster->red.right+SafeMargin)) &&
568 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,q)) >=
569 (cluster->green.left-SafeMargin)) &&
570 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,q)) <=
571 (cluster->green.right+SafeMargin)) &&
572 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,q)) >=
573 (cluster->blue.left-SafeMargin)) &&
574 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,q)) <=
575 (cluster->blue.right+SafeMargin)))
580 SetPixelIndex(image,(Quantum) cluster->id,q);
584 if (cluster == (Cluster *) NULL)
598 Compute fuzzy membership.
601 for (j=0; j < (ssize_t) image->colors; j++)
605 distance_squared=squares[(ssize_t) ScaleQuantumToChar(
606 GetPixelRed(image,q))-(ssize_t)
607 ScaleQuantumToChar(p->red)]+squares[(ssize_t)
608 ScaleQuantumToChar(GetPixelGreen(image,q))-(ssize_t)
609 ScaleQuantumToChar(p->green)]+squares[(ssize_t)
610 ScaleQuantumToChar(GetPixelBlue(image,q))-(ssize_t)
611 ScaleQuantumToChar(p->blue)];
612 numerator=distance_squared;
613 for (k=0; k < (ssize_t) image->colors; k++)
616 distance_squared=squares[(ssize_t) ScaleQuantumToChar(
617 GetPixelRed(image,q))-(ssize_t)
618 ScaleQuantumToChar(p->red)]+squares[(ssize_t)
619 ScaleQuantumToChar(GetPixelGreen(image,q))-(ssize_t)
620 ScaleQuantumToChar(p->green)]+squares[(ssize_t)
621 ScaleQuantumToChar(GetPixelBlue(image,q))-(ssize_t)
622 ScaleQuantumToChar(p->blue)];
623 ratio=numerator/distance_squared;
624 sum+=SegmentPower(ratio);
626 if ((sum != 0.0) && ((1.0/sum) > local_minima))
631 local_minima=1.0/sum;
632 SetPixelIndex(image,(Quantum) j,q);
636 q+=GetPixelChannels(image);
638 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
640 if (image->progress_monitor != (MagickProgressMonitor) NULL)
645 #if defined(MAGICKCORE_OPENMP_SUPPORT)
646 #pragma omp critical (MagickCore_Classify)
648 proceed=SetImageProgress(image,SegmentImageTag,progress++,
650 if (proceed == MagickFalse)
654 image_view=DestroyCacheView(image_view);
655 status&=SyncImage(image);
657 Relinquish resources.
659 for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster)
661 next_cluster=cluster->next;
662 cluster=(Cluster *) RelinquishMagickMemory(cluster);
665 free_squares=squares;
666 free_squares=(MagickRealType *) RelinquishMagickMemory(free_squares);
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 + C o n s o l i d a t e C r o s s i n g s %
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % ConsolidateCrossings() guarantees that an even number of zero crossings
682 % always lie between two crossings.
684 % The format of the ConsolidateCrossings method is:
686 % ConsolidateCrossings(ZeroCrossing *zero_crossing,
687 % const size_t number_crossings)
689 % A description of each parameter follows.
691 % o zero_crossing: Specifies an array of structures of type ZeroCrossing.
693 % o number_crossings: This size_t specifies the number of elements
694 % in the zero_crossing array.
698 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
705 static inline ssize_t MagickMax(const ssize_t x,const ssize_t y)
712 static inline ssize_t MagickMin(const ssize_t x,const ssize_t y)
719 static void ConsolidateCrossings(ZeroCrossing *zero_crossing,
720 const size_t number_crossings)
736 Consolidate zero crossings.
738 for (i=(ssize_t) number_crossings-1; i >= 0; i--)
739 for (j=0; j <= 255; j++)
741 if (zero_crossing[i].crossings[j] == 0)
744 Find the entry that is closest to j and still preserves the
745 property that there are an even number of crossings between
748 for (k=j-1; k > 0; k--)
749 if (zero_crossing[i+1].crossings[k] != 0)
753 for (k=j+1; k < 255; k++)
754 if (zero_crossing[i+1].crossings[k] != 0)
756 right=MagickMin(k,255);
758 K is the zero crossing just left of j.
760 for (k=j-1; k > 0; k--)
761 if (zero_crossing[i].crossings[k] != 0)
766 Check center for an even number of crossings between k and j.
769 if (zero_crossing[i+1].crossings[j] != 0)
772 for (l=k+1; l < center; l++)
773 if (zero_crossing[i+1].crossings[l] != 0)
775 if (((count % 2) == 0) && (center != k))
779 Check left for an even number of crossings between k and j.
784 for (l=k+1; l < left; l++)
785 if (zero_crossing[i+1].crossings[l] != 0)
787 if (((count % 2) == 0) && (left != k))
791 Check right for an even number of crossings between k and j.
796 for (l=k+1; l < right; l++)
797 if (zero_crossing[i+1].crossings[l] != 0)
799 if (((count % 2) == 0) && (right != k))
802 l=(ssize_t) zero_crossing[i].crossings[j];
803 zero_crossing[i].crossings[j]=0;
805 zero_crossing[i].crossings[correct]=(short) l;
810 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
814 + D e f i n e R e g i o n %
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % DefineRegion() defines the left and right boundaries of a peak region.
822 % The format of the DefineRegion method is:
824 % ssize_t DefineRegion(const short *extrema,ExtentPacket *extents)
826 % A description of each parameter follows.
828 % o extrema: Specifies a pointer to an array of integers. They
829 % represent the peaks and valleys of the histogram for each color
832 % o extents: This pointer to an ExtentPacket represent the extends
833 % of a particular peak or valley of a color component.
836 static ssize_t DefineRegion(const short *extrema,ExtentPacket *extents)
839 Initialize to default values.
845 Find the left side (maxima).
847 for ( ; extents->index <= 255; extents->index++)
848 if (extrema[extents->index] > 0)
850 if (extents->index > 255)
851 return(MagickFalse); /* no left side - no region exists */
852 extents->left=extents->index;
854 Find the right side (minima).
856 for ( ; extents->index <= 255; extents->index++)
857 if (extrema[extents->index] < 0)
859 extents->right=extents->index-1;
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 + D e r i v a t i v e H i s t o g r a m %
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % DerivativeHistogram() determines the derivative of the histogram using
875 % central differencing.
877 % The format of the DerivativeHistogram method is:
879 % DerivativeHistogram(const MagickRealType *histogram,
880 % MagickRealType *derivative)
882 % A description of each parameter follows.
884 % o histogram: Specifies an array of MagickRealTypes representing the number
885 % of pixels for each intensity of a particular color component.
887 % o derivative: This array of MagickRealTypes is initialized by
888 % DerivativeHistogram to the derivative of the histogram using central
892 static void DerivativeHistogram(const MagickRealType *histogram,
893 MagickRealType *derivative)
900 Compute endpoints using second order polynomial interpolation.
903 derivative[0]=(-1.5*histogram[0]+2.0*histogram[1]-0.5*histogram[2]);
904 derivative[n]=(0.5*histogram[n-2]-2.0*histogram[n-1]+1.5*histogram[n]);
906 Compute derivative using central differencing.
908 for (i=1; i < n; i++)
909 derivative[i]=(histogram[i+1]-histogram[i-1])/2.0;
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 + G e t I m a g e D y n a m i c T h r e s h o l d %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % GetImageDynamicThreshold() returns the dynamic threshold for an image.
926 % The format of the GetImageDynamicThreshold method is:
928 % MagickBooleanType GetImageDynamicThreshold(const Image *image,
929 % const double cluster_threshold,const double smooth_threshold,
930 % PixelInfo *pixel,ExceptionInfo *exception)
932 % A description of each parameter follows.
934 % o image: the image.
936 % o cluster_threshold: This MagickRealType represents the minimum number of
937 % pixels contained in a hexahedra before it can be considered valid
938 % (expressed as a percentage).
940 % o smooth_threshold: the smoothing threshold eliminates noise in the second
941 % derivative of the histogram. As the value is increased, you can expect a
942 % smoother second derivative.
944 % o pixel: return the dynamic threshold here.
946 % o exception: return any errors or warnings in this structure.
949 MagickExport MagickBooleanType GetImageDynamicThreshold(const Image *image,
950 const double cluster_threshold,const double smooth_threshold,
951 PixelInfo *pixel,ExceptionInfo *exception)
972 register const Quantum
980 *extrema[MaxDimension];
984 *histogram[MaxDimension],
988 Allocate histogram and extrema.
990 assert(image != (Image *) NULL);
991 assert(image->signature == MagickSignature);
992 if (image->debug != MagickFalse)
993 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
994 GetPixelInfo(image,pixel);
995 for (i=0; i < MaxDimension; i++)
997 histogram[i]=(ssize_t *) AcquireQuantumMemory(256UL,sizeof(**histogram));
998 extrema[i]=(short *) AcquireQuantumMemory(256UL,sizeof(**histogram));
999 if ((histogram[i] == (ssize_t *) NULL) || (extrema[i] == (short *) NULL))
1001 for (i-- ; i >= 0; i--)
1003 extrema[i]=(short *) RelinquishMagickMemory(extrema[i]);
1004 histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]);
1006 (void) ThrowMagickException(exception,GetMagickModule(),
1007 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1008 return(MagickFalse);
1012 Initialize histogram.
1014 InitializeHistogram(image,histogram,exception);
1015 (void) OptimalTau(histogram[Red],Tau,0.2f,DeltaTau,
1016 (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Red]);
1017 (void) OptimalTau(histogram[Green],Tau,0.2f,DeltaTau,
1018 (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Green]);
1019 (void) OptimalTau(histogram[Blue],Tau,0.2f,DeltaTau,
1020 (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Blue]);
1024 cluster=(Cluster *) NULL;
1025 head=(Cluster *) NULL;
1026 (void) ResetMagickMemory(&red,0,sizeof(red));
1027 (void) ResetMagickMemory(&green,0,sizeof(green));
1028 (void) ResetMagickMemory(&blue,0,sizeof(blue));
1029 while (DefineRegion(extrema[Red],&red) != 0)
1032 while (DefineRegion(extrema[Green],&green) != 0)
1035 while (DefineRegion(extrema[Blue],&blue) != 0)
1038 Allocate a new class.
1040 if (head != (Cluster *) NULL)
1042 cluster->next=(Cluster *) AcquireMagickMemory(
1043 sizeof(*cluster->next));
1044 cluster=cluster->next;
1048 cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster));
1051 if (cluster == (Cluster *) NULL)
1053 (void) ThrowMagickException(exception,GetMagickModule(),
1054 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1056 return(MagickFalse);
1059 Initialize a new class.
1063 cluster->green=green;
1065 cluster->next=(Cluster *) NULL;
1069 if (head == (Cluster *) NULL)
1072 No classes were identified-- create one.
1074 cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster));
1075 if (cluster == (Cluster *) NULL)
1077 (void) ThrowMagickException(exception,GetMagickModule(),
1078 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1079 return(MagickFalse);
1082 Initialize a new class.
1086 cluster->green=green;
1088 cluster->next=(Cluster *) NULL;
1092 Count the pixels for each cluster.
1095 for (y=0; y < (ssize_t) image->rows; y++)
1097 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1098 if (p == (const Quantum *) NULL)
1100 for (x=0; x < (ssize_t) image->columns; x++)
1102 for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next)
1103 if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) >=
1104 (cluster->red.left-SafeMargin)) &&
1105 ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) <=
1106 (cluster->red.right+SafeMargin)) &&
1107 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) >=
1108 (cluster->green.left-SafeMargin)) &&
1109 ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) <=
1110 (cluster->green.right+SafeMargin)) &&
1111 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) >=
1112 (cluster->blue.left-SafeMargin)) &&
1113 ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) <=
1114 (cluster->blue.right+SafeMargin)))
1120 cluster->red.center+=(MagickRealType) ScaleQuantumToChar(
1121 GetPixelRed(image,p));
1122 cluster->green.center+=(MagickRealType) ScaleQuantumToChar(
1123 GetPixelGreen(image,p));
1124 cluster->blue.center+=(MagickRealType) ScaleQuantumToChar(
1125 GetPixelBlue(image,p));
1129 p+=GetPixelChannels(image);
1131 proceed=SetImageProgress(image,SegmentImageTag,(MagickOffsetType) y,
1133 if (proceed == MagickFalse)
1137 Remove clusters that do not meet minimum cluster threshold.
1142 for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster)
1144 next_cluster=cluster->next;
1145 if ((cluster->count > 0) &&
1146 (cluster->count >= (count*cluster_threshold/100.0)))
1152 cluster->red.center/=cluster->count;
1153 cluster->green.center/=cluster->count;
1154 cluster->blue.center/=cluster->count;
1156 last_cluster=cluster;
1162 if (cluster == head)
1165 last_cluster->next=next_cluster;
1166 cluster=(Cluster *) RelinquishMagickMemory(cluster);
1173 for (cluster=object; cluster->next != (Cluster *) NULL; )
1175 if (cluster->count < object->count)
1177 cluster=cluster->next;
1179 background=head->next;
1180 for (cluster=background; cluster->next != (Cluster *) NULL; )
1182 if (cluster->count > background->count)
1184 cluster=cluster->next;
1187 threshold=(background->red.center+object->red.center)/2.0;
1188 pixel->red=(MagickRealType) ScaleCharToQuantum((unsigned char)
1190 threshold=(background->green.center+object->green.center)/2.0;
1191 pixel->green=(MagickRealType) ScaleCharToQuantum((unsigned char)
1193 threshold=(background->blue.center+object->blue.center)/2.0;
1194 pixel->blue=(MagickRealType) ScaleCharToQuantum((unsigned char)
1197 Relinquish resources.
1199 for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster)
1201 next_cluster=cluster->next;
1202 cluster=(Cluster *) RelinquishMagickMemory(cluster);
1204 for (i=0; i < MaxDimension; i++)
1206 extrema[i]=(short *) RelinquishMagickMemory(extrema[i]);
1207 histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]);
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1217 + I n i t i a l i z e H i s t o g r a m %
1221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1223 % InitializeHistogram() computes the histogram for an image.
1225 % The format of the InitializeHistogram method is:
1227 % InitializeHistogram(const Image *image,ssize_t **histogram)
1229 % A description of each parameter follows.
1231 % o image: Specifies a pointer to an Image structure; returned from
1234 % o histogram: Specifies an array of integers representing the number
1235 % of pixels for each intensity of a particular color component.
1238 static void InitializeHistogram(const Image *image,ssize_t **histogram,
1239 ExceptionInfo *exception)
1241 register const Quantum
1252 Initialize histogram.
1254 for (i=0; i <= 255; i++)
1256 histogram[Red][i]=0;
1257 histogram[Green][i]=0;
1258 histogram[Blue][i]=0;
1260 for (y=0; y < (ssize_t) image->rows; y++)
1262 p=GetVirtualPixels(image,0,y,image->columns,1,exception);
1263 if (p == (const Quantum *) NULL)
1265 for (x=0; x < (ssize_t) image->columns; x++)
1267 histogram[Red][(ssize_t) ScaleQuantumToChar(GetPixelRed(image,p))]++;
1268 histogram[Green][(ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p))]++;
1269 histogram[Blue][(ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p))]++;
1270 p+=GetPixelChannels(image);
1276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1280 + I n i t i a l i z e I n t e r v a l T r e e %
1284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % InitializeIntervalTree() initializes an interval tree from the lists of
1289 % The format of the InitializeIntervalTree method is:
1291 % InitializeIntervalTree(IntervalTree **list,ssize_t *number_nodes,
1292 % IntervalTree *node)
1294 % A description of each parameter follows.
1296 % o zero_crossing: Specifies an array of structures of type ZeroCrossing.
1298 % o number_crossings: This size_t specifies the number of elements
1299 % in the zero_crossing array.
1303 static void InitializeList(IntervalTree **list,ssize_t *number_nodes,
1306 if (node == (IntervalTree *) NULL)
1308 if (node->child == (IntervalTree *) NULL)
1309 list[(*number_nodes)++]=node;
1310 InitializeList(list,number_nodes,node->sibling);
1311 InitializeList(list,number_nodes,node->child);
1314 static void MeanStability(IntervalTree *node)
1316 register IntervalTree
1319 if (node == (IntervalTree *) NULL)
1321 node->mean_stability=0.0;
1323 if (child != (IntervalTree *) NULL)
1328 register MagickRealType
1333 for ( ; child != (IntervalTree *) NULL; child=child->sibling)
1335 sum+=child->stability;
1338 node->mean_stability=sum/(MagickRealType) count;
1340 MeanStability(node->sibling);
1341 MeanStability(node->child);
1344 static void Stability(IntervalTree *node)
1346 if (node == (IntervalTree *) NULL)
1348 if (node->child == (IntervalTree *) NULL)
1349 node->stability=0.0;
1351 node->stability=node->tau-(node->child)->tau;
1352 Stability(node->sibling);
1353 Stability(node->child);
1356 static IntervalTree *InitializeIntervalTree(const ZeroCrossing *zero_crossing,
1357 const size_t number_crossings)
1375 Allocate interval tree.
1377 list=(IntervalTree **) AcquireQuantumMemory((size_t) TreeLength,
1379 if (list == (IntervalTree **) NULL)
1380 return((IntervalTree *) NULL);
1382 The root is the entire histogram.
1384 root=(IntervalTree *) AcquireMagickMemory(sizeof(*root));
1385 root->child=(IntervalTree *) NULL;
1386 root->sibling=(IntervalTree *) NULL;
1390 for (i=(-1); i < (ssize_t) number_crossings; i++)
1393 Initialize list with all nodes with no children.
1396 InitializeList(list,&number_nodes,root);
1400 for (j=0; j < number_nodes; j++)
1405 for (k=head->left+1; k < head->right; k++)
1407 if (zero_crossing[i+1].crossings[k] != 0)
1411 node->child=(IntervalTree *) AcquireMagickMemory(
1412 sizeof(*node->child));
1417 node->sibling=(IntervalTree *) AcquireMagickMemory(
1418 sizeof(*node->sibling));
1421 node->tau=zero_crossing[i+1].tau;
1422 node->child=(IntervalTree *) NULL;
1423 node->sibling=(IntervalTree *) NULL;
1429 if (left != head->left)
1431 node->sibling=(IntervalTree *) AcquireMagickMemory(
1432 sizeof(*node->sibling));
1434 node->tau=zero_crossing[i+1].tau;
1435 node->child=(IntervalTree *) NULL;
1436 node->sibling=(IntervalTree *) NULL;
1438 node->right=head->right;
1443 Determine the stability: difference between a nodes tau and its child.
1445 Stability(root->child);
1446 MeanStability(root->child);
1447 list=(IntervalTree **) RelinquishMagickMemory(list);
1452 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1456 + O p t i m a l T a u %
1460 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462 % OptimalTau() finds the optimal tau for each band of the histogram.
1464 % The format of the OptimalTau method is:
1466 % MagickRealType OptimalTau(const ssize_t *histogram,const double max_tau,
1467 % const double min_tau,const double delta_tau,
1468 % const double smooth_threshold,short *extrema)
1470 % A description of each parameter follows.
1472 % o histogram: Specifies an array of integers representing the number
1473 % of pixels for each intensity of a particular color component.
1475 % o extrema: Specifies a pointer to an array of integers. They
1476 % represent the peaks and valleys of the histogram for each color
1481 static void ActiveNodes(IntervalTree **list,ssize_t *number_nodes,
1484 if (node == (IntervalTree *) NULL)
1486 if (node->stability >= node->mean_stability)
1488 list[(*number_nodes)++]=node;
1489 ActiveNodes(list,number_nodes,node->sibling);
1493 ActiveNodes(list,number_nodes,node->sibling);
1494 ActiveNodes(list,number_nodes,node->child);
1498 static void FreeNodes(IntervalTree *node)
1500 if (node == (IntervalTree *) NULL)
1502 FreeNodes(node->sibling);
1503 FreeNodes(node->child);
1504 node=(IntervalTree *) RelinquishMagickMemory(node);
1507 static MagickRealType OptimalTau(const ssize_t *histogram,const double max_tau,
1508 const double min_tau,const double delta_tau,const double smooth_threshold,
1544 Allocate interval tree.
1546 list=(IntervalTree **) AcquireQuantumMemory((size_t) TreeLength,
1548 if (list == (IntervalTree **) NULL)
1551 Allocate zero crossing list.
1553 count=(size_t) ((max_tau-min_tau)/delta_tau)+2;
1554 zero_crossing=(ZeroCrossing *) AcquireQuantumMemory((size_t) count,
1555 sizeof(*zero_crossing));
1556 if (zero_crossing == (ZeroCrossing *) NULL)
1558 for (i=0; i < (ssize_t) count; i++)
1559 zero_crossing[i].tau=(-1.0);
1561 Initialize zero crossing list.
1563 derivative=(MagickRealType *) AcquireQuantumMemory(256,sizeof(*derivative));
1564 second_derivative=(MagickRealType *) AcquireQuantumMemory(256,
1565 sizeof(*second_derivative));
1566 if ((derivative == (MagickRealType *) NULL) ||
1567 (second_derivative == (MagickRealType *) NULL))
1568 ThrowFatalException(ResourceLimitFatalError,
1569 "UnableToAllocateDerivatives");
1571 for (tau=max_tau; tau >= min_tau; tau-=delta_tau)
1573 zero_crossing[i].tau=tau;
1574 ScaleSpace(histogram,tau,zero_crossing[i].histogram);
1575 DerivativeHistogram(zero_crossing[i].histogram,derivative);
1576 DerivativeHistogram(derivative,second_derivative);
1577 ZeroCrossHistogram(second_derivative,smooth_threshold,
1578 zero_crossing[i].crossings);
1582 Add an entry for the original histogram.
1584 zero_crossing[i].tau=0.0;
1585 for (j=0; j <= 255; j++)
1586 zero_crossing[i].histogram[j]=(MagickRealType) histogram[j];
1587 DerivativeHistogram(zero_crossing[i].histogram,derivative);
1588 DerivativeHistogram(derivative,second_derivative);
1589 ZeroCrossHistogram(second_derivative,smooth_threshold,
1590 zero_crossing[i].crossings);
1591 number_crossings=(size_t) i;
1592 derivative=(MagickRealType *) RelinquishMagickMemory(derivative);
1593 second_derivative=(MagickRealType *)
1594 RelinquishMagickMemory(second_derivative);
1596 Ensure the scale-space fingerprints form lines in scale-space, not loops.
1598 ConsolidateCrossings(zero_crossing,number_crossings);
1600 Force endpoints to be included in the interval.
1602 for (i=0; i <= (ssize_t) number_crossings; i++)
1604 for (j=0; j < 255; j++)
1605 if (zero_crossing[i].crossings[j] != 0)
1607 zero_crossing[i].crossings[0]=(-zero_crossing[i].crossings[j]);
1608 for (j=255; j > 0; j--)
1609 if (zero_crossing[i].crossings[j] != 0)
1611 zero_crossing[i].crossings[255]=(-zero_crossing[i].crossings[j]);
1614 Initialize interval tree.
1616 root=InitializeIntervalTree(zero_crossing,number_crossings);
1617 if (root == (IntervalTree *) NULL)
1620 Find active nodes: stability is greater (or equal) to the mean stability of
1624 ActiveNodes(list,&number_nodes,root->child);
1628 for (i=0; i <= 255; i++)
1630 for (i=0; i < number_nodes; i++)
1633 Find this tau in zero crossings list.
1637 for (j=0; j <= (ssize_t) number_crossings; j++)
1638 if (zero_crossing[j].tau == node->tau)
1641 Find the value of the peak.
1643 peak=zero_crossing[k].crossings[node->right] == -1 ? MagickTrue :
1646 value=zero_crossing[k].histogram[index];
1647 for (x=node->left; x <= node->right; x++)
1649 if (peak != MagickFalse)
1651 if (zero_crossing[k].histogram[x] > value)
1653 value=zero_crossing[k].histogram[x];
1658 if (zero_crossing[k].histogram[x] < value)
1660 value=zero_crossing[k].histogram[x];
1664 for (x=node->left; x <= node->right; x++)
1668 if (peak != MagickFalse)
1669 extrema[x]=(short) index;
1671 extrema[x]=(short) (-index);
1675 Determine the average tau.
1678 for (i=0; i < number_nodes; i++)
1679 average_tau+=list[i]->tau;
1680 average_tau/=(MagickRealType) number_nodes;
1682 Relinquish resources.
1685 zero_crossing=(ZeroCrossing *) RelinquishMagickMemory(zero_crossing);
1686 list=(IntervalTree **) RelinquishMagickMemory(list);
1687 return(average_tau);
1691 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1695 + S c a l e S p a c e %
1699 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1701 % ScaleSpace() performs a scale-space filter on the 1D histogram.
1703 % The format of the ScaleSpace method is:
1705 % ScaleSpace(const ssize_t *histogram,const MagickRealType tau,
1706 % MagickRealType *scale_histogram)
1708 % A description of each parameter follows.
1710 % o histogram: Specifies an array of MagickRealTypes representing the number
1711 % of pixels for each intensity of a particular color component.
1715 static void ScaleSpace(const ssize_t *histogram,const MagickRealType tau,
1716 MagickRealType *scale_histogram)
1728 gamma=(MagickRealType *) AcquireQuantumMemory(256,sizeof(*gamma));
1729 if (gamma == (MagickRealType *) NULL)
1730 ThrowFatalException(ResourceLimitFatalError,
1731 "UnableToAllocateGammaMap");
1732 alpha=1.0/(tau*sqrt(2.0*MagickPI));
1733 beta=(-1.0/(2.0*tau*tau));
1734 for (x=0; x <= 255; x++)
1736 for (x=0; x <= 255; x++)
1738 gamma[x]=exp((double) beta*x*x);
1739 if (gamma[x] < MagickEpsilon)
1742 for (x=0; x <= 255; x++)
1745 for (u=0; u <= 255; u++)
1746 sum+=(MagickRealType) histogram[u]*gamma[MagickAbsoluteValue(x-u)];
1747 scale_histogram[x]=alpha*sum;
1749 gamma=(MagickRealType *) RelinquishMagickMemory(gamma);
1753 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1757 % S e g m e n t I m a g e %
1761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1763 % SegmentImage() segment an image by analyzing the histograms of the color
1764 % components and identifying units that are homogeneous with the fuzzy
1765 % C-means technique.
1767 % The format of the SegmentImage method is:
1769 % MagickBooleanType SegmentImage(Image *image,
1770 % const ColorspaceType colorspace,const MagickBooleanType verbose,
1771 % const double cluster_threshold,const double smooth_threshold)
1773 % A description of each parameter follows.
1775 % o image: the image.
1777 % o colorspace: Indicate the colorspace.
1779 % o verbose: Set to MagickTrue to print detailed information about the
1780 % identified classes.
1782 % o cluster_threshold: This represents the minimum number of pixels
1783 % contained in a hexahedra before it can be considered valid (expressed
1786 % o smooth_threshold: the smoothing threshold eliminates noise in the second
1787 % derivative of the histogram. As the value is increased, you can expect a
1788 % smoother second derivative.
1791 MagickExport MagickBooleanType SegmentImage(Image *image,
1792 const ColorspaceType colorspace,const MagickBooleanType verbose,
1793 const double cluster_threshold,const double smooth_threshold)
1802 *extrema[MaxDimension];
1805 *histogram[MaxDimension];
1808 Allocate histogram and extrema.
1810 assert(image != (Image *) NULL);
1811 assert(image->signature == MagickSignature);
1812 if (image->debug != MagickFalse)
1813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1814 for (i=0; i < MaxDimension; i++)
1816 histogram[i]=(ssize_t *) AcquireQuantumMemory(256,sizeof(**histogram));
1817 extrema[i]=(short *) AcquireQuantumMemory(256,sizeof(**extrema));
1818 if ((histogram[i] == (ssize_t *) NULL) || (extrema[i] == (short *) NULL))
1820 for (i-- ; i >= 0; i--)
1822 extrema[i]=(short *) RelinquishMagickMemory(extrema[i]);
1823 histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]);
1825 ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed",
1829 if (IsRGBColorspace(colorspace) == MagickFalse)
1830 (void) TransformImageColorspace(image,colorspace);
1832 Initialize histogram.
1834 InitializeHistogram(image,histogram,&image->exception);
1835 (void) OptimalTau(histogram[Red],Tau,0.2,DeltaTau,
1836 smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Red]);
1837 (void) OptimalTau(histogram[Green],Tau,0.2,DeltaTau,
1838 smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Green]);
1839 (void) OptimalTau(histogram[Blue],Tau,0.2,DeltaTau,
1840 smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Blue]);
1842 Classify using the fuzzy c-Means technique.
1844 status=Classify(image,extrema,cluster_threshold,WeightingExponent,verbose);
1845 if (IsRGBColorspace(colorspace) == MagickFalse)
1846 (void) TransformImageColorspace(image,colorspace);
1848 Relinquish resources.
1850 for (i=0; i < MaxDimension; i++)
1852 extrema[i]=(short *) RelinquishMagickMemory(extrema[i]);
1853 histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]);
1859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1863 + Z e r o C r o s s H i s t o g r a m %
1867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1869 % ZeroCrossHistogram() find the zero crossings in a histogram and marks
1870 % directions as: 1 is negative to positive; 0 is zero crossing; and -1
1871 % is positive to negative.
1873 % The format of the ZeroCrossHistogram method is:
1875 % ZeroCrossHistogram(MagickRealType *second_derivative,
1876 % const MagickRealType smooth_threshold,short *crossings)
1878 % A description of each parameter follows.
1880 % o second_derivative: Specifies an array of MagickRealTypes representing the
1881 % second derivative of the histogram of a particular color component.
1883 % o crossings: This array of integers is initialized with
1884 % -1, 0, or 1 representing the slope of the first derivative of the
1885 % of a particular color component.
1888 static void ZeroCrossHistogram(MagickRealType *second_derivative,
1889 const MagickRealType smooth_threshold,short *crossings)
1898 Merge low numbers to zero to help prevent noise.
1900 for (i=0; i <= 255; i++)
1901 if ((second_derivative[i] < smooth_threshold) &&
1902 (second_derivative[i] >= -smooth_threshold))
1903 second_derivative[i]=0.0;
1905 Mark zero crossings.
1908 for (i=0; i <= 255; i++)
1911 if (second_derivative[i] < 0.0)
1918 if (second_derivative[i] > 0.0)