2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % FFFFF EEEEE AAA TTTTT U U RRRR EEEEE %
7 % F E A A T U U R R E %
8 % FFF EEE AAAAA T U U RRRR EEE %
9 % F E A A T U U R R E %
10 % F EEEEE A A T UUU R R EEEEE %
13 % MagickCore Image Feature Methods %
20 % Copyright 1999-2014 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "MagickCore/studio.h"
44 #include "MagickCore/animate.h"
45 #include "MagickCore/artifact.h"
46 #include "MagickCore/blob.h"
47 #include "MagickCore/blob-private.h"
48 #include "MagickCore/cache.h"
49 #include "MagickCore/cache-private.h"
50 #include "MagickCore/cache-view.h"
51 #include "MagickCore/client.h"
52 #include "MagickCore/color.h"
53 #include "MagickCore/color-private.h"
54 #include "MagickCore/colorspace.h"
55 #include "MagickCore/colorspace-private.h"
56 #include "MagickCore/composite.h"
57 #include "MagickCore/composite-private.h"
58 #include "MagickCore/compress.h"
59 #include "MagickCore/constitute.h"
60 #include "MagickCore/display.h"
61 #include "MagickCore/draw.h"
62 #include "MagickCore/enhance.h"
63 #include "MagickCore/exception.h"
64 #include "MagickCore/exception-private.h"
65 #include "MagickCore/feature.h"
66 #include "MagickCore/gem.h"
67 #include "MagickCore/geometry.h"
68 #include "MagickCore/list.h"
69 #include "MagickCore/image-private.h"
70 #include "MagickCore/magic.h"
71 #include "MagickCore/magick.h"
72 #include "MagickCore/matrix.h"
73 #include "MagickCore/memory_.h"
74 #include "MagickCore/module.h"
75 #include "MagickCore/monitor.h"
76 #include "MagickCore/monitor-private.h"
77 #include "MagickCore/morphology-private.h"
78 #include "MagickCore/option.h"
79 #include "MagickCore/paint.h"
80 #include "MagickCore/pixel-accessor.h"
81 #include "MagickCore/profile.h"
82 #include "MagickCore/property.h"
83 #include "MagickCore/quantize.h"
84 #include "MagickCore/quantum-private.h"
85 #include "MagickCore/random_.h"
86 #include "MagickCore/resource_.h"
87 #include "MagickCore/segment.h"
88 #include "MagickCore/semaphore.h"
89 #include "MagickCore/signature-private.h"
90 #include "MagickCore/string_.h"
91 #include "MagickCore/thread-private.h"
92 #include "MagickCore/timer.h"
93 #include "MagickCore/utility.h"
94 #include "MagickCore/version.h"
97 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101 % C a n n y E d g e I m a g e %
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 % CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of
110 % The format of the EdgeImage method is:
112 % Image *CannyEdgeImage(const Image *image,const double radius,
113 % const double sigma,const double lower_percent,
114 % const double upper_percent,ExceptionInfo *exception)
116 % A description of each parameter follows:
118 % o image: the image.
120 % o radius: the radius of the gaussian smoothing filter.
122 % o sigma: the sigma of the gaussian smoothing filter.
124 % o lower_precent: percentage of edge pixels in the lower threshold.
126 % o upper_percent: percentage of edge pixels in the upper threshold.
128 % o exception: return any errors or warnings in this structure.
132 typedef struct _CannyInfo
146 static inline MagickBooleanType IsAuthenticPixel(const Image *image,
147 const ssize_t x,const ssize_t y)
149 if ((x < 0) || (x >= (ssize_t) image->columns))
151 if ((y < 0) || (y >= (ssize_t) image->rows))
156 static MagickBooleanType TraceEdges(Image *edge_image,CacheView *edge_view,
157 MatrixInfo *canny_cache,const ssize_t x,const ssize_t y,
158 const double lower_threshold,ExceptionInfo *exception)
173 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
174 if (q == (Quantum *) NULL)
177 status=SyncCacheViewAuthenticPixels(edge_view,exception);
178 if (status == MagickFalse)
179 return(MagickFalse);;
180 if (GetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
184 if (SetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
192 status=GetMatrixElement(canny_cache,i,0,&edge);
193 if (status == MagickFalse)
195 for (v=(-1); v <= 1; v++)
200 for (u=(-1); u <= 1; u++)
202 if ((u == 0) && (v == 0))
204 if (IsAuthenticPixel(edge_image,edge.x+u,edge.y+v) == MagickFalse)
207 Not an edge if gradient value is below the lower threshold.
209 q=GetCacheViewAuthenticPixels(edge_view,edge.x+u,edge.y+v,1,1,
211 if (q == (Quantum *) NULL)
213 status=GetMatrixElement(canny_cache,edge.x+u,edge.y+v,&pixel);
214 if (status == MagickFalse)
216 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
217 (pixel.intensity >= lower_threshold))
220 status=SyncCacheViewAuthenticPixels(edge_view,exception);
221 if (status == MagickFalse)
225 status=SetMatrixElement(canny_cache,i,0,&edge);
226 if (status == MagickFalse)
236 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
237 const double sigma,const double lower_percent,const double upper_percent,
238 ExceptionInfo *exception)
247 geometry[MaxTextExtent];
270 assert(image != (const Image *) NULL);
271 assert(image->signature == MagickSignature);
272 if (image->debug != MagickFalse)
273 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
274 assert(exception != (ExceptionInfo *) NULL);
275 assert(exception->signature == MagickSignature);
279 (void) FormatLocaleString(geometry,MaxTextExtent,
280 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
281 kernel_info=AcquireKernelInfo(geometry);
282 if (kernel_info == (KernelInfo *) NULL)
283 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
284 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
285 UndefinedCompositeOp,0.0,exception);
286 kernel_info=DestroyKernelInfo(kernel_info);
287 if (edge_image == (Image *) NULL)
288 return((Image *) NULL);
289 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
291 edge_image=DestroyImage(edge_image);
292 return((Image *) NULL);
295 Find the intensity gradient of the image.
297 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
298 sizeof(CannyInfo),exception);
299 if (canny_cache == (MatrixInfo *) NULL)
301 edge_image=DestroyImage(edge_image);
302 return((Image *) NULL);
305 edge_view=AcquireVirtualCacheView(edge_image,exception);
306 #if defined(MAGICKCORE_OPENMP_SUPPORT)
307 #pragma omp parallel for schedule(static,4) shared(status) \
308 magick_threads(edge_image,edge_image,edge_image->rows,1)
310 for (y=0; y < (ssize_t) edge_image->rows; y++)
312 register const Quantum
318 if (status == MagickFalse)
320 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
322 if (p == (const Quantum *) NULL)
327 for (x=0; x < (ssize_t) edge_image->columns; x++)
336 register const Quantum
337 *restrict kernel_pixels;
354 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
358 for (v=0; v < 2; v++)
363 for (u=0; u < 2; u++)
368 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
369 dx+=0.5*Gx[v][u]*intensity;
370 dy+=0.5*Gy[v][u]*intensity;
372 kernel_pixels+=edge_image->columns+1;
374 pixel.magnitude=hypot(dx,dy);
376 if (fabs(dx) > MagickEpsilon)
384 if (slope < -2.41421356237)
387 if (slope < -0.414213562373)
394 if (slope > 2.41421356237)
397 if (slope > 0.414213562373)
403 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
405 p+=GetPixelChannels(edge_image);
408 edge_view=DestroyCacheView(edge_view);
410 Non-maxima suppression, remove pixels that are not considered to be part
413 (void) GetMatrixElement(canny_cache,0,0,&pixel);
416 edge_view=AcquireAuthenticCacheView(edge_image,exception);
417 #if defined(MAGICKCORE_OPENMP_SUPPORT)
418 #pragma omp parallel for schedule(static,4) shared(status) \
419 magick_threads(edge_image,edge_image,edge_image->rows,1)
421 for (y=0; y < (ssize_t) edge_image->rows; y++)
429 if (status == MagickFalse)
431 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
433 if (q == (Quantum *) NULL)
438 for (x=0; x < (ssize_t) edge_image->columns; x++)
445 (void) GetMatrixElement(canny_cache,x,y,&pixel);
446 switch (pixel.orientation)
452 0 degrees, north and south.
454 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
455 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
461 45 degrees, northwest and southeast.
463 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
464 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
470 90 degrees, east and west.
472 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
473 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
479 135 degrees, northeast and southwest.
481 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
482 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
486 pixel.intensity=pixel.magnitude;
487 if ((pixel.magnitude < alpha_pixel.magnitude) ||
488 (pixel.magnitude < beta_pixel.magnitude))
490 (void) SetMatrixElement(canny_cache,x,y,&pixel);
491 #if defined(MAGICKCORE_OPENMP_SUPPORT)
492 #pragma omp critical (MagickCore_CannyEdgeImage)
495 if (pixel.intensity < min)
497 if (pixel.intensity > max)
501 q+=GetPixelChannels(edge_image);
503 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
506 edge_view=DestroyCacheView(edge_view);
508 Estimate hysteresis threshold.
510 lower_threshold=lower_percent*(max-min)+min;
511 upper_threshold=upper_percent*(max-min)+min;
513 Hysteresis threshold.
515 edge_view=AcquireAuthenticCacheView(edge_image,exception);
516 for (y=0; y < (ssize_t) edge_image->rows; y++)
521 if (status == MagickFalse)
523 for (x=0; x < (ssize_t) edge_image->columns; x++)
528 register const Quantum
532 Edge if pixel gradient higher than upper threshold.
534 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
535 if (p == (const Quantum *) NULL)
537 status=GetMatrixElement(canny_cache,x,y,&pixel);
538 if (status == MagickFalse)
540 if ((GetPixelIntensity(edge_image,p) == 0.0) &&
541 (pixel.intensity >= upper_threshold))
542 status=TraceEdges(edge_image,edge_view,canny_cache,x,y,lower_threshold,
546 edge_view=DestroyCacheView(edge_view);
550 canny_cache=DestroyMatrixInfo(canny_cache);
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % G e t I m a g e F e a t u r e s %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % GetImageFeatures() returns features for each channel in the image in
566 % each of four directions (horizontal, vertical, left and right diagonals)
567 % for the specified distance. The features include the angular second
568 % moment, contrast, correlation, sum of squares: variance, inverse difference
569 % moment, sum average, sum varience, sum entropy, entropy, difference variance,% difference entropy, information measures of correlation 1, information
570 % measures of correlation 2, and maximum correlation coefficient. You can
571 % access the red channel contrast, for example, like this:
573 % channel_features=GetImageFeatures(image,1,exception);
574 % contrast=channel_features[RedPixelChannel].contrast[0];
576 % Use MagickRelinquishMemory() to free the features buffer.
578 % The format of the GetImageFeatures method is:
580 % ChannelFeatures *GetImageFeatures(const Image *image,
581 % const size_t distance,ExceptionInfo *exception)
583 % A description of each parameter follows:
585 % o image: the image.
587 % o distance: the distance.
589 % o exception: return any errors or warnings in this structure.
593 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
600 static inline double MagickLog10(const double x)
602 #define Log10Epsilon (1.0e-11)
604 if (fabs(x) < Log10Epsilon)
605 return(log10(Log10Epsilon));
606 return(log10(fabs(x)));
609 MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
610 const size_t distance,ExceptionInfo *exception)
612 typedef struct _ChannelStatistics
615 direction[4]; /* horizontal, vertical, left and right diagonals */
660 assert(image != (Image *) NULL);
661 assert(image->signature == MagickSignature);
662 if (image->debug != MagickFalse)
663 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
664 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
665 return((ChannelFeatures *) NULL);
666 length=CompositeChannels+1UL;
667 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
668 sizeof(*channel_features));
669 if (channel_features == (ChannelFeatures *) NULL)
670 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
671 (void) ResetMagickMemory(channel_features,0,length*
672 sizeof(*channel_features));
676 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
677 if (grays == (PixelPacket *) NULL)
679 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
681 (void) ThrowMagickException(exception,GetMagickModule(),
682 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
683 return(channel_features);
685 for (i=0; i <= (ssize_t) MaxMap; i++)
688 grays[i].green=(~0U);
690 grays[i].alpha=(~0U);
691 grays[i].black=(~0U);
694 image_view=AcquireVirtualCacheView(image,exception);
695 #if defined(MAGICKCORE_OPENMP_SUPPORT)
696 #pragma omp parallel for schedule(static,4) shared(status) \
697 magick_threads(image,image,image->rows,1)
699 for (y=0; y < (ssize_t) image->rows; y++)
701 register const Quantum
707 if (status == MagickFalse)
709 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
710 if (p == (const Quantum *) NULL)
715 for (x=0; x < (ssize_t) image->columns; x++)
717 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
718 ScaleQuantumToMap(GetPixelRed(image,p));
719 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
720 ScaleQuantumToMap(GetPixelGreen(image,p));
721 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
722 ScaleQuantumToMap(GetPixelBlue(image,p));
723 if (image->colorspace == CMYKColorspace)
724 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
725 ScaleQuantumToMap(GetPixelBlack(image,p));
726 if (image->alpha_trait == BlendPixelTrait)
727 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
728 ScaleQuantumToMap(GetPixelAlpha(image,p));
729 p+=GetPixelChannels(image);
732 image_view=DestroyCacheView(image_view);
733 if (status == MagickFalse)
735 grays=(PixelPacket *) RelinquishMagickMemory(grays);
736 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
738 return(channel_features);
740 (void) ResetMagickMemory(&gray,0,sizeof(gray));
741 for (i=0; i <= (ssize_t) MaxMap; i++)
743 if (grays[i].red != ~0U)
744 grays[gray.red++].red=grays[i].red;
745 if (grays[i].green != ~0U)
746 grays[gray.green++].green=grays[i].green;
747 if (grays[i].blue != ~0U)
748 grays[gray.blue++].blue=grays[i].blue;
749 if (image->colorspace == CMYKColorspace)
750 if (grays[i].black != ~0U)
751 grays[gray.black++].black=grays[i].black;
752 if (image->alpha_trait == BlendPixelTrait)
753 if (grays[i].alpha != ~0U)
754 grays[gray.alpha++].alpha=grays[i].alpha;
757 Allocate spatial dependence matrix.
759 number_grays=gray.red;
760 if (gray.green > number_grays)
761 number_grays=gray.green;
762 if (gray.blue > number_grays)
763 number_grays=gray.blue;
764 if (image->colorspace == CMYKColorspace)
765 if (gray.black > number_grays)
766 number_grays=gray.black;
767 if (image->alpha_trait == BlendPixelTrait)
768 if (gray.alpha > number_grays)
769 number_grays=gray.alpha;
770 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
771 sizeof(*cooccurrence));
772 density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
774 density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
775 sizeof(*density_xy));
776 density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
778 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
779 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
780 if ((cooccurrence == (ChannelStatistics **) NULL) ||
781 (density_x == (ChannelStatistics *) NULL) ||
782 (density_xy == (ChannelStatistics *) NULL) ||
783 (density_y == (ChannelStatistics *) NULL) ||
784 (Q == (ChannelStatistics **) NULL) ||
785 (sum == (ChannelStatistics *) NULL))
787 if (Q != (ChannelStatistics **) NULL)
789 for (i=0; i < (ssize_t) number_grays; i++)
790 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
791 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
793 if (sum != (ChannelStatistics *) NULL)
794 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
795 if (density_y != (ChannelStatistics *) NULL)
796 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
797 if (density_xy != (ChannelStatistics *) NULL)
798 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
799 if (density_x != (ChannelStatistics *) NULL)
800 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
801 if (cooccurrence != (ChannelStatistics **) NULL)
803 for (i=0; i < (ssize_t) number_grays; i++)
804 cooccurrence[i]=(ChannelStatistics *)
805 RelinquishMagickMemory(cooccurrence[i]);
806 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
809 grays=(PixelPacket *) RelinquishMagickMemory(grays);
810 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
812 (void) ThrowMagickException(exception,GetMagickModule(),
813 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
814 return(channel_features);
816 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
817 (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x));
818 (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
819 (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y));
820 (void) ResetMagickMemory(&mean,0,sizeof(mean));
821 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
822 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
823 (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy));
824 (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x));
825 (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy));
826 (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1));
827 (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2));
828 (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y));
829 (void) ResetMagickMemory(&variance,0,sizeof(variance));
830 for (i=0; i < (ssize_t) number_grays; i++)
832 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
833 sizeof(**cooccurrence));
834 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
835 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
836 (Q[i] == (ChannelStatistics *) NULL))
838 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
839 sizeof(**cooccurrence));
840 (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q));
842 if (i < (ssize_t) number_grays)
844 for (i--; i >= 0; i--)
846 if (Q[i] != (ChannelStatistics *) NULL)
847 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
848 if (cooccurrence[i] != (ChannelStatistics *) NULL)
849 cooccurrence[i]=(ChannelStatistics *)
850 RelinquishMagickMemory(cooccurrence[i]);
852 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
853 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
854 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
855 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
856 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
857 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
858 grays=(PixelPacket *) RelinquishMagickMemory(grays);
859 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
861 (void) ThrowMagickException(exception,GetMagickModule(),
862 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
863 return(channel_features);
866 Initialize spatial dependence matrix.
869 image_view=AcquireVirtualCacheView(image,exception);
870 for (y=0; y < (ssize_t) image->rows; y++)
872 register const Quantum
884 if (status == MagickFalse)
886 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+
887 2*distance,distance+2,exception);
888 if (p == (const Quantum *) NULL)
893 p+=distance*GetPixelChannels(image);;
894 for (x=0; x < (ssize_t) image->columns; x++)
896 for (i=0; i < 4; i++)
904 Horizontal adjacency.
906 offset=(ssize_t) distance;
914 offset=(ssize_t) (image->columns+2*distance);
920 Right diagonal adjacency.
922 offset=(ssize_t) ((image->columns+2*distance)-distance);
928 Left diagonal adjacency.
930 offset=(ssize_t) ((image->columns+2*distance)+distance);
936 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
938 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
940 cooccurrence[u][v].direction[i].red++;
941 cooccurrence[v][u].direction[i].red++;
944 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
946 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
948 cooccurrence[u][v].direction[i].green++;
949 cooccurrence[v][u].direction[i].green++;
952 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
954 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
956 cooccurrence[u][v].direction[i].blue++;
957 cooccurrence[v][u].direction[i].blue++;
958 if (image->colorspace == CMYKColorspace)
962 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
964 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
966 cooccurrence[u][v].direction[i].black++;
967 cooccurrence[v][u].direction[i].black++;
969 if (image->alpha_trait == BlendPixelTrait)
973 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
975 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
977 cooccurrence[u][v].direction[i].alpha++;
978 cooccurrence[v][u].direction[i].alpha++;
981 p+=GetPixelChannels(image);
984 grays=(PixelPacket *) RelinquishMagickMemory(grays);
985 image_view=DestroyCacheView(image_view);
986 if (status == MagickFalse)
988 for (i=0; i < (ssize_t) number_grays; i++)
989 cooccurrence[i]=(ChannelStatistics *)
990 RelinquishMagickMemory(cooccurrence[i]);
991 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
992 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
994 (void) ThrowMagickException(exception,GetMagickModule(),
995 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
996 return(channel_features);
999 Normalize spatial dependence matrix.
1001 for (i=0; i < 4; i++)
1015 Horizontal adjacency.
1017 normalize=2.0*image->rows*(image->columns-distance);
1025 normalize=2.0*(image->rows-distance)*image->columns;
1031 Right diagonal adjacency.
1033 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1039 Left diagonal adjacency.
1041 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1045 normalize=PerceptibleReciprocal(normalize);
1046 for (y=0; y < (ssize_t) number_grays; y++)
1051 for (x=0; x < (ssize_t) number_grays; x++)
1053 cooccurrence[x][y].direction[i].red*=normalize;
1054 cooccurrence[x][y].direction[i].green*=normalize;
1055 cooccurrence[x][y].direction[i].blue*=normalize;
1056 if (image->colorspace == CMYKColorspace)
1057 cooccurrence[x][y].direction[i].black*=normalize;
1058 if (image->alpha_trait == BlendPixelTrait)
1059 cooccurrence[x][y].direction[i].alpha*=normalize;
1064 Compute texture features.
1066 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1067 #pragma omp parallel for schedule(static,4) shared(status) \
1068 magick_threads(image,image,number_grays,1)
1070 for (i=0; i < 4; i++)
1075 for (y=0; y < (ssize_t) number_grays; y++)
1080 for (x=0; x < (ssize_t) number_grays; x++)
1083 Angular second moment: measure of homogeneity of the image.
1085 channel_features[RedPixelChannel].angular_second_moment[i]+=
1086 cooccurrence[x][y].direction[i].red*
1087 cooccurrence[x][y].direction[i].red;
1088 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1089 cooccurrence[x][y].direction[i].green*
1090 cooccurrence[x][y].direction[i].green;
1091 channel_features[BluePixelChannel].angular_second_moment[i]+=
1092 cooccurrence[x][y].direction[i].blue*
1093 cooccurrence[x][y].direction[i].blue;
1094 if (image->colorspace == CMYKColorspace)
1095 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1096 cooccurrence[x][y].direction[i].black*
1097 cooccurrence[x][y].direction[i].black;
1098 if (image->alpha_trait == BlendPixelTrait)
1099 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1100 cooccurrence[x][y].direction[i].alpha*
1101 cooccurrence[x][y].direction[i].alpha;
1103 Correlation: measure of linear-dependencies in the image.
1105 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1106 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1107 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1108 if (image->colorspace == CMYKColorspace)
1109 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1110 if (image->alpha_trait == BlendPixelTrait)
1111 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1112 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1113 correlation.direction[i].green+=x*y*
1114 cooccurrence[x][y].direction[i].green;
1115 correlation.direction[i].blue+=x*y*
1116 cooccurrence[x][y].direction[i].blue;
1117 if (image->colorspace == CMYKColorspace)
1118 correlation.direction[i].black+=x*y*
1119 cooccurrence[x][y].direction[i].black;
1120 if (image->alpha_trait == BlendPixelTrait)
1121 correlation.direction[i].alpha+=x*y*
1122 cooccurrence[x][y].direction[i].alpha;
1124 Inverse Difference Moment.
1126 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1127 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1128 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1129 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1130 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1131 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1132 if (image->colorspace == CMYKColorspace)
1133 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1134 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1135 if (image->alpha_trait == BlendPixelTrait)
1136 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1137 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1141 density_xy[y+x+2].direction[i].red+=
1142 cooccurrence[x][y].direction[i].red;
1143 density_xy[y+x+2].direction[i].green+=
1144 cooccurrence[x][y].direction[i].green;
1145 density_xy[y+x+2].direction[i].blue+=
1146 cooccurrence[x][y].direction[i].blue;
1147 if (image->colorspace == CMYKColorspace)
1148 density_xy[y+x+2].direction[i].black+=
1149 cooccurrence[x][y].direction[i].black;
1150 if (image->alpha_trait == BlendPixelTrait)
1151 density_xy[y+x+2].direction[i].alpha+=
1152 cooccurrence[x][y].direction[i].alpha;
1156 channel_features[RedPixelChannel].entropy[i]-=
1157 cooccurrence[x][y].direction[i].red*
1158 MagickLog10(cooccurrence[x][y].direction[i].red);
1159 channel_features[GreenPixelChannel].entropy[i]-=
1160 cooccurrence[x][y].direction[i].green*
1161 MagickLog10(cooccurrence[x][y].direction[i].green);
1162 channel_features[BluePixelChannel].entropy[i]-=
1163 cooccurrence[x][y].direction[i].blue*
1164 MagickLog10(cooccurrence[x][y].direction[i].blue);
1165 if (image->colorspace == CMYKColorspace)
1166 channel_features[BlackPixelChannel].entropy[i]-=
1167 cooccurrence[x][y].direction[i].black*
1168 MagickLog10(cooccurrence[x][y].direction[i].black);
1169 if (image->alpha_trait == BlendPixelTrait)
1170 channel_features[AlphaPixelChannel].entropy[i]-=
1171 cooccurrence[x][y].direction[i].alpha*
1172 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1174 Information Measures of Correlation.
1176 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1177 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1178 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1179 if (image->alpha_trait == BlendPixelTrait)
1180 density_x[x].direction[i].alpha+=
1181 cooccurrence[x][y].direction[i].alpha;
1182 if (image->colorspace == CMYKColorspace)
1183 density_x[x].direction[i].black+=
1184 cooccurrence[x][y].direction[i].black;
1185 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1186 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1187 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1188 if (image->colorspace == CMYKColorspace)
1189 density_y[y].direction[i].black+=
1190 cooccurrence[x][y].direction[i].black;
1191 if (image->alpha_trait == BlendPixelTrait)
1192 density_y[y].direction[i].alpha+=
1193 cooccurrence[x][y].direction[i].alpha;
1195 mean.direction[i].red+=y*sum[y].direction[i].red;
1196 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1197 mean.direction[i].green+=y*sum[y].direction[i].green;
1198 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1199 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1200 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1201 if (image->colorspace == CMYKColorspace)
1203 mean.direction[i].black+=y*sum[y].direction[i].black;
1204 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1206 if (image->alpha_trait == BlendPixelTrait)
1208 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1209 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1213 Correlation: measure of linear-dependencies in the image.
1215 channel_features[RedPixelChannel].correlation[i]=
1216 (correlation.direction[i].red-mean.direction[i].red*
1217 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1218 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1219 sum_squares.direction[i].red-(mean.direction[i].red*
1220 mean.direction[i].red)));
1221 channel_features[GreenPixelChannel].correlation[i]=
1222 (correlation.direction[i].green-mean.direction[i].green*
1223 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1224 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1225 sum_squares.direction[i].green-(mean.direction[i].green*
1226 mean.direction[i].green)));
1227 channel_features[BluePixelChannel].correlation[i]=
1228 (correlation.direction[i].blue-mean.direction[i].blue*
1229 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1230 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1231 sum_squares.direction[i].blue-(mean.direction[i].blue*
1232 mean.direction[i].blue)));
1233 if (image->colorspace == CMYKColorspace)
1234 channel_features[BlackPixelChannel].correlation[i]=
1235 (correlation.direction[i].black-mean.direction[i].black*
1236 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1237 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1238 sum_squares.direction[i].black-(mean.direction[i].black*
1239 mean.direction[i].black)));
1240 if (image->alpha_trait == BlendPixelTrait)
1241 channel_features[AlphaPixelChannel].correlation[i]=
1242 (correlation.direction[i].alpha-mean.direction[i].alpha*
1243 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1244 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1245 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1246 mean.direction[i].alpha)));
1249 Compute more texture features.
1251 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1252 #pragma omp parallel for schedule(static,4) shared(status) \
1253 magick_threads(image,image,number_grays,1)
1255 for (i=0; i < 4; i++)
1260 for (x=2; x < (ssize_t) (2*number_grays); x++)
1265 channel_features[RedPixelChannel].sum_average[i]+=
1266 x*density_xy[x].direction[i].red;
1267 channel_features[GreenPixelChannel].sum_average[i]+=
1268 x*density_xy[x].direction[i].green;
1269 channel_features[BluePixelChannel].sum_average[i]+=
1270 x*density_xy[x].direction[i].blue;
1271 if (image->colorspace == CMYKColorspace)
1272 channel_features[BlackPixelChannel].sum_average[i]+=
1273 x*density_xy[x].direction[i].black;
1274 if (image->alpha_trait == BlendPixelTrait)
1275 channel_features[AlphaPixelChannel].sum_average[i]+=
1276 x*density_xy[x].direction[i].alpha;
1280 channel_features[RedPixelChannel].sum_entropy[i]-=
1281 density_xy[x].direction[i].red*
1282 MagickLog10(density_xy[x].direction[i].red);
1283 channel_features[GreenPixelChannel].sum_entropy[i]-=
1284 density_xy[x].direction[i].green*
1285 MagickLog10(density_xy[x].direction[i].green);
1286 channel_features[BluePixelChannel].sum_entropy[i]-=
1287 density_xy[x].direction[i].blue*
1288 MagickLog10(density_xy[x].direction[i].blue);
1289 if (image->colorspace == CMYKColorspace)
1290 channel_features[BlackPixelChannel].sum_entropy[i]-=
1291 density_xy[x].direction[i].black*
1292 MagickLog10(density_xy[x].direction[i].black);
1293 if (image->alpha_trait == BlendPixelTrait)
1294 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1295 density_xy[x].direction[i].alpha*
1296 MagickLog10(density_xy[x].direction[i].alpha);
1300 channel_features[RedPixelChannel].sum_variance[i]+=
1301 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1302 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1303 density_xy[x].direction[i].red;
1304 channel_features[GreenPixelChannel].sum_variance[i]+=
1305 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1306 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1307 density_xy[x].direction[i].green;
1308 channel_features[BluePixelChannel].sum_variance[i]+=
1309 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1310 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1311 density_xy[x].direction[i].blue;
1312 if (image->colorspace == CMYKColorspace)
1313 channel_features[BlackPixelChannel].sum_variance[i]+=
1314 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1315 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1316 density_xy[x].direction[i].black;
1317 if (image->alpha_trait == BlendPixelTrait)
1318 channel_features[AlphaPixelChannel].sum_variance[i]+=
1319 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1320 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1321 density_xy[x].direction[i].alpha;
1325 Compute more texture features.
1327 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1328 #pragma omp parallel for schedule(static,4) shared(status) \
1329 magick_threads(image,image,number_grays,1)
1331 for (i=0; i < 4; i++)
1336 for (y=0; y < (ssize_t) number_grays; y++)
1341 for (x=0; x < (ssize_t) number_grays; x++)
1344 Sum of Squares: Variance
1346 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1347 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1348 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1349 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1350 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1351 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1352 if (image->colorspace == CMYKColorspace)
1353 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1354 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1355 if (image->alpha_trait == BlendPixelTrait)
1356 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1357 (y-mean.direction[i].alpha+1)*
1358 cooccurrence[x][y].direction[i].alpha;
1360 Sum average / Difference Variance.
1362 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1363 cooccurrence[x][y].direction[i].red;
1364 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1365 cooccurrence[x][y].direction[i].green;
1366 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1367 cooccurrence[x][y].direction[i].blue;
1368 if (image->colorspace == CMYKColorspace)
1369 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1370 cooccurrence[x][y].direction[i].black;
1371 if (image->alpha_trait == BlendPixelTrait)
1372 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1373 cooccurrence[x][y].direction[i].alpha;
1375 Information Measures of Correlation.
1377 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1378 MagickLog10(cooccurrence[x][y].direction[i].red);
1379 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1380 MagickLog10(cooccurrence[x][y].direction[i].green);
1381 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1382 MagickLog10(cooccurrence[x][y].direction[i].blue);
1383 if (image->colorspace == CMYKColorspace)
1384 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1385 MagickLog10(cooccurrence[x][y].direction[i].black);
1386 if (image->alpha_trait == BlendPixelTrait)
1387 entropy_xy.direction[i].alpha-=
1388 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1389 cooccurrence[x][y].direction[i].alpha);
1390 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1391 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1392 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1393 MagickLog10(density_x[x].direction[i].green*
1394 density_y[y].direction[i].green));
1395 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1396 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1397 if (image->colorspace == CMYKColorspace)
1398 entropy_xy1.direction[i].black-=(
1399 cooccurrence[x][y].direction[i].black*MagickLog10(
1400 density_x[x].direction[i].black*density_y[y].direction[i].black));
1401 if (image->alpha_trait == BlendPixelTrait)
1402 entropy_xy1.direction[i].alpha-=(
1403 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1404 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1405 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1406 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1407 density_y[y].direction[i].red));
1408 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1409 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1410 density_y[y].direction[i].green));
1411 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1412 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1413 density_y[y].direction[i].blue));
1414 if (image->colorspace == CMYKColorspace)
1415 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1416 density_y[y].direction[i].black*MagickLog10(
1417 density_x[x].direction[i].black*density_y[y].direction[i].black));
1418 if (image->alpha_trait == BlendPixelTrait)
1419 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1420 density_y[y].direction[i].alpha*MagickLog10(
1421 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1424 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1425 variance.direction[i].red;
1426 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1427 variance.direction[i].green;
1428 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1429 variance.direction[i].blue;
1430 if (image->colorspace == CMYKColorspace)
1431 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1432 variance.direction[i].black;
1433 if (image->alpha_trait == BlendPixelTrait)
1434 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1435 variance.direction[i].alpha;
1438 Compute more texture features.
1440 (void) ResetMagickMemory(&variance,0,sizeof(variance));
1441 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
1442 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1443 #pragma omp parallel for schedule(static,4) shared(status) \
1444 magick_threads(image,image,number_grays,1)
1446 for (i=0; i < 4; i++)
1451 for (x=0; x < (ssize_t) number_grays; x++)
1454 Difference variance.
1456 variance.direction[i].red+=density_xy[x].direction[i].red;
1457 variance.direction[i].green+=density_xy[x].direction[i].green;
1458 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1459 if (image->colorspace == CMYKColorspace)
1460 variance.direction[i].black+=density_xy[x].direction[i].black;
1461 if (image->alpha_trait == BlendPixelTrait)
1462 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1463 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1464 density_xy[x].direction[i].red;
1465 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1466 density_xy[x].direction[i].green;
1467 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1468 density_xy[x].direction[i].blue;
1469 if (image->colorspace == CMYKColorspace)
1470 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1471 density_xy[x].direction[i].black;
1472 if (image->alpha_trait == BlendPixelTrait)
1473 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1474 density_xy[x].direction[i].alpha;
1478 channel_features[RedPixelChannel].difference_entropy[i]-=
1479 density_xy[x].direction[i].red*
1480 MagickLog10(density_xy[x].direction[i].red);
1481 channel_features[GreenPixelChannel].difference_entropy[i]-=
1482 density_xy[x].direction[i].green*
1483 MagickLog10(density_xy[x].direction[i].green);
1484 channel_features[BluePixelChannel].difference_entropy[i]-=
1485 density_xy[x].direction[i].blue*
1486 MagickLog10(density_xy[x].direction[i].blue);
1487 if (image->colorspace == CMYKColorspace)
1488 channel_features[BlackPixelChannel].difference_entropy[i]-=
1489 density_xy[x].direction[i].black*
1490 MagickLog10(density_xy[x].direction[i].black);
1491 if (image->alpha_trait == BlendPixelTrait)
1492 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1493 density_xy[x].direction[i].alpha*
1494 MagickLog10(density_xy[x].direction[i].alpha);
1496 Information Measures of Correlation.
1498 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1499 MagickLog10(density_x[x].direction[i].red));
1500 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1501 MagickLog10(density_x[x].direction[i].green));
1502 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1503 MagickLog10(density_x[x].direction[i].blue));
1504 if (image->colorspace == CMYKColorspace)
1505 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1506 MagickLog10(density_x[x].direction[i].black));
1507 if (image->alpha_trait == BlendPixelTrait)
1508 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1509 MagickLog10(density_x[x].direction[i].alpha));
1510 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1511 MagickLog10(density_y[x].direction[i].red));
1512 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1513 MagickLog10(density_y[x].direction[i].green));
1514 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1515 MagickLog10(density_y[x].direction[i].blue));
1516 if (image->colorspace == CMYKColorspace)
1517 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1518 MagickLog10(density_y[x].direction[i].black));
1519 if (image->alpha_trait == BlendPixelTrait)
1520 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1521 MagickLog10(density_y[x].direction[i].alpha));
1524 Difference variance.
1526 channel_features[RedPixelChannel].difference_variance[i]=
1527 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1528 (variance.direction[i].red*variance.direction[i].red))/
1529 ((double) number_grays*number_grays*number_grays*number_grays);
1530 channel_features[GreenPixelChannel].difference_variance[i]=
1531 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1532 (variance.direction[i].green*variance.direction[i].green))/
1533 ((double) number_grays*number_grays*number_grays*number_grays);
1534 channel_features[BluePixelChannel].difference_variance[i]=
1535 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1536 (variance.direction[i].blue*variance.direction[i].blue))/
1537 ((double) number_grays*number_grays*number_grays*number_grays);
1538 if (image->colorspace == CMYKColorspace)
1539 channel_features[BlackPixelChannel].difference_variance[i]=
1540 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1541 (variance.direction[i].black*variance.direction[i].black))/
1542 ((double) number_grays*number_grays*number_grays*number_grays);
1543 if (image->alpha_trait == BlendPixelTrait)
1544 channel_features[AlphaPixelChannel].difference_variance[i]=
1545 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1546 (variance.direction[i].alpha*variance.direction[i].alpha))/
1547 ((double) number_grays*number_grays*number_grays*number_grays);
1549 Information Measures of Correlation.
1551 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1552 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1553 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1554 entropy_x.direction[i].red : entropy_y.direction[i].red);
1555 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1556 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1557 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1558 entropy_x.direction[i].green : entropy_y.direction[i].green);
1559 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1560 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1561 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1562 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1563 if (image->colorspace == CMYKColorspace)
1564 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1565 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1566 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1567 entropy_x.direction[i].black : entropy_y.direction[i].black);
1568 if (image->alpha_trait == BlendPixelTrait)
1569 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1570 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1571 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1572 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1573 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1574 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].red-
1575 entropy_xy.direction[i].red)))));
1576 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1577 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].green-
1578 entropy_xy.direction[i].green)))));
1579 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1580 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].blue-
1581 entropy_xy.direction[i].blue)))));
1582 if (image->colorspace == CMYKColorspace)
1583 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1584 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].black-
1585 entropy_xy.direction[i].black)))));
1586 if (image->alpha_trait == BlendPixelTrait)
1587 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1588 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].alpha-
1589 entropy_xy.direction[i].alpha)))));
1592 Compute more texture features.
1594 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1595 #pragma omp parallel for schedule(static,4) shared(status) \
1596 magick_threads(image,image,number_grays,1)
1598 for (i=0; i < 4; i++)
1603 for (z=0; z < (ssize_t) number_grays; z++)
1611 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1612 for (y=0; y < (ssize_t) number_grays; y++)
1617 for (x=0; x < (ssize_t) number_grays; x++)
1620 Contrast: amount of local variations present in an image.
1622 if (((y-x) == z) || ((x-y) == z))
1624 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1625 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1626 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1627 if (image->colorspace == CMYKColorspace)
1628 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1629 if (image->alpha_trait == BlendPixelTrait)
1630 pixel.direction[i].alpha+=
1631 cooccurrence[x][y].direction[i].alpha;
1634 Maximum Correlation Coefficient.
1636 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1637 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1638 density_y[x].direction[i].red;
1639 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1640 cooccurrence[y][x].direction[i].green/
1641 density_x[z].direction[i].green/density_y[x].direction[i].red;
1642 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1643 cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/
1644 density_y[x].direction[i].blue;
1645 if (image->colorspace == CMYKColorspace)
1646 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1647 cooccurrence[y][x].direction[i].black/
1648 density_x[z].direction[i].black/density_y[x].direction[i].black;
1649 if (image->alpha_trait == BlendPixelTrait)
1650 Q[z][y].direction[i].alpha+=
1651 cooccurrence[z][x].direction[i].alpha*
1652 cooccurrence[y][x].direction[i].alpha/
1653 density_x[z].direction[i].alpha/
1654 density_y[x].direction[i].alpha;
1657 channel_features[RedPixelChannel].contrast[i]+=z*z*
1658 pixel.direction[i].red;
1659 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1660 pixel.direction[i].green;
1661 channel_features[BluePixelChannel].contrast[i]+=z*z*
1662 pixel.direction[i].blue;
1663 if (image->colorspace == CMYKColorspace)
1664 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1665 pixel.direction[i].black;
1666 if (image->alpha_trait == BlendPixelTrait)
1667 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1668 pixel.direction[i].alpha;
1671 Maximum Correlation Coefficient.
1672 Future: return second largest eigenvalue of Q.
1674 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1675 sqrt((double) -1.0);
1676 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1677 sqrt((double) -1.0);
1678 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1679 sqrt((double) -1.0);
1680 if (image->colorspace == CMYKColorspace)
1681 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1682 sqrt((double) -1.0);
1683 if (image->alpha_trait == BlendPixelTrait)
1684 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1685 sqrt((double) -1.0);
1688 Relinquish resources.
1690 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
1691 for (i=0; i < (ssize_t) number_grays; i++)
1692 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1693 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1694 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1695 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1696 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
1697 for (i=0; i < (ssize_t) number_grays; i++)
1698 cooccurrence[i]=(ChannelStatistics *)
1699 RelinquishMagickMemory(cooccurrence[i]);
1700 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1701 return(channel_features);
1705 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1709 % H o u g h L i n e I m a g e %
1713 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715 % HoughLineImage() identifies lines in the image.
1717 % The format of the HoughLineImage method is:
1719 % Image *HoughLineImage(const Image *image,const size_t width,
1720 % const size_t height,const size_t threshold,ExceptionInfo *exception)
1722 % A description of each parameter follows:
1724 % o image: the image.
1726 % o width, height: find line pairs as local maxima in this neighborhood.
1728 % o threshold: the line count threshold.
1730 % o exception: return any errors or warnings in this structure.
1734 static inline double MagickRound(double x)
1737 Round the fraction to nearest integer.
1739 if ((x-floor(x)) < (ceil(x)-x))
1744 MagickExport Image *HoughLineImage(const Image *image,const size_t width,
1745 const size_t height,const size_t threshold,ExceptionInfo *exception)
1751 message[MaxTextExtent],
1752 path[MaxTextExtent];
1761 *lines_image = NULL;
1787 Create the accumulator.
1789 assert(image != (const Image *) NULL);
1790 assert(image->signature == MagickSignature);
1791 if (image->debug != MagickFalse)
1792 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1793 assert(exception != (ExceptionInfo *) NULL);
1794 assert(exception->signature == MagickSignature);
1795 accumulator_width=180;
1796 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1797 image->rows : image->columns))/2.0);
1798 accumulator_height=(size_t) (2.0*hough_height);
1799 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1800 sizeof(double),exception);
1801 if (accumulator == (MatrixInfo *) NULL)
1802 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1803 if (NullMatrix(accumulator) == MagickFalse)
1805 accumulator=DestroyMatrixInfo(accumulator);
1806 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1809 Populate the accumulator.
1812 center.x=(double) image->columns/2.0;
1813 center.y=(double) image->rows/2.0;
1814 image_view=AcquireVirtualCacheView(image,exception);
1815 for (y=0; y < (ssize_t) image->rows; y++)
1817 register const Quantum
1823 if (status == MagickFalse)
1825 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1826 if (p == (Quantum *) NULL)
1831 for (x=0; x < (ssize_t) image->columns; x++)
1833 if (GetPixelIntensity(image,p) > (QuantumRange/2))
1838 for (i=0; i < 180; i++)
1844 radius=(((double) x-center.x)*cos(DegreesToRadians((double) i)))+
1845 (((double) y-center.y)*sin(DegreesToRadians((double) i)));
1846 (void) GetMatrixElement(accumulator,i,(ssize_t)
1847 MagickRound(radius+hough_height),&count);
1849 (void) SetMatrixElement(accumulator,i,(ssize_t)
1850 MagickRound(radius+hough_height),&count);
1853 p+=GetPixelChannels(image);
1856 image_view=DestroyCacheView(image_view);
1857 if (status == MagickFalse)
1859 accumulator=DestroyMatrixInfo(accumulator);
1860 return((Image *) NULL);
1863 Generate line segments from accumulator.
1865 file=AcquireUniqueFileResource(path);
1868 accumulator=DestroyMatrixInfo(accumulator);
1869 return((Image *) NULL);
1871 (void) FormatLocaleString(message,MaxTextExtent,
1872 "# Hough line transform: %.20gx%.20g%+.20g\n",(double) width,
1873 (double) height,(double) threshold);
1874 (void) write(file,message,strlen(message));
1875 (void) FormatLocaleString(message,MaxTextExtent,"viewbox 0 0 %.20g %.20g\n",
1876 (double) image->columns,(double) image->rows);
1877 (void) write(file,message,strlen(message));
1878 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1880 line_count=threshold;
1881 for (y=0; y < (ssize_t) accumulator_height; y++)
1886 for (x=0; x < (ssize_t) accumulator_width; x++)
1891 (void) GetMatrixElement(accumulator,x,y,&count);
1892 if (count >= (double) line_count)
1904 Is point a local maxima?
1907 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
1912 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
1914 if ((u != 0) || (v !=0))
1916 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
1924 if (u < (ssize_t) (width/2))
1927 (void) GetMatrixElement(accumulator,x,y,&count);
1930 if ((x >= 45) && (x <= 135))
1933 y = (r-x cos(t))/sin(t)
1936 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
1937 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1938 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1939 line.x2=(double) image->columns;
1940 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
1941 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1942 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1947 x = (r-y cos(t))/sin(t)
1950 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
1951 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1952 cos(DegreesToRadians((double) x))+(image->columns/2.0);
1953 line.y2=(double) image->rows;
1954 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
1955 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1956 cos(DegreesToRadians((double) x))+(image->columns/2.0);
1958 (void) FormatLocaleString(message,MaxTextExtent,
1959 "line %g,%g %g,%g # %g\n",line.x1,line.y1,line.x2,line.y2,maxima);
1960 (void) write(file,message,strlen(message));
1966 Render lines to image canvas.
1968 image_info=AcquireImageInfo();
1969 image_info->background_color=image->background_color;
1970 (void) FormatLocaleString(image_info->filename,MaxTextExtent,"mvg:%s",path);
1971 artifact=GetImageArtifact(image,"background");
1972 if (artifact != (const char *) NULL)
1973 (void) SetImageOption(image_info,"background",artifact);
1974 artifact=GetImageArtifact(image,"fill");
1975 if (artifact != (const char *) NULL)
1976 (void) SetImageOption(image_info,"fill",artifact);
1977 artifact=GetImageArtifact(image,"stroke");
1978 if (artifact != (const char *) NULL)
1979 (void) SetImageOption(image_info,"stroke",artifact);
1980 artifact=GetImageArtifact(image,"strokewidth");
1981 if (artifact != (const char *) NULL)
1982 (void) SetImageOption(image_info,"strokewidth",artifact);
1983 lines_image=ReadImage(image_info,exception);
1984 artifact=GetImageArtifact(image,"hough-lines:accumulator");
1985 if ((lines_image != (Image *) NULL) &&
1986 (IsStringTrue(artifact) != MagickFalse))
1991 accumulator_image=MatrixToImage(accumulator,exception);
1992 if (accumulator_image != (Image *) NULL)
1993 AppendImageToList(&lines_image,accumulator_image);
1998 accumulator=DestroyMatrixInfo(accumulator);
1999 image_info=DestroyImageInfo(image_info);
2000 (void) RelinquishUniqueFileResource(path);
2001 return(GetFirstImageInList(lines_image));
2005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2009 % M e a n S h i f t I m a g e %
2013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2015 % MeanShiftImage() delineate arbitrarily shaped clusters in the image.
2017 % The format of the MeanShiftImage method is:
2019 % Image *MeanShiftImage(const Image *image,const size_t width,
2020 % const size_t height,const double color_distance,
2021 % ExceptionInfo *exception)
2023 % A description of each parameter follows:
2025 % o image: the image.
2027 % o width, height: find pixels in this neighborhood.
2029 % o color_distance: the color distance.
2031 % o exception: return any errors or warnings in this structure.
2034 MagickExport Image *MeanShiftImage(const Image *image,const size_t width,
2035 const size_t height,const double color_distance,ExceptionInfo *exception)
2037 #define MaxMeanShiftIterations 100
2053 assert(image != (const Image *) NULL);
2054 assert(image->signature == MagickSignature);
2055 if (image->debug != MagickFalse)
2056 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2057 assert(exception != (ExceptionInfo *) NULL);
2058 assert(exception->signature == MagickSignature);
2059 mean_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2060 if (mean_image == (Image *) NULL)
2061 return((Image *) NULL);
2062 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2064 mean_image=DestroyImage(mean_image);
2065 return((Image *) NULL);
2068 image_view=AcquireVirtualCacheView(image,exception);
2069 pixel_view=AcquireVirtualCacheView(image,exception);
2070 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2071 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2072 #pragma omp parallel for schedule(static,4) shared(status) \
2073 magick_threads(mean_image,mean_image,mean_image->rows,1)
2075 for (y=0; y < (ssize_t) mean_image->rows; y++)
2077 register const Quantum
2086 if (status == MagickFalse)
2088 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2089 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2091 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2096 for (x=0; x < (ssize_t) mean_image->columns; x++)
2109 GetPixelInfo(image,&mean_pixel);
2110 GetPixelInfoPixel(image,p,&mean_pixel);
2111 mean_location.x=(double) x;
2112 mean_location.y=(double) y;
2113 for (i=0; i < MaxMeanShiftIterations; i++)
2131 GetPixelInfo(image,&sum_pixel);
2132 previous_location=mean_location;
2133 previous_pixel=mean_pixel;
2135 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2140 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2142 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2147 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2148 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2149 mean_location.y+v),&pixel,exception);
2150 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2151 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2152 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2153 if (distance <= (color_distance*color_distance))
2155 sum_location.x+=mean_location.x+u;
2156 sum_location.y+=mean_location.y+v;
2157 sum_pixel.red+=pixel.red;
2158 sum_pixel.green+=pixel.green;
2159 sum_pixel.blue+=pixel.blue;
2160 sum_pixel.alpha+=pixel.alpha;
2167 mean_location.x=gamma*sum_location.x;
2168 mean_location.y=gamma*sum_location.y;
2169 mean_pixel.red=gamma*sum_pixel.red;
2170 mean_pixel.green=gamma*sum_pixel.green;
2171 mean_pixel.blue=gamma*sum_pixel.blue;
2172 mean_pixel.alpha=gamma*sum_pixel.alpha;
2173 distance=(mean_location.x-previous_location.x)*
2174 (mean_location.x-previous_location.x)+
2175 (mean_location.y-previous_location.y)*
2176 (mean_location.y-previous_location.y)+
2177 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2178 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2179 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2180 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2181 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2182 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2183 if (distance <= 3.0)
2186 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2187 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2188 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2189 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2190 p+=GetPixelChannels(image);
2191 q+=GetPixelChannels(mean_image);
2193 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2196 mean_view=DestroyCacheView(mean_view);
2197 pixel_view=DestroyCacheView(pixel_view);
2198 image_view=DestroyCacheView(image_view);