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)
240 #define CannyEdgeImageTag "CannyEdge/Image"
249 geometry[MaxTextExtent];
275 assert(image != (const Image *) NULL);
276 assert(image->signature == MagickSignature);
277 if (image->debug != MagickFalse)
278 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
279 assert(exception != (ExceptionInfo *) NULL);
280 assert(exception->signature == MagickSignature);
284 (void) FormatLocaleString(geometry,MaxTextExtent,
285 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
286 kernel_info=AcquireKernelInfo(geometry);
287 if (kernel_info == (KernelInfo *) NULL)
288 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
289 edge_image=MorphologyApply(image,ConvolveMorphology,1,kernel_info,
290 UndefinedCompositeOp,0.0,exception);
291 kernel_info=DestroyKernelInfo(kernel_info);
292 if (edge_image == (Image *) NULL)
293 return((Image *) NULL);
294 if (SetImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
296 edge_image=DestroyImage(edge_image);
297 return((Image *) NULL);
300 Find the intensity gradient of the image.
302 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
303 sizeof(CannyInfo),exception);
304 if (canny_cache == (MatrixInfo *) NULL)
306 edge_image=DestroyImage(edge_image);
307 return((Image *) NULL);
310 edge_view=AcquireVirtualCacheView(edge_image,exception);
311 #if defined(MAGICKCORE_OPENMP_SUPPORT)
312 #pragma omp parallel for schedule(static,4) shared(status) \
313 magick_threads(edge_image,edge_image,edge_image->rows,1)
315 for (y=0; y < (ssize_t) edge_image->rows; y++)
317 register const Quantum
323 if (status == MagickFalse)
325 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
327 if (p == (const Quantum *) NULL)
332 for (x=0; x < (ssize_t) edge_image->columns; x++)
341 register const Quantum
342 *restrict kernel_pixels;
359 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
363 for (v=0; v < 2; v++)
368 for (u=0; u < 2; u++)
373 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
374 dx+=0.5*Gx[v][u]*intensity;
375 dy+=0.5*Gy[v][u]*intensity;
377 kernel_pixels+=edge_image->columns+1;
379 pixel.magnitude=hypot(dx,dy);
381 if (fabs(dx) > MagickEpsilon)
389 if (slope < -2.41421356237)
392 if (slope < -0.414213562373)
399 if (slope > 2.41421356237)
402 if (slope > 0.414213562373)
408 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
410 p+=GetPixelChannels(edge_image);
413 edge_view=DestroyCacheView(edge_view);
415 Non-maxima suppression, remove pixels that are not considered to be part
419 (void) GetMatrixElement(canny_cache,0,0,&pixel);
422 edge_view=AcquireAuthenticCacheView(edge_image,exception);
423 #if defined(MAGICKCORE_OPENMP_SUPPORT)
424 #pragma omp parallel for schedule(static,4) shared(status) \
425 magick_threads(edge_image,edge_image,edge_image->rows,1)
427 for (y=0; y < (ssize_t) edge_image->rows; y++)
435 if (status == MagickFalse)
437 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
439 if (q == (Quantum *) NULL)
444 for (x=0; x < (ssize_t) edge_image->columns; x++)
451 (void) GetMatrixElement(canny_cache,x,y,&pixel);
452 switch (pixel.orientation)
458 0 degrees, north and south.
460 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
461 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
467 45 degrees, northwest and southeast.
469 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
470 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
476 90 degrees, east and west.
478 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
479 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
485 135 degrees, northeast and southwest.
487 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
488 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
492 pixel.intensity=pixel.magnitude;
493 if ((pixel.magnitude < alpha_pixel.magnitude) ||
494 (pixel.magnitude < beta_pixel.magnitude))
496 (void) SetMatrixElement(canny_cache,x,y,&pixel);
497 #if defined(MAGICKCORE_OPENMP_SUPPORT)
498 #pragma omp critical (MagickCore_CannyEdgeImage)
501 if (pixel.intensity < min)
503 if (pixel.intensity > max)
507 q+=GetPixelChannels(edge_image);
509 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
512 edge_view=DestroyCacheView(edge_view);
514 Estimate hysteresis threshold.
516 lower_threshold=lower_percent*(max-min)+min;
517 upper_threshold=upper_percent*(max-min)+min;
519 Hysteresis threshold.
521 edge_view=AcquireAuthenticCacheView(edge_image,exception);
522 for (y=0; y < (ssize_t) edge_image->rows; y++)
527 if (status == MagickFalse)
529 for (x=0; x < (ssize_t) edge_image->columns; x++)
534 register const Quantum
538 Edge if pixel gradient higher than upper threshold.
540 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
541 if (p == (const Quantum *) NULL)
543 status=GetMatrixElement(canny_cache,x,y,&pixel);
544 if (status == MagickFalse)
546 if ((GetPixelIntensity(edge_image,p) == 0.0) &&
547 (pixel.intensity >= upper_threshold))
548 status=TraceEdges(edge_image,edge_view,canny_cache,x,y,lower_threshold,
551 if (image->progress_monitor != (MagickProgressMonitor) NULL)
556 #if defined(MAGICKCORE_OPENMP_SUPPORT)
557 #pragma omp critical (MagickCore_CannyEdgeImage)
559 proceed=SetImageProgress(image,CannyEdgeImageTag,progress++,
561 if (proceed == MagickFalse)
565 edge_view=DestroyCacheView(edge_view);
569 canny_cache=DestroyMatrixInfo(canny_cache);
574 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
578 % G e t I m a g e F e a t u r e s %
582 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
584 % GetImageFeatures() returns features for each channel in the image in
585 % each of four directions (horizontal, vertical, left and right diagonals)
586 % for the specified distance. The features include the angular second
587 % moment, contrast, correlation, sum of squares: variance, inverse difference
588 % moment, sum average, sum varience, sum entropy, entropy, difference variance,% difference entropy, information measures of correlation 1, information
589 % measures of correlation 2, and maximum correlation coefficient. You can
590 % access the red channel contrast, for example, like this:
592 % channel_features=GetImageFeatures(image,1,exception);
593 % contrast=channel_features[RedPixelChannel].contrast[0];
595 % Use MagickRelinquishMemory() to free the features buffer.
597 % The format of the GetImageFeatures method is:
599 % ChannelFeatures *GetImageFeatures(const Image *image,
600 % const size_t distance,ExceptionInfo *exception)
602 % A description of each parameter follows:
604 % o image: the image.
606 % o distance: the distance.
608 % o exception: return any errors or warnings in this structure.
612 static inline ssize_t MagickAbsoluteValue(const ssize_t x)
619 static inline double MagickLog10(const double x)
621 #define Log10Epsilon (1.0e-11)
623 if (fabs(x) < Log10Epsilon)
624 return(log10(Log10Epsilon));
625 return(log10(fabs(x)));
628 MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
629 const size_t distance,ExceptionInfo *exception)
631 typedef struct _ChannelStatistics
634 direction[4]; /* horizontal, vertical, left and right diagonals */
679 assert(image != (Image *) NULL);
680 assert(image->signature == MagickSignature);
681 if (image->debug != MagickFalse)
682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
683 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
684 return((ChannelFeatures *) NULL);
685 length=CompositeChannels+1UL;
686 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
687 sizeof(*channel_features));
688 if (channel_features == (ChannelFeatures *) NULL)
689 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
690 (void) ResetMagickMemory(channel_features,0,length*
691 sizeof(*channel_features));
695 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
696 if (grays == (PixelPacket *) NULL)
698 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
700 (void) ThrowMagickException(exception,GetMagickModule(),
701 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
702 return(channel_features);
704 for (i=0; i <= (ssize_t) MaxMap; i++)
707 grays[i].green=(~0U);
709 grays[i].alpha=(~0U);
710 grays[i].black=(~0U);
713 image_view=AcquireVirtualCacheView(image,exception);
714 #if defined(MAGICKCORE_OPENMP_SUPPORT)
715 #pragma omp parallel for schedule(static,4) shared(status) \
716 magick_threads(image,image,image->rows,1)
718 for (y=0; y < (ssize_t) image->rows; y++)
720 register const Quantum
726 if (status == MagickFalse)
728 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
729 if (p == (const Quantum *) NULL)
734 for (x=0; x < (ssize_t) image->columns; x++)
736 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
737 ScaleQuantumToMap(GetPixelRed(image,p));
738 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
739 ScaleQuantumToMap(GetPixelGreen(image,p));
740 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
741 ScaleQuantumToMap(GetPixelBlue(image,p));
742 if (image->colorspace == CMYKColorspace)
743 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
744 ScaleQuantumToMap(GetPixelBlack(image,p));
745 if (image->alpha_trait == BlendPixelTrait)
746 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
747 ScaleQuantumToMap(GetPixelAlpha(image,p));
748 p+=GetPixelChannels(image);
751 image_view=DestroyCacheView(image_view);
752 if (status == MagickFalse)
754 grays=(PixelPacket *) RelinquishMagickMemory(grays);
755 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
757 return(channel_features);
759 (void) ResetMagickMemory(&gray,0,sizeof(gray));
760 for (i=0; i <= (ssize_t) MaxMap; i++)
762 if (grays[i].red != ~0U)
763 grays[gray.red++].red=grays[i].red;
764 if (grays[i].green != ~0U)
765 grays[gray.green++].green=grays[i].green;
766 if (grays[i].blue != ~0U)
767 grays[gray.blue++].blue=grays[i].blue;
768 if (image->colorspace == CMYKColorspace)
769 if (grays[i].black != ~0U)
770 grays[gray.black++].black=grays[i].black;
771 if (image->alpha_trait == BlendPixelTrait)
772 if (grays[i].alpha != ~0U)
773 grays[gray.alpha++].alpha=grays[i].alpha;
776 Allocate spatial dependence matrix.
778 number_grays=gray.red;
779 if (gray.green > number_grays)
780 number_grays=gray.green;
781 if (gray.blue > number_grays)
782 number_grays=gray.blue;
783 if (image->colorspace == CMYKColorspace)
784 if (gray.black > number_grays)
785 number_grays=gray.black;
786 if (image->alpha_trait == BlendPixelTrait)
787 if (gray.alpha > number_grays)
788 number_grays=gray.alpha;
789 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
790 sizeof(*cooccurrence));
791 density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
793 density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
794 sizeof(*density_xy));
795 density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
797 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
798 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
799 if ((cooccurrence == (ChannelStatistics **) NULL) ||
800 (density_x == (ChannelStatistics *) NULL) ||
801 (density_xy == (ChannelStatistics *) NULL) ||
802 (density_y == (ChannelStatistics *) NULL) ||
803 (Q == (ChannelStatistics **) NULL) ||
804 (sum == (ChannelStatistics *) NULL))
806 if (Q != (ChannelStatistics **) NULL)
808 for (i=0; i < (ssize_t) number_grays; i++)
809 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
810 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
812 if (sum != (ChannelStatistics *) NULL)
813 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
814 if (density_y != (ChannelStatistics *) NULL)
815 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
816 if (density_xy != (ChannelStatistics *) NULL)
817 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
818 if (density_x != (ChannelStatistics *) NULL)
819 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
820 if (cooccurrence != (ChannelStatistics **) NULL)
822 for (i=0; i < (ssize_t) number_grays; i++)
823 cooccurrence[i]=(ChannelStatistics *)
824 RelinquishMagickMemory(cooccurrence[i]);
825 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
828 grays=(PixelPacket *) RelinquishMagickMemory(grays);
829 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
831 (void) ThrowMagickException(exception,GetMagickModule(),
832 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
833 return(channel_features);
835 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
836 (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x));
837 (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
838 (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y));
839 (void) ResetMagickMemory(&mean,0,sizeof(mean));
840 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
841 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
842 (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy));
843 (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x));
844 (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy));
845 (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1));
846 (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2));
847 (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y));
848 (void) ResetMagickMemory(&variance,0,sizeof(variance));
849 for (i=0; i < (ssize_t) number_grays; i++)
851 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
852 sizeof(**cooccurrence));
853 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
854 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
855 (Q[i] == (ChannelStatistics *) NULL))
857 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
858 sizeof(**cooccurrence));
859 (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q));
861 if (i < (ssize_t) number_grays)
863 for (i--; i >= 0; i--)
865 if (Q[i] != (ChannelStatistics *) NULL)
866 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
867 if (cooccurrence[i] != (ChannelStatistics *) NULL)
868 cooccurrence[i]=(ChannelStatistics *)
869 RelinquishMagickMemory(cooccurrence[i]);
871 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
872 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
873 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
874 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
875 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
876 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
877 grays=(PixelPacket *) RelinquishMagickMemory(grays);
878 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
880 (void) ThrowMagickException(exception,GetMagickModule(),
881 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
882 return(channel_features);
885 Initialize spatial dependence matrix.
888 image_view=AcquireVirtualCacheView(image,exception);
889 for (y=0; y < (ssize_t) image->rows; y++)
891 register const Quantum
903 if (status == MagickFalse)
905 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+
906 2*distance,distance+2,exception);
907 if (p == (const Quantum *) NULL)
912 p+=distance*GetPixelChannels(image);;
913 for (x=0; x < (ssize_t) image->columns; x++)
915 for (i=0; i < 4; i++)
923 Horizontal adjacency.
925 offset=(ssize_t) distance;
933 offset=(ssize_t) (image->columns+2*distance);
939 Right diagonal adjacency.
941 offset=(ssize_t) ((image->columns+2*distance)-distance);
947 Left diagonal adjacency.
949 offset=(ssize_t) ((image->columns+2*distance)+distance);
955 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
957 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
959 cooccurrence[u][v].direction[i].red++;
960 cooccurrence[v][u].direction[i].red++;
963 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
965 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
967 cooccurrence[u][v].direction[i].green++;
968 cooccurrence[v][u].direction[i].green++;
971 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
973 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
975 cooccurrence[u][v].direction[i].blue++;
976 cooccurrence[v][u].direction[i].blue++;
977 if (image->colorspace == CMYKColorspace)
981 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
983 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
985 cooccurrence[u][v].direction[i].black++;
986 cooccurrence[v][u].direction[i].black++;
988 if (image->alpha_trait == BlendPixelTrait)
992 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
994 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
996 cooccurrence[u][v].direction[i].alpha++;
997 cooccurrence[v][u].direction[i].alpha++;
1000 p+=GetPixelChannels(image);
1003 grays=(PixelPacket *) RelinquishMagickMemory(grays);
1004 image_view=DestroyCacheView(image_view);
1005 if (status == MagickFalse)
1007 for (i=0; i < (ssize_t) number_grays; i++)
1008 cooccurrence[i]=(ChannelStatistics *)
1009 RelinquishMagickMemory(cooccurrence[i]);
1010 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1011 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
1013 (void) ThrowMagickException(exception,GetMagickModule(),
1014 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1015 return(channel_features);
1018 Normalize spatial dependence matrix.
1020 for (i=0; i < 4; i++)
1034 Horizontal adjacency.
1036 normalize=2.0*image->rows*(image->columns-distance);
1044 normalize=2.0*(image->rows-distance)*image->columns;
1050 Right diagonal adjacency.
1052 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1058 Left diagonal adjacency.
1060 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1064 normalize=PerceptibleReciprocal(normalize);
1065 for (y=0; y < (ssize_t) number_grays; y++)
1070 for (x=0; x < (ssize_t) number_grays; x++)
1072 cooccurrence[x][y].direction[i].red*=normalize;
1073 cooccurrence[x][y].direction[i].green*=normalize;
1074 cooccurrence[x][y].direction[i].blue*=normalize;
1075 if (image->colorspace == CMYKColorspace)
1076 cooccurrence[x][y].direction[i].black*=normalize;
1077 if (image->alpha_trait == BlendPixelTrait)
1078 cooccurrence[x][y].direction[i].alpha*=normalize;
1083 Compute texture features.
1085 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1086 #pragma omp parallel for schedule(static,4) shared(status) \
1087 magick_threads(image,image,number_grays,1)
1089 for (i=0; i < 4; i++)
1094 for (y=0; y < (ssize_t) number_grays; y++)
1099 for (x=0; x < (ssize_t) number_grays; x++)
1102 Angular second moment: measure of homogeneity of the image.
1104 channel_features[RedPixelChannel].angular_second_moment[i]+=
1105 cooccurrence[x][y].direction[i].red*
1106 cooccurrence[x][y].direction[i].red;
1107 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1108 cooccurrence[x][y].direction[i].green*
1109 cooccurrence[x][y].direction[i].green;
1110 channel_features[BluePixelChannel].angular_second_moment[i]+=
1111 cooccurrence[x][y].direction[i].blue*
1112 cooccurrence[x][y].direction[i].blue;
1113 if (image->colorspace == CMYKColorspace)
1114 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1115 cooccurrence[x][y].direction[i].black*
1116 cooccurrence[x][y].direction[i].black;
1117 if (image->alpha_trait == BlendPixelTrait)
1118 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1119 cooccurrence[x][y].direction[i].alpha*
1120 cooccurrence[x][y].direction[i].alpha;
1122 Correlation: measure of linear-dependencies in the image.
1124 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1125 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1126 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1127 if (image->colorspace == CMYKColorspace)
1128 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1129 if (image->alpha_trait == BlendPixelTrait)
1130 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1131 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1132 correlation.direction[i].green+=x*y*
1133 cooccurrence[x][y].direction[i].green;
1134 correlation.direction[i].blue+=x*y*
1135 cooccurrence[x][y].direction[i].blue;
1136 if (image->colorspace == CMYKColorspace)
1137 correlation.direction[i].black+=x*y*
1138 cooccurrence[x][y].direction[i].black;
1139 if (image->alpha_trait == BlendPixelTrait)
1140 correlation.direction[i].alpha+=x*y*
1141 cooccurrence[x][y].direction[i].alpha;
1143 Inverse Difference Moment.
1145 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1146 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1147 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1148 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1149 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1150 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1151 if (image->colorspace == CMYKColorspace)
1152 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1153 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1154 if (image->alpha_trait == BlendPixelTrait)
1155 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1156 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1160 density_xy[y+x+2].direction[i].red+=
1161 cooccurrence[x][y].direction[i].red;
1162 density_xy[y+x+2].direction[i].green+=
1163 cooccurrence[x][y].direction[i].green;
1164 density_xy[y+x+2].direction[i].blue+=
1165 cooccurrence[x][y].direction[i].blue;
1166 if (image->colorspace == CMYKColorspace)
1167 density_xy[y+x+2].direction[i].black+=
1168 cooccurrence[x][y].direction[i].black;
1169 if (image->alpha_trait == BlendPixelTrait)
1170 density_xy[y+x+2].direction[i].alpha+=
1171 cooccurrence[x][y].direction[i].alpha;
1175 channel_features[RedPixelChannel].entropy[i]-=
1176 cooccurrence[x][y].direction[i].red*
1177 MagickLog10(cooccurrence[x][y].direction[i].red);
1178 channel_features[GreenPixelChannel].entropy[i]-=
1179 cooccurrence[x][y].direction[i].green*
1180 MagickLog10(cooccurrence[x][y].direction[i].green);
1181 channel_features[BluePixelChannel].entropy[i]-=
1182 cooccurrence[x][y].direction[i].blue*
1183 MagickLog10(cooccurrence[x][y].direction[i].blue);
1184 if (image->colorspace == CMYKColorspace)
1185 channel_features[BlackPixelChannel].entropy[i]-=
1186 cooccurrence[x][y].direction[i].black*
1187 MagickLog10(cooccurrence[x][y].direction[i].black);
1188 if (image->alpha_trait == BlendPixelTrait)
1189 channel_features[AlphaPixelChannel].entropy[i]-=
1190 cooccurrence[x][y].direction[i].alpha*
1191 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1193 Information Measures of Correlation.
1195 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1196 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1197 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1198 if (image->alpha_trait == BlendPixelTrait)
1199 density_x[x].direction[i].alpha+=
1200 cooccurrence[x][y].direction[i].alpha;
1201 if (image->colorspace == CMYKColorspace)
1202 density_x[x].direction[i].black+=
1203 cooccurrence[x][y].direction[i].black;
1204 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1205 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1206 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1207 if (image->colorspace == CMYKColorspace)
1208 density_y[y].direction[i].black+=
1209 cooccurrence[x][y].direction[i].black;
1210 if (image->alpha_trait == BlendPixelTrait)
1211 density_y[y].direction[i].alpha+=
1212 cooccurrence[x][y].direction[i].alpha;
1214 mean.direction[i].red+=y*sum[y].direction[i].red;
1215 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1216 mean.direction[i].green+=y*sum[y].direction[i].green;
1217 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1218 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1219 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1220 if (image->colorspace == CMYKColorspace)
1222 mean.direction[i].black+=y*sum[y].direction[i].black;
1223 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1225 if (image->alpha_trait == BlendPixelTrait)
1227 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1228 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1232 Correlation: measure of linear-dependencies in the image.
1234 channel_features[RedPixelChannel].correlation[i]=
1235 (correlation.direction[i].red-mean.direction[i].red*
1236 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1237 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1238 sum_squares.direction[i].red-(mean.direction[i].red*
1239 mean.direction[i].red)));
1240 channel_features[GreenPixelChannel].correlation[i]=
1241 (correlation.direction[i].green-mean.direction[i].green*
1242 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1243 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1244 sum_squares.direction[i].green-(mean.direction[i].green*
1245 mean.direction[i].green)));
1246 channel_features[BluePixelChannel].correlation[i]=
1247 (correlation.direction[i].blue-mean.direction[i].blue*
1248 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1249 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1250 sum_squares.direction[i].blue-(mean.direction[i].blue*
1251 mean.direction[i].blue)));
1252 if (image->colorspace == CMYKColorspace)
1253 channel_features[BlackPixelChannel].correlation[i]=
1254 (correlation.direction[i].black-mean.direction[i].black*
1255 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1256 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1257 sum_squares.direction[i].black-(mean.direction[i].black*
1258 mean.direction[i].black)));
1259 if (image->alpha_trait == BlendPixelTrait)
1260 channel_features[AlphaPixelChannel].correlation[i]=
1261 (correlation.direction[i].alpha-mean.direction[i].alpha*
1262 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1263 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1264 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1265 mean.direction[i].alpha)));
1268 Compute more texture features.
1270 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1271 #pragma omp parallel for schedule(static,4) shared(status) \
1272 magick_threads(image,image,number_grays,1)
1274 for (i=0; i < 4; i++)
1279 for (x=2; x < (ssize_t) (2*number_grays); x++)
1284 channel_features[RedPixelChannel].sum_average[i]+=
1285 x*density_xy[x].direction[i].red;
1286 channel_features[GreenPixelChannel].sum_average[i]+=
1287 x*density_xy[x].direction[i].green;
1288 channel_features[BluePixelChannel].sum_average[i]+=
1289 x*density_xy[x].direction[i].blue;
1290 if (image->colorspace == CMYKColorspace)
1291 channel_features[BlackPixelChannel].sum_average[i]+=
1292 x*density_xy[x].direction[i].black;
1293 if (image->alpha_trait == BlendPixelTrait)
1294 channel_features[AlphaPixelChannel].sum_average[i]+=
1295 x*density_xy[x].direction[i].alpha;
1299 channel_features[RedPixelChannel].sum_entropy[i]-=
1300 density_xy[x].direction[i].red*
1301 MagickLog10(density_xy[x].direction[i].red);
1302 channel_features[GreenPixelChannel].sum_entropy[i]-=
1303 density_xy[x].direction[i].green*
1304 MagickLog10(density_xy[x].direction[i].green);
1305 channel_features[BluePixelChannel].sum_entropy[i]-=
1306 density_xy[x].direction[i].blue*
1307 MagickLog10(density_xy[x].direction[i].blue);
1308 if (image->colorspace == CMYKColorspace)
1309 channel_features[BlackPixelChannel].sum_entropy[i]-=
1310 density_xy[x].direction[i].black*
1311 MagickLog10(density_xy[x].direction[i].black);
1312 if (image->alpha_trait == BlendPixelTrait)
1313 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1314 density_xy[x].direction[i].alpha*
1315 MagickLog10(density_xy[x].direction[i].alpha);
1319 channel_features[RedPixelChannel].sum_variance[i]+=
1320 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1321 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1322 density_xy[x].direction[i].red;
1323 channel_features[GreenPixelChannel].sum_variance[i]+=
1324 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1325 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1326 density_xy[x].direction[i].green;
1327 channel_features[BluePixelChannel].sum_variance[i]+=
1328 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1329 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1330 density_xy[x].direction[i].blue;
1331 if (image->colorspace == CMYKColorspace)
1332 channel_features[BlackPixelChannel].sum_variance[i]+=
1333 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1334 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1335 density_xy[x].direction[i].black;
1336 if (image->alpha_trait == BlendPixelTrait)
1337 channel_features[AlphaPixelChannel].sum_variance[i]+=
1338 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1339 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1340 density_xy[x].direction[i].alpha;
1344 Compute more texture features.
1346 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1347 #pragma omp parallel for schedule(static,4) shared(status) \
1348 magick_threads(image,image,number_grays,1)
1350 for (i=0; i < 4; i++)
1355 for (y=0; y < (ssize_t) number_grays; y++)
1360 for (x=0; x < (ssize_t) number_grays; x++)
1363 Sum of Squares: Variance
1365 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1366 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1367 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1368 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1369 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1370 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1371 if (image->colorspace == CMYKColorspace)
1372 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1373 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1374 if (image->alpha_trait == BlendPixelTrait)
1375 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1376 (y-mean.direction[i].alpha+1)*
1377 cooccurrence[x][y].direction[i].alpha;
1379 Sum average / Difference Variance.
1381 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1382 cooccurrence[x][y].direction[i].red;
1383 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1384 cooccurrence[x][y].direction[i].green;
1385 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1386 cooccurrence[x][y].direction[i].blue;
1387 if (image->colorspace == CMYKColorspace)
1388 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1389 cooccurrence[x][y].direction[i].black;
1390 if (image->alpha_trait == BlendPixelTrait)
1391 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1392 cooccurrence[x][y].direction[i].alpha;
1394 Information Measures of Correlation.
1396 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1397 MagickLog10(cooccurrence[x][y].direction[i].red);
1398 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1399 MagickLog10(cooccurrence[x][y].direction[i].green);
1400 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1401 MagickLog10(cooccurrence[x][y].direction[i].blue);
1402 if (image->colorspace == CMYKColorspace)
1403 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1404 MagickLog10(cooccurrence[x][y].direction[i].black);
1405 if (image->alpha_trait == BlendPixelTrait)
1406 entropy_xy.direction[i].alpha-=
1407 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1408 cooccurrence[x][y].direction[i].alpha);
1409 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1410 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1411 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1412 MagickLog10(density_x[x].direction[i].green*
1413 density_y[y].direction[i].green));
1414 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1415 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1416 if (image->colorspace == CMYKColorspace)
1417 entropy_xy1.direction[i].black-=(
1418 cooccurrence[x][y].direction[i].black*MagickLog10(
1419 density_x[x].direction[i].black*density_y[y].direction[i].black));
1420 if (image->alpha_trait == BlendPixelTrait)
1421 entropy_xy1.direction[i].alpha-=(
1422 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1423 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1424 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1425 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1426 density_y[y].direction[i].red));
1427 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1428 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1429 density_y[y].direction[i].green));
1430 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1431 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1432 density_y[y].direction[i].blue));
1433 if (image->colorspace == CMYKColorspace)
1434 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1435 density_y[y].direction[i].black*MagickLog10(
1436 density_x[x].direction[i].black*density_y[y].direction[i].black));
1437 if (image->alpha_trait == BlendPixelTrait)
1438 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1439 density_y[y].direction[i].alpha*MagickLog10(
1440 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1443 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1444 variance.direction[i].red;
1445 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1446 variance.direction[i].green;
1447 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1448 variance.direction[i].blue;
1449 if (image->colorspace == CMYKColorspace)
1450 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1451 variance.direction[i].black;
1452 if (image->alpha_trait == BlendPixelTrait)
1453 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1454 variance.direction[i].alpha;
1457 Compute more texture features.
1459 (void) ResetMagickMemory(&variance,0,sizeof(variance));
1460 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
1461 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1462 #pragma omp parallel for schedule(static,4) shared(status) \
1463 magick_threads(image,image,number_grays,1)
1465 for (i=0; i < 4; i++)
1470 for (x=0; x < (ssize_t) number_grays; x++)
1473 Difference variance.
1475 variance.direction[i].red+=density_xy[x].direction[i].red;
1476 variance.direction[i].green+=density_xy[x].direction[i].green;
1477 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1478 if (image->colorspace == CMYKColorspace)
1479 variance.direction[i].black+=density_xy[x].direction[i].black;
1480 if (image->alpha_trait == BlendPixelTrait)
1481 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1482 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1483 density_xy[x].direction[i].red;
1484 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1485 density_xy[x].direction[i].green;
1486 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1487 density_xy[x].direction[i].blue;
1488 if (image->colorspace == CMYKColorspace)
1489 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1490 density_xy[x].direction[i].black;
1491 if (image->alpha_trait == BlendPixelTrait)
1492 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1493 density_xy[x].direction[i].alpha;
1497 channel_features[RedPixelChannel].difference_entropy[i]-=
1498 density_xy[x].direction[i].red*
1499 MagickLog10(density_xy[x].direction[i].red);
1500 channel_features[GreenPixelChannel].difference_entropy[i]-=
1501 density_xy[x].direction[i].green*
1502 MagickLog10(density_xy[x].direction[i].green);
1503 channel_features[BluePixelChannel].difference_entropy[i]-=
1504 density_xy[x].direction[i].blue*
1505 MagickLog10(density_xy[x].direction[i].blue);
1506 if (image->colorspace == CMYKColorspace)
1507 channel_features[BlackPixelChannel].difference_entropy[i]-=
1508 density_xy[x].direction[i].black*
1509 MagickLog10(density_xy[x].direction[i].black);
1510 if (image->alpha_trait == BlendPixelTrait)
1511 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1512 density_xy[x].direction[i].alpha*
1513 MagickLog10(density_xy[x].direction[i].alpha);
1515 Information Measures of Correlation.
1517 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1518 MagickLog10(density_x[x].direction[i].red));
1519 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1520 MagickLog10(density_x[x].direction[i].green));
1521 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1522 MagickLog10(density_x[x].direction[i].blue));
1523 if (image->colorspace == CMYKColorspace)
1524 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1525 MagickLog10(density_x[x].direction[i].black));
1526 if (image->alpha_trait == BlendPixelTrait)
1527 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1528 MagickLog10(density_x[x].direction[i].alpha));
1529 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1530 MagickLog10(density_y[x].direction[i].red));
1531 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1532 MagickLog10(density_y[x].direction[i].green));
1533 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1534 MagickLog10(density_y[x].direction[i].blue));
1535 if (image->colorspace == CMYKColorspace)
1536 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1537 MagickLog10(density_y[x].direction[i].black));
1538 if (image->alpha_trait == BlendPixelTrait)
1539 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1540 MagickLog10(density_y[x].direction[i].alpha));
1543 Difference variance.
1545 channel_features[RedPixelChannel].difference_variance[i]=
1546 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1547 (variance.direction[i].red*variance.direction[i].red))/
1548 ((double) number_grays*number_grays*number_grays*number_grays);
1549 channel_features[GreenPixelChannel].difference_variance[i]=
1550 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1551 (variance.direction[i].green*variance.direction[i].green))/
1552 ((double) number_grays*number_grays*number_grays*number_grays);
1553 channel_features[BluePixelChannel].difference_variance[i]=
1554 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1555 (variance.direction[i].blue*variance.direction[i].blue))/
1556 ((double) number_grays*number_grays*number_grays*number_grays);
1557 if (image->colorspace == CMYKColorspace)
1558 channel_features[BlackPixelChannel].difference_variance[i]=
1559 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1560 (variance.direction[i].black*variance.direction[i].black))/
1561 ((double) number_grays*number_grays*number_grays*number_grays);
1562 if (image->alpha_trait == BlendPixelTrait)
1563 channel_features[AlphaPixelChannel].difference_variance[i]=
1564 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1565 (variance.direction[i].alpha*variance.direction[i].alpha))/
1566 ((double) number_grays*number_grays*number_grays*number_grays);
1568 Information Measures of Correlation.
1570 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1571 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1572 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1573 entropy_x.direction[i].red : entropy_y.direction[i].red);
1574 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1575 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1576 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1577 entropy_x.direction[i].green : entropy_y.direction[i].green);
1578 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1579 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1580 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1581 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1582 if (image->colorspace == CMYKColorspace)
1583 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1584 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1585 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1586 entropy_x.direction[i].black : entropy_y.direction[i].black);
1587 if (image->alpha_trait == BlendPixelTrait)
1588 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1589 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1590 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1591 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1592 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1593 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].red-
1594 entropy_xy.direction[i].red)))));
1595 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1596 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].green-
1597 entropy_xy.direction[i].green)))));
1598 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1599 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].blue-
1600 entropy_xy.direction[i].blue)))));
1601 if (image->colorspace == CMYKColorspace)
1602 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1603 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].black-
1604 entropy_xy.direction[i].black)))));
1605 if (image->alpha_trait == BlendPixelTrait)
1606 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1607 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].alpha-
1608 entropy_xy.direction[i].alpha)))));
1611 Compute more texture features.
1613 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1614 #pragma omp parallel for schedule(static,4) shared(status) \
1615 magick_threads(image,image,number_grays,1)
1617 for (i=0; i < 4; i++)
1622 for (z=0; z < (ssize_t) number_grays; z++)
1630 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1631 for (y=0; y < (ssize_t) number_grays; y++)
1636 for (x=0; x < (ssize_t) number_grays; x++)
1639 Contrast: amount of local variations present in an image.
1641 if (((y-x) == z) || ((x-y) == z))
1643 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1644 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1645 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1646 if (image->colorspace == CMYKColorspace)
1647 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1648 if (image->alpha_trait == BlendPixelTrait)
1649 pixel.direction[i].alpha+=
1650 cooccurrence[x][y].direction[i].alpha;
1653 Maximum Correlation Coefficient.
1655 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1656 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1657 density_y[x].direction[i].red;
1658 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1659 cooccurrence[y][x].direction[i].green/
1660 density_x[z].direction[i].green/density_y[x].direction[i].red;
1661 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1662 cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/
1663 density_y[x].direction[i].blue;
1664 if (image->colorspace == CMYKColorspace)
1665 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1666 cooccurrence[y][x].direction[i].black/
1667 density_x[z].direction[i].black/density_y[x].direction[i].black;
1668 if (image->alpha_trait == BlendPixelTrait)
1669 Q[z][y].direction[i].alpha+=
1670 cooccurrence[z][x].direction[i].alpha*
1671 cooccurrence[y][x].direction[i].alpha/
1672 density_x[z].direction[i].alpha/
1673 density_y[x].direction[i].alpha;
1676 channel_features[RedPixelChannel].contrast[i]+=z*z*
1677 pixel.direction[i].red;
1678 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1679 pixel.direction[i].green;
1680 channel_features[BluePixelChannel].contrast[i]+=z*z*
1681 pixel.direction[i].blue;
1682 if (image->colorspace == CMYKColorspace)
1683 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1684 pixel.direction[i].black;
1685 if (image->alpha_trait == BlendPixelTrait)
1686 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1687 pixel.direction[i].alpha;
1690 Maximum Correlation Coefficient.
1691 Future: return second largest eigenvalue of Q.
1693 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1694 sqrt((double) -1.0);
1695 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1696 sqrt((double) -1.0);
1697 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1698 sqrt((double) -1.0);
1699 if (image->colorspace == CMYKColorspace)
1700 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1701 sqrt((double) -1.0);
1702 if (image->alpha_trait == BlendPixelTrait)
1703 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1704 sqrt((double) -1.0);
1707 Relinquish resources.
1709 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
1710 for (i=0; i < (ssize_t) number_grays; i++)
1711 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1712 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1713 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1714 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1715 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
1716 for (i=0; i < (ssize_t) number_grays; i++)
1717 cooccurrence[i]=(ChannelStatistics *)
1718 RelinquishMagickMemory(cooccurrence[i]);
1719 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1720 return(channel_features);
1724 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1728 % H o u g h L i n e I m a g e %
1732 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1734 % HoughLineImage() identifies lines in the image.
1736 % The format of the HoughLineImage method is:
1738 % Image *HoughLineImage(const Image *image,const size_t width,
1739 % const size_t height,const size_t threshold,ExceptionInfo *exception)
1741 % A description of each parameter follows:
1743 % o image: the image.
1745 % o width, height: find line pairs as local maxima in this neighborhood.
1747 % o threshold: the line count threshold.
1749 % o exception: return any errors or warnings in this structure.
1753 static inline double MagickRound(double x)
1756 Round the fraction to nearest integer.
1758 if ((x-floor(x)) < (ceil(x)-x))
1763 MagickExport Image *HoughLineImage(const Image *image,const size_t width,
1764 const size_t height,const size_t threshold,ExceptionInfo *exception)
1766 #define HoughLineImageTag "HoughLine/Image"
1772 message[MaxTextExtent],
1773 path[MaxTextExtent];
1782 *lines_image = NULL;
1811 Create the accumulator.
1813 assert(image != (const Image *) NULL);
1814 assert(image->signature == MagickSignature);
1815 if (image->debug != MagickFalse)
1816 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1817 assert(exception != (ExceptionInfo *) NULL);
1818 assert(exception->signature == MagickSignature);
1819 accumulator_width=180;
1820 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1821 image->rows : image->columns))/2.0);
1822 accumulator_height=(size_t) (2.0*hough_height);
1823 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1824 sizeof(double),exception);
1825 if (accumulator == (MatrixInfo *) NULL)
1826 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1827 if (NullMatrix(accumulator) == MagickFalse)
1829 accumulator=DestroyMatrixInfo(accumulator);
1830 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1833 Populate the accumulator.
1837 center.x=(double) image->columns/2.0;
1838 center.y=(double) image->rows/2.0;
1839 image_view=AcquireVirtualCacheView(image,exception);
1840 for (y=0; y < (ssize_t) image->rows; y++)
1842 register const Quantum
1848 if (status == MagickFalse)
1850 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1851 if (p == (Quantum *) NULL)
1856 for (x=0; x < (ssize_t) image->columns; x++)
1858 if (GetPixelIntensity(image,p) > (QuantumRange/2))
1863 for (i=0; i < 180; i++)
1869 radius=(((double) x-center.x)*cos(DegreesToRadians((double) i)))+
1870 (((double) y-center.y)*sin(DegreesToRadians((double) i)));
1871 (void) GetMatrixElement(accumulator,i,(ssize_t)
1872 MagickRound(radius+hough_height),&count);
1874 (void) SetMatrixElement(accumulator,i,(ssize_t)
1875 MagickRound(radius+hough_height),&count);
1878 p+=GetPixelChannels(image);
1880 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1885 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1886 #pragma omp critical (MagickCore_CannyEdgeImage)
1888 proceed=SetImageProgress(image,CannyEdgeImageTag,progress++,
1890 if (proceed == MagickFalse)
1894 image_view=DestroyCacheView(image_view);
1895 if (status == MagickFalse)
1897 accumulator=DestroyMatrixInfo(accumulator);
1898 return((Image *) NULL);
1901 Generate line segments from accumulator.
1903 file=AcquireUniqueFileResource(path);
1906 accumulator=DestroyMatrixInfo(accumulator);
1907 return((Image *) NULL);
1909 (void) FormatLocaleString(message,MaxTextExtent,
1910 "# Hough line transform: %.20gx%.20g%+.20g\n",(double) width,
1911 (double) height,(double) threshold);
1912 (void) write(file,message,strlen(message));
1913 (void) FormatLocaleString(message,MaxTextExtent,"viewbox 0 0 %.20g %.20g\n",
1914 (double) image->columns,(double) image->rows);
1915 (void) write(file,message,strlen(message));
1916 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1918 line_count=threshold;
1919 for (y=0; y < (ssize_t) accumulator_height; y++)
1924 for (x=0; x < (ssize_t) accumulator_width; x++)
1929 (void) GetMatrixElement(accumulator,x,y,&count);
1930 if (count >= (double) line_count)
1942 Is point a local maxima?
1945 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
1950 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
1952 if ((u != 0) || (v !=0))
1954 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
1962 if (u < (ssize_t) (width/2))
1965 (void) GetMatrixElement(accumulator,x,y,&count);
1968 if ((x >= 45) && (x <= 135))
1971 y = (r-x cos(t))/sin(t)
1974 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
1975 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1976 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1977 line.x2=(double) image->columns;
1978 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
1979 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1980 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1985 x = (r-y cos(t))/sin(t)
1988 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
1989 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1990 cos(DegreesToRadians((double) x))+(image->columns/2.0);
1991 line.y2=(double) image->rows;
1992 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
1993 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1994 cos(DegreesToRadians((double) x))+(image->columns/2.0);
1996 (void) FormatLocaleString(message,MaxTextExtent,
1997 "line %g,%g %g,%g # %g\n",line.x1,line.y1,line.x2,line.y2,maxima);
1998 (void) write(file,message,strlen(message));
2004 Render lines to image canvas.
2006 image_info=AcquireImageInfo();
2007 image_info->background_color=image->background_color;
2008 (void) FormatLocaleString(image_info->filename,MaxTextExtent,"mvg:%s",path);
2009 artifact=GetImageArtifact(image,"background");
2010 if (artifact != (const char *) NULL)
2011 (void) SetImageOption(image_info,"background",artifact);
2012 artifact=GetImageArtifact(image,"fill");
2013 if (artifact != (const char *) NULL)
2014 (void) SetImageOption(image_info,"fill",artifact);
2015 artifact=GetImageArtifact(image,"stroke");
2016 if (artifact != (const char *) NULL)
2017 (void) SetImageOption(image_info,"stroke",artifact);
2018 artifact=GetImageArtifact(image,"strokewidth");
2019 if (artifact != (const char *) NULL)
2020 (void) SetImageOption(image_info,"strokewidth",artifact);
2021 lines_image=ReadImage(image_info,exception);
2022 artifact=GetImageArtifact(image,"hough-lines:accumulator");
2023 if ((lines_image != (Image *) NULL) &&
2024 (IsStringTrue(artifact) != MagickFalse))
2029 accumulator_image=MatrixToImage(accumulator,exception);
2030 if (accumulator_image != (Image *) NULL)
2031 AppendImageToList(&lines_image,accumulator_image);
2036 accumulator=DestroyMatrixInfo(accumulator);
2037 image_info=DestroyImageInfo(image_info);
2038 (void) RelinquishUniqueFileResource(path);
2039 return(GetFirstImageInList(lines_image));
2043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2047 % M e a n S h i f t I m a g e %
2051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2053 % MeanShiftImage() delineate arbitrarily shaped clusters in the image. For
2054 % each pixel, it visits all the pixels in the neighborhood specified by
2055 % the window centered at the pixel and excludes those that are outside the
2056 % radius=(window-1)/2 surrounding the pixel. From those pixels, it finds those
2057 % that are within the specified color distance from the current mean, and
2058 % computes a new x,y centroid from those coordinates and a new mean. This new
2059 % x,y centroid is used as the center for a new window. This process iterates
2060 % until it converges and the final mean is replaces the (original window
2061 % center) pixel value. It repeats this process for the next pixel, etc.,
2062 % until it processes all pixels in the image. Results are typically better with
2063 % colorspaces other than sRGB. We recommend YIQ, YUV or YCbCr.
2065 % The format of the MeanShiftImage method is:
2067 % Image *MeanShiftImage(const Image *image,const size_t width,
2068 % const size_t height,const double color_distance,
2069 % ExceptionInfo *exception)
2071 % A description of each parameter follows:
2073 % o image: the image.
2075 % o width, height: find pixels in this neighborhood.
2077 % o color_distance: the color distance.
2079 % o exception: return any errors or warnings in this structure.
2082 MagickExport Image *MeanShiftImage(const Image *image,const size_t width,
2083 const size_t height,const double color_distance,ExceptionInfo *exception)
2085 #define MaxMeanShiftIterations 100
2086 #define MeanShiftImageTag "MeanShift/Image"
2105 assert(image != (const Image *) NULL);
2106 assert(image->signature == MagickSignature);
2107 if (image->debug != MagickFalse)
2108 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2109 assert(exception != (ExceptionInfo *) NULL);
2110 assert(exception->signature == MagickSignature);
2111 mean_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2112 if (mean_image == (Image *) NULL)
2113 return((Image *) NULL);
2114 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2116 mean_image=DestroyImage(mean_image);
2117 return((Image *) NULL);
2121 image_view=AcquireVirtualCacheView(image,exception);
2122 pixel_view=AcquireVirtualCacheView(image,exception);
2123 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2124 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2125 #pragma omp parallel for schedule(static,4) shared(status,progress) \
2126 magick_threads(mean_image,mean_image,mean_image->rows,1)
2128 for (y=0; y < (ssize_t) mean_image->rows; y++)
2130 register const Quantum
2139 if (status == MagickFalse)
2141 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2142 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2144 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2149 for (x=0; x < (ssize_t) mean_image->columns; x++)
2162 GetPixelInfo(image,&mean_pixel);
2163 GetPixelInfoPixel(image,p,&mean_pixel);
2164 mean_location.x=(double) x;
2165 mean_location.y=(double) y;
2166 for (i=0; i < MaxMeanShiftIterations; i++)
2184 GetPixelInfo(image,&sum_pixel);
2185 previous_location=mean_location;
2186 previous_pixel=mean_pixel;
2188 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2193 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2195 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2200 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2201 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2202 mean_location.y+v),&pixel,exception);
2203 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2204 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2205 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2206 if (distance <= (color_distance*color_distance))
2208 sum_location.x+=mean_location.x+u;
2209 sum_location.y+=mean_location.y+v;
2210 sum_pixel.red+=pixel.red;
2211 sum_pixel.green+=pixel.green;
2212 sum_pixel.blue+=pixel.blue;
2213 sum_pixel.alpha+=pixel.alpha;
2220 mean_location.x=gamma*sum_location.x;
2221 mean_location.y=gamma*sum_location.y;
2222 mean_pixel.red=gamma*sum_pixel.red;
2223 mean_pixel.green=gamma*sum_pixel.green;
2224 mean_pixel.blue=gamma*sum_pixel.blue;
2225 mean_pixel.alpha=gamma*sum_pixel.alpha;
2226 distance=(mean_location.x-previous_location.x)*
2227 (mean_location.x-previous_location.x)+
2228 (mean_location.y-previous_location.y)*
2229 (mean_location.y-previous_location.y)+
2230 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2231 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2232 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2233 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2234 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2235 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2236 if (distance <= 3.0)
2239 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2240 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2241 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2242 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2243 p+=GetPixelChannels(image);
2244 q+=GetPixelChannels(mean_image);
2246 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2248 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2253 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2254 #pragma omp critical (MagickCore_MeanShiftImage)
2256 proceed=SetImageProgress(image,MeanShiftImageTag,progress++,
2258 if (proceed == MagickFalse)
2262 mean_view=DestroyCacheView(mean_view);
2263 pixel_view=DestroyCacheView(pixel_view);
2264 image_view=DestroyCacheView(image_view);