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-2015 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 CannyEdgeImage 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,exception);
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 double MagickLog10(const double x)
614 #define Log10Epsilon (1.0e-11)
616 if (fabs(x) < Log10Epsilon)
617 return(log10(Log10Epsilon));
618 return(log10(fabs(x)));
621 MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
622 const size_t distance,ExceptionInfo *exception)
624 typedef struct _ChannelStatistics
627 direction[4]; /* horizontal, vertical, left and right diagonals */
672 assert(image != (Image *) NULL);
673 assert(image->signature == MagickSignature);
674 if (image->debug != MagickFalse)
675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
676 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
677 return((ChannelFeatures *) NULL);
678 length=CompositeChannels+1UL;
679 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
680 sizeof(*channel_features));
681 if (channel_features == (ChannelFeatures *) NULL)
682 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
683 (void) ResetMagickMemory(channel_features,0,length*
684 sizeof(*channel_features));
688 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
689 if (grays == (PixelPacket *) NULL)
691 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
695 return(channel_features);
697 for (i=0; i <= (ssize_t) MaxMap; i++)
700 grays[i].green=(~0U);
702 grays[i].alpha=(~0U);
703 grays[i].black=(~0U);
706 image_view=AcquireVirtualCacheView(image,exception);
707 #if defined(MAGICKCORE_OPENMP_SUPPORT)
708 #pragma omp parallel for schedule(static,4) shared(status) \
709 magick_threads(image,image,image->rows,1)
711 for (y=0; y < (ssize_t) image->rows; y++)
713 register const Quantum
719 if (status == MagickFalse)
721 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
722 if (p == (const Quantum *) NULL)
727 for (x=0; x < (ssize_t) image->columns; x++)
729 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
730 ScaleQuantumToMap(GetPixelRed(image,p));
731 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
732 ScaleQuantumToMap(GetPixelGreen(image,p));
733 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
734 ScaleQuantumToMap(GetPixelBlue(image,p));
735 if (image->colorspace == CMYKColorspace)
736 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
737 ScaleQuantumToMap(GetPixelBlack(image,p));
738 if (image->alpha_trait != UndefinedPixelTrait)
739 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
740 ScaleQuantumToMap(GetPixelAlpha(image,p));
741 p+=GetPixelChannels(image);
744 image_view=DestroyCacheView(image_view);
745 if (status == MagickFalse)
747 grays=(PixelPacket *) RelinquishMagickMemory(grays);
748 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
750 return(channel_features);
752 (void) ResetMagickMemory(&gray,0,sizeof(gray));
753 for (i=0; i <= (ssize_t) MaxMap; i++)
755 if (grays[i].red != ~0U)
756 grays[gray.red++].red=grays[i].red;
757 if (grays[i].green != ~0U)
758 grays[gray.green++].green=grays[i].green;
759 if (grays[i].blue != ~0U)
760 grays[gray.blue++].blue=grays[i].blue;
761 if (image->colorspace == CMYKColorspace)
762 if (grays[i].black != ~0U)
763 grays[gray.black++].black=grays[i].black;
764 if (image->alpha_trait != UndefinedPixelTrait)
765 if (grays[i].alpha != ~0U)
766 grays[gray.alpha++].alpha=grays[i].alpha;
769 Allocate spatial dependence matrix.
771 number_grays=gray.red;
772 if (gray.green > number_grays)
773 number_grays=gray.green;
774 if (gray.blue > number_grays)
775 number_grays=gray.blue;
776 if (image->colorspace == CMYKColorspace)
777 if (gray.black > number_grays)
778 number_grays=gray.black;
779 if (image->alpha_trait != UndefinedPixelTrait)
780 if (gray.alpha > number_grays)
781 number_grays=gray.alpha;
782 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
783 sizeof(*cooccurrence));
784 density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
786 density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
787 sizeof(*density_xy));
788 density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
790 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
791 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
792 if ((cooccurrence == (ChannelStatistics **) NULL) ||
793 (density_x == (ChannelStatistics *) NULL) ||
794 (density_xy == (ChannelStatistics *) NULL) ||
795 (density_y == (ChannelStatistics *) NULL) ||
796 (Q == (ChannelStatistics **) NULL) ||
797 (sum == (ChannelStatistics *) NULL))
799 if (Q != (ChannelStatistics **) NULL)
801 for (i=0; i < (ssize_t) number_grays; i++)
802 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
803 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
805 if (sum != (ChannelStatistics *) NULL)
806 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
807 if (density_y != (ChannelStatistics *) NULL)
808 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
809 if (density_xy != (ChannelStatistics *) NULL)
810 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
811 if (density_x != (ChannelStatistics *) NULL)
812 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
813 if (cooccurrence != (ChannelStatistics **) NULL)
815 for (i=0; i < (ssize_t) number_grays; i++)
816 cooccurrence[i]=(ChannelStatistics *)
817 RelinquishMagickMemory(cooccurrence[i]);
818 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
821 grays=(PixelPacket *) RelinquishMagickMemory(grays);
822 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
824 (void) ThrowMagickException(exception,GetMagickModule(),
825 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
826 return(channel_features);
828 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
829 (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x));
830 (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
831 (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y));
832 (void) ResetMagickMemory(&mean,0,sizeof(mean));
833 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
834 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
835 (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy));
836 (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x));
837 (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy));
838 (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1));
839 (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2));
840 (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y));
841 (void) ResetMagickMemory(&variance,0,sizeof(variance));
842 for (i=0; i < (ssize_t) number_grays; i++)
844 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
845 sizeof(**cooccurrence));
846 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
847 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
848 (Q[i] == (ChannelStatistics *) NULL))
850 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
851 sizeof(**cooccurrence));
852 (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q));
854 if (i < (ssize_t) number_grays)
856 for (i--; i >= 0; i--)
858 if (Q[i] != (ChannelStatistics *) NULL)
859 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
860 if (cooccurrence[i] != (ChannelStatistics *) NULL)
861 cooccurrence[i]=(ChannelStatistics *)
862 RelinquishMagickMemory(cooccurrence[i]);
864 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
865 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
866 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
867 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
868 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
869 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
870 grays=(PixelPacket *) RelinquishMagickMemory(grays);
871 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
873 (void) ThrowMagickException(exception,GetMagickModule(),
874 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
875 return(channel_features);
878 Initialize spatial dependence matrix.
881 image_view=AcquireVirtualCacheView(image,exception);
882 for (y=0; y < (ssize_t) image->rows; y++)
884 register const Quantum
896 if (status == MagickFalse)
898 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+
899 2*distance,distance+2,exception);
900 if (p == (const Quantum *) NULL)
905 p+=distance*GetPixelChannels(image);;
906 for (x=0; x < (ssize_t) image->columns; x++)
908 for (i=0; i < 4; i++)
916 Horizontal adjacency.
918 offset=(ssize_t) distance;
926 offset=(ssize_t) (image->columns+2*distance);
932 Right diagonal adjacency.
934 offset=(ssize_t) ((image->columns+2*distance)-distance);
940 Left diagonal adjacency.
942 offset=(ssize_t) ((image->columns+2*distance)+distance);
948 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
950 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
952 cooccurrence[u][v].direction[i].red++;
953 cooccurrence[v][u].direction[i].red++;
956 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
958 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
960 cooccurrence[u][v].direction[i].green++;
961 cooccurrence[v][u].direction[i].green++;
964 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
966 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
968 cooccurrence[u][v].direction[i].blue++;
969 cooccurrence[v][u].direction[i].blue++;
970 if (image->colorspace == CMYKColorspace)
974 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
976 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
978 cooccurrence[u][v].direction[i].black++;
979 cooccurrence[v][u].direction[i].black++;
981 if (image->alpha_trait != UndefinedPixelTrait)
985 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
987 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
989 cooccurrence[u][v].direction[i].alpha++;
990 cooccurrence[v][u].direction[i].alpha++;
993 p+=GetPixelChannels(image);
996 grays=(PixelPacket *) RelinquishMagickMemory(grays);
997 image_view=DestroyCacheView(image_view);
998 if (status == MagickFalse)
1000 for (i=0; i < (ssize_t) number_grays; i++)
1001 cooccurrence[i]=(ChannelStatistics *)
1002 RelinquishMagickMemory(cooccurrence[i]);
1003 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1004 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
1006 (void) ThrowMagickException(exception,GetMagickModule(),
1007 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1008 return(channel_features);
1011 Normalize spatial dependence matrix.
1013 for (i=0; i < 4; i++)
1027 Horizontal adjacency.
1029 normalize=2.0*image->rows*(image->columns-distance);
1037 normalize=2.0*(image->rows-distance)*image->columns;
1043 Right diagonal adjacency.
1045 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1051 Left diagonal adjacency.
1053 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1057 normalize=PerceptibleReciprocal(normalize);
1058 for (y=0; y < (ssize_t) number_grays; y++)
1063 for (x=0; x < (ssize_t) number_grays; x++)
1065 cooccurrence[x][y].direction[i].red*=normalize;
1066 cooccurrence[x][y].direction[i].green*=normalize;
1067 cooccurrence[x][y].direction[i].blue*=normalize;
1068 if (image->colorspace == CMYKColorspace)
1069 cooccurrence[x][y].direction[i].black*=normalize;
1070 if (image->alpha_trait != UndefinedPixelTrait)
1071 cooccurrence[x][y].direction[i].alpha*=normalize;
1076 Compute texture features.
1078 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1079 #pragma omp parallel for schedule(static,4) shared(status) \
1080 magick_threads(image,image,number_grays,1)
1082 for (i=0; i < 4; i++)
1087 for (y=0; y < (ssize_t) number_grays; y++)
1092 for (x=0; x < (ssize_t) number_grays; x++)
1095 Angular second moment: measure of homogeneity of the image.
1097 channel_features[RedPixelChannel].angular_second_moment[i]+=
1098 cooccurrence[x][y].direction[i].red*
1099 cooccurrence[x][y].direction[i].red;
1100 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1101 cooccurrence[x][y].direction[i].green*
1102 cooccurrence[x][y].direction[i].green;
1103 channel_features[BluePixelChannel].angular_second_moment[i]+=
1104 cooccurrence[x][y].direction[i].blue*
1105 cooccurrence[x][y].direction[i].blue;
1106 if (image->colorspace == CMYKColorspace)
1107 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1108 cooccurrence[x][y].direction[i].black*
1109 cooccurrence[x][y].direction[i].black;
1110 if (image->alpha_trait != UndefinedPixelTrait)
1111 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1112 cooccurrence[x][y].direction[i].alpha*
1113 cooccurrence[x][y].direction[i].alpha;
1115 Correlation: measure of linear-dependencies in the image.
1117 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1118 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1119 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1120 if (image->colorspace == CMYKColorspace)
1121 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1122 if (image->alpha_trait != UndefinedPixelTrait)
1123 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1124 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1125 correlation.direction[i].green+=x*y*
1126 cooccurrence[x][y].direction[i].green;
1127 correlation.direction[i].blue+=x*y*
1128 cooccurrence[x][y].direction[i].blue;
1129 if (image->colorspace == CMYKColorspace)
1130 correlation.direction[i].black+=x*y*
1131 cooccurrence[x][y].direction[i].black;
1132 if (image->alpha_trait != UndefinedPixelTrait)
1133 correlation.direction[i].alpha+=x*y*
1134 cooccurrence[x][y].direction[i].alpha;
1136 Inverse Difference Moment.
1138 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1139 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1140 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1141 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1142 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1143 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1144 if (image->colorspace == CMYKColorspace)
1145 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1146 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1147 if (image->alpha_trait != UndefinedPixelTrait)
1148 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1149 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1153 density_xy[y+x+2].direction[i].red+=
1154 cooccurrence[x][y].direction[i].red;
1155 density_xy[y+x+2].direction[i].green+=
1156 cooccurrence[x][y].direction[i].green;
1157 density_xy[y+x+2].direction[i].blue+=
1158 cooccurrence[x][y].direction[i].blue;
1159 if (image->colorspace == CMYKColorspace)
1160 density_xy[y+x+2].direction[i].black+=
1161 cooccurrence[x][y].direction[i].black;
1162 if (image->alpha_trait != UndefinedPixelTrait)
1163 density_xy[y+x+2].direction[i].alpha+=
1164 cooccurrence[x][y].direction[i].alpha;
1168 channel_features[RedPixelChannel].entropy[i]-=
1169 cooccurrence[x][y].direction[i].red*
1170 MagickLog10(cooccurrence[x][y].direction[i].red);
1171 channel_features[GreenPixelChannel].entropy[i]-=
1172 cooccurrence[x][y].direction[i].green*
1173 MagickLog10(cooccurrence[x][y].direction[i].green);
1174 channel_features[BluePixelChannel].entropy[i]-=
1175 cooccurrence[x][y].direction[i].blue*
1176 MagickLog10(cooccurrence[x][y].direction[i].blue);
1177 if (image->colorspace == CMYKColorspace)
1178 channel_features[BlackPixelChannel].entropy[i]-=
1179 cooccurrence[x][y].direction[i].black*
1180 MagickLog10(cooccurrence[x][y].direction[i].black);
1181 if (image->alpha_trait != UndefinedPixelTrait)
1182 channel_features[AlphaPixelChannel].entropy[i]-=
1183 cooccurrence[x][y].direction[i].alpha*
1184 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1186 Information Measures of Correlation.
1188 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1189 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1190 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1191 if (image->alpha_trait != UndefinedPixelTrait)
1192 density_x[x].direction[i].alpha+=
1193 cooccurrence[x][y].direction[i].alpha;
1194 if (image->colorspace == CMYKColorspace)
1195 density_x[x].direction[i].black+=
1196 cooccurrence[x][y].direction[i].black;
1197 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1198 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1199 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1200 if (image->colorspace == CMYKColorspace)
1201 density_y[y].direction[i].black+=
1202 cooccurrence[x][y].direction[i].black;
1203 if (image->alpha_trait != UndefinedPixelTrait)
1204 density_y[y].direction[i].alpha+=
1205 cooccurrence[x][y].direction[i].alpha;
1207 mean.direction[i].red+=y*sum[y].direction[i].red;
1208 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1209 mean.direction[i].green+=y*sum[y].direction[i].green;
1210 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1211 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1212 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1213 if (image->colorspace == CMYKColorspace)
1215 mean.direction[i].black+=y*sum[y].direction[i].black;
1216 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1218 if (image->alpha_trait != UndefinedPixelTrait)
1220 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1221 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1225 Correlation: measure of linear-dependencies in the image.
1227 channel_features[RedPixelChannel].correlation[i]=
1228 (correlation.direction[i].red-mean.direction[i].red*
1229 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1230 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1231 sum_squares.direction[i].red-(mean.direction[i].red*
1232 mean.direction[i].red)));
1233 channel_features[GreenPixelChannel].correlation[i]=
1234 (correlation.direction[i].green-mean.direction[i].green*
1235 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1236 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1237 sum_squares.direction[i].green-(mean.direction[i].green*
1238 mean.direction[i].green)));
1239 channel_features[BluePixelChannel].correlation[i]=
1240 (correlation.direction[i].blue-mean.direction[i].blue*
1241 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1242 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1243 sum_squares.direction[i].blue-(mean.direction[i].blue*
1244 mean.direction[i].blue)));
1245 if (image->colorspace == CMYKColorspace)
1246 channel_features[BlackPixelChannel].correlation[i]=
1247 (correlation.direction[i].black-mean.direction[i].black*
1248 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1249 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1250 sum_squares.direction[i].black-(mean.direction[i].black*
1251 mean.direction[i].black)));
1252 if (image->alpha_trait != UndefinedPixelTrait)
1253 channel_features[AlphaPixelChannel].correlation[i]=
1254 (correlation.direction[i].alpha-mean.direction[i].alpha*
1255 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1256 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1257 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1258 mean.direction[i].alpha)));
1261 Compute more texture features.
1263 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1264 #pragma omp parallel for schedule(static,4) shared(status) \
1265 magick_threads(image,image,number_grays,1)
1267 for (i=0; i < 4; i++)
1272 for (x=2; x < (ssize_t) (2*number_grays); x++)
1277 channel_features[RedPixelChannel].sum_average[i]+=
1278 x*density_xy[x].direction[i].red;
1279 channel_features[GreenPixelChannel].sum_average[i]+=
1280 x*density_xy[x].direction[i].green;
1281 channel_features[BluePixelChannel].sum_average[i]+=
1282 x*density_xy[x].direction[i].blue;
1283 if (image->colorspace == CMYKColorspace)
1284 channel_features[BlackPixelChannel].sum_average[i]+=
1285 x*density_xy[x].direction[i].black;
1286 if (image->alpha_trait != UndefinedPixelTrait)
1287 channel_features[AlphaPixelChannel].sum_average[i]+=
1288 x*density_xy[x].direction[i].alpha;
1292 channel_features[RedPixelChannel].sum_entropy[i]-=
1293 density_xy[x].direction[i].red*
1294 MagickLog10(density_xy[x].direction[i].red);
1295 channel_features[GreenPixelChannel].sum_entropy[i]-=
1296 density_xy[x].direction[i].green*
1297 MagickLog10(density_xy[x].direction[i].green);
1298 channel_features[BluePixelChannel].sum_entropy[i]-=
1299 density_xy[x].direction[i].blue*
1300 MagickLog10(density_xy[x].direction[i].blue);
1301 if (image->colorspace == CMYKColorspace)
1302 channel_features[BlackPixelChannel].sum_entropy[i]-=
1303 density_xy[x].direction[i].black*
1304 MagickLog10(density_xy[x].direction[i].black);
1305 if (image->alpha_trait != UndefinedPixelTrait)
1306 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1307 density_xy[x].direction[i].alpha*
1308 MagickLog10(density_xy[x].direction[i].alpha);
1312 channel_features[RedPixelChannel].sum_variance[i]+=
1313 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1314 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1315 density_xy[x].direction[i].red;
1316 channel_features[GreenPixelChannel].sum_variance[i]+=
1317 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1318 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1319 density_xy[x].direction[i].green;
1320 channel_features[BluePixelChannel].sum_variance[i]+=
1321 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1322 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1323 density_xy[x].direction[i].blue;
1324 if (image->colorspace == CMYKColorspace)
1325 channel_features[BlackPixelChannel].sum_variance[i]+=
1326 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1327 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1328 density_xy[x].direction[i].black;
1329 if (image->alpha_trait != UndefinedPixelTrait)
1330 channel_features[AlphaPixelChannel].sum_variance[i]+=
1331 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1332 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1333 density_xy[x].direction[i].alpha;
1337 Compute more texture features.
1339 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1340 #pragma omp parallel for schedule(static,4) shared(status) \
1341 magick_threads(image,image,number_grays,1)
1343 for (i=0; i < 4; i++)
1348 for (y=0; y < (ssize_t) number_grays; y++)
1353 for (x=0; x < (ssize_t) number_grays; x++)
1356 Sum of Squares: Variance
1358 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1359 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1360 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1361 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1362 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1363 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1364 if (image->colorspace == CMYKColorspace)
1365 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1366 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1367 if (image->alpha_trait != UndefinedPixelTrait)
1368 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1369 (y-mean.direction[i].alpha+1)*
1370 cooccurrence[x][y].direction[i].alpha;
1372 Sum average / Difference Variance.
1374 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1375 cooccurrence[x][y].direction[i].red;
1376 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1377 cooccurrence[x][y].direction[i].green;
1378 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1379 cooccurrence[x][y].direction[i].blue;
1380 if (image->colorspace == CMYKColorspace)
1381 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1382 cooccurrence[x][y].direction[i].black;
1383 if (image->alpha_trait != UndefinedPixelTrait)
1384 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1385 cooccurrence[x][y].direction[i].alpha;
1387 Information Measures of Correlation.
1389 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1390 MagickLog10(cooccurrence[x][y].direction[i].red);
1391 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1392 MagickLog10(cooccurrence[x][y].direction[i].green);
1393 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1394 MagickLog10(cooccurrence[x][y].direction[i].blue);
1395 if (image->colorspace == CMYKColorspace)
1396 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1397 MagickLog10(cooccurrence[x][y].direction[i].black);
1398 if (image->alpha_trait != UndefinedPixelTrait)
1399 entropy_xy.direction[i].alpha-=
1400 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1401 cooccurrence[x][y].direction[i].alpha);
1402 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1403 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1404 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1405 MagickLog10(density_x[x].direction[i].green*
1406 density_y[y].direction[i].green));
1407 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1408 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1409 if (image->colorspace == CMYKColorspace)
1410 entropy_xy1.direction[i].black-=(
1411 cooccurrence[x][y].direction[i].black*MagickLog10(
1412 density_x[x].direction[i].black*density_y[y].direction[i].black));
1413 if (image->alpha_trait != UndefinedPixelTrait)
1414 entropy_xy1.direction[i].alpha-=(
1415 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1416 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1417 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1418 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1419 density_y[y].direction[i].red));
1420 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1421 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1422 density_y[y].direction[i].green));
1423 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1424 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1425 density_y[y].direction[i].blue));
1426 if (image->colorspace == CMYKColorspace)
1427 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1428 density_y[y].direction[i].black*MagickLog10(
1429 density_x[x].direction[i].black*density_y[y].direction[i].black));
1430 if (image->alpha_trait != UndefinedPixelTrait)
1431 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1432 density_y[y].direction[i].alpha*MagickLog10(
1433 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1436 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1437 variance.direction[i].red;
1438 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1439 variance.direction[i].green;
1440 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1441 variance.direction[i].blue;
1442 if (image->colorspace == CMYKColorspace)
1443 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1444 variance.direction[i].black;
1445 if (image->alpha_trait != UndefinedPixelTrait)
1446 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1447 variance.direction[i].alpha;
1450 Compute more texture features.
1452 (void) ResetMagickMemory(&variance,0,sizeof(variance));
1453 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
1454 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1455 #pragma omp parallel for schedule(static,4) shared(status) \
1456 magick_threads(image,image,number_grays,1)
1458 for (i=0; i < 4; i++)
1463 for (x=0; x < (ssize_t) number_grays; x++)
1466 Difference variance.
1468 variance.direction[i].red+=density_xy[x].direction[i].red;
1469 variance.direction[i].green+=density_xy[x].direction[i].green;
1470 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1471 if (image->colorspace == CMYKColorspace)
1472 variance.direction[i].black+=density_xy[x].direction[i].black;
1473 if (image->alpha_trait != UndefinedPixelTrait)
1474 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1475 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1476 density_xy[x].direction[i].red;
1477 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1478 density_xy[x].direction[i].green;
1479 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1480 density_xy[x].direction[i].blue;
1481 if (image->colorspace == CMYKColorspace)
1482 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1483 density_xy[x].direction[i].black;
1484 if (image->alpha_trait != UndefinedPixelTrait)
1485 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1486 density_xy[x].direction[i].alpha;
1490 channel_features[RedPixelChannel].difference_entropy[i]-=
1491 density_xy[x].direction[i].red*
1492 MagickLog10(density_xy[x].direction[i].red);
1493 channel_features[GreenPixelChannel].difference_entropy[i]-=
1494 density_xy[x].direction[i].green*
1495 MagickLog10(density_xy[x].direction[i].green);
1496 channel_features[BluePixelChannel].difference_entropy[i]-=
1497 density_xy[x].direction[i].blue*
1498 MagickLog10(density_xy[x].direction[i].blue);
1499 if (image->colorspace == CMYKColorspace)
1500 channel_features[BlackPixelChannel].difference_entropy[i]-=
1501 density_xy[x].direction[i].black*
1502 MagickLog10(density_xy[x].direction[i].black);
1503 if (image->alpha_trait != UndefinedPixelTrait)
1504 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1505 density_xy[x].direction[i].alpha*
1506 MagickLog10(density_xy[x].direction[i].alpha);
1508 Information Measures of Correlation.
1510 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1511 MagickLog10(density_x[x].direction[i].red));
1512 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1513 MagickLog10(density_x[x].direction[i].green));
1514 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1515 MagickLog10(density_x[x].direction[i].blue));
1516 if (image->colorspace == CMYKColorspace)
1517 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1518 MagickLog10(density_x[x].direction[i].black));
1519 if (image->alpha_trait != UndefinedPixelTrait)
1520 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1521 MagickLog10(density_x[x].direction[i].alpha));
1522 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1523 MagickLog10(density_y[x].direction[i].red));
1524 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1525 MagickLog10(density_y[x].direction[i].green));
1526 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1527 MagickLog10(density_y[x].direction[i].blue));
1528 if (image->colorspace == CMYKColorspace)
1529 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1530 MagickLog10(density_y[x].direction[i].black));
1531 if (image->alpha_trait != UndefinedPixelTrait)
1532 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1533 MagickLog10(density_y[x].direction[i].alpha));
1536 Difference variance.
1538 channel_features[RedPixelChannel].difference_variance[i]=
1539 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1540 (variance.direction[i].red*variance.direction[i].red))/
1541 ((double) number_grays*number_grays*number_grays*number_grays);
1542 channel_features[GreenPixelChannel].difference_variance[i]=
1543 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1544 (variance.direction[i].green*variance.direction[i].green))/
1545 ((double) number_grays*number_grays*number_grays*number_grays);
1546 channel_features[BluePixelChannel].difference_variance[i]=
1547 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1548 (variance.direction[i].blue*variance.direction[i].blue))/
1549 ((double) number_grays*number_grays*number_grays*number_grays);
1550 if (image->colorspace == CMYKColorspace)
1551 channel_features[BlackPixelChannel].difference_variance[i]=
1552 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1553 (variance.direction[i].black*variance.direction[i].black))/
1554 ((double) number_grays*number_grays*number_grays*number_grays);
1555 if (image->alpha_trait != UndefinedPixelTrait)
1556 channel_features[AlphaPixelChannel].difference_variance[i]=
1557 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1558 (variance.direction[i].alpha*variance.direction[i].alpha))/
1559 ((double) number_grays*number_grays*number_grays*number_grays);
1561 Information Measures of Correlation.
1563 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1564 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1565 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1566 entropy_x.direction[i].red : entropy_y.direction[i].red);
1567 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1568 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1569 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1570 entropy_x.direction[i].green : entropy_y.direction[i].green);
1571 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1572 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1573 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1574 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1575 if (image->colorspace == CMYKColorspace)
1576 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1577 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1578 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1579 entropy_x.direction[i].black : entropy_y.direction[i].black);
1580 if (image->alpha_trait != UndefinedPixelTrait)
1581 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1582 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1583 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1584 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1585 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1586 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].red-
1587 entropy_xy.direction[i].red)))));
1588 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1589 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].green-
1590 entropy_xy.direction[i].green)))));
1591 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1592 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].blue-
1593 entropy_xy.direction[i].blue)))));
1594 if (image->colorspace == CMYKColorspace)
1595 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1596 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].black-
1597 entropy_xy.direction[i].black)))));
1598 if (image->alpha_trait != UndefinedPixelTrait)
1599 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1600 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].alpha-
1601 entropy_xy.direction[i].alpha)))));
1604 Compute more texture features.
1606 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1607 #pragma omp parallel for schedule(static,4) shared(status) \
1608 magick_threads(image,image,number_grays,1)
1610 for (i=0; i < 4; i++)
1615 for (z=0; z < (ssize_t) number_grays; z++)
1623 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1624 for (y=0; y < (ssize_t) number_grays; y++)
1629 for (x=0; x < (ssize_t) number_grays; x++)
1632 Contrast: amount of local variations present in an image.
1634 if (((y-x) == z) || ((x-y) == z))
1636 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1637 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1638 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1639 if (image->colorspace == CMYKColorspace)
1640 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1641 if (image->alpha_trait != UndefinedPixelTrait)
1642 pixel.direction[i].alpha+=
1643 cooccurrence[x][y].direction[i].alpha;
1646 Maximum Correlation Coefficient.
1648 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1649 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1650 density_y[x].direction[i].red;
1651 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1652 cooccurrence[y][x].direction[i].green/
1653 density_x[z].direction[i].green/density_y[x].direction[i].red;
1654 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1655 cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/
1656 density_y[x].direction[i].blue;
1657 if (image->colorspace == CMYKColorspace)
1658 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1659 cooccurrence[y][x].direction[i].black/
1660 density_x[z].direction[i].black/density_y[x].direction[i].black;
1661 if (image->alpha_trait != UndefinedPixelTrait)
1662 Q[z][y].direction[i].alpha+=
1663 cooccurrence[z][x].direction[i].alpha*
1664 cooccurrence[y][x].direction[i].alpha/
1665 density_x[z].direction[i].alpha/
1666 density_y[x].direction[i].alpha;
1669 channel_features[RedPixelChannel].contrast[i]+=z*z*
1670 pixel.direction[i].red;
1671 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1672 pixel.direction[i].green;
1673 channel_features[BluePixelChannel].contrast[i]+=z*z*
1674 pixel.direction[i].blue;
1675 if (image->colorspace == CMYKColorspace)
1676 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1677 pixel.direction[i].black;
1678 if (image->alpha_trait != UndefinedPixelTrait)
1679 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1680 pixel.direction[i].alpha;
1683 Maximum Correlation Coefficient.
1684 Future: return second largest eigenvalue of Q.
1686 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1687 sqrt((double) -1.0);
1688 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1689 sqrt((double) -1.0);
1690 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1691 sqrt((double) -1.0);
1692 if (image->colorspace == CMYKColorspace)
1693 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1694 sqrt((double) -1.0);
1695 if (image->alpha_trait != UndefinedPixelTrait)
1696 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1697 sqrt((double) -1.0);
1700 Relinquish resources.
1702 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
1703 for (i=0; i < (ssize_t) number_grays; i++)
1704 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1705 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1706 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1707 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1708 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
1709 for (i=0; i < (ssize_t) number_grays; i++)
1710 cooccurrence[i]=(ChannelStatistics *)
1711 RelinquishMagickMemory(cooccurrence[i]);
1712 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1713 return(channel_features);
1717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1721 % H o u g h L i n e I m a g e %
1725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1727 % Use HoughLineImage() in conjunction with any binary edge extracted image (we
1728 % recommand Canny) to identify lines in the image. The algorithm accumulates
1729 % counts for every white pixel for every possible orientation (for angles from
1730 % 0 to 179 in 1 degree increments) and distance from the center of the image to
1731 % the corner (in 1 px increments) and stores the counts in an accumulator matrix
1732 % of angle vs distance. The size of the accumulator is 180x(diagonal/2). Next
1733 % it searches this space for peaks in counts and converts the locations of the
1734 % peaks to slope and intercept in the normal x,y input image space. Use the
1735 % slope/intercepts to find the endpoints clipped to the bounds of the image. The
1736 % lines are then drawn. The counts are a measure of the length of the lines
1738 % The format of the HoughLineImage method is:
1740 % Image *HoughLineImage(const Image *image,const size_t width,
1741 % const size_t height,const size_t threshold,ExceptionInfo *exception)
1743 % A description of each parameter follows:
1745 % o image: the image.
1747 % o width, height: find line pairs as local maxima in this neighborhood.
1749 % o threshold: the line count threshold.
1751 % o exception: return any errors or warnings in this structure.
1755 static inline double MagickRound(double x)
1758 Round the fraction to nearest integer.
1760 if ((x-floor(x)) < (ceil(x)-x))
1765 MagickExport Image *HoughLineImage(const Image *image,const size_t width,
1766 const size_t height,const size_t threshold,ExceptionInfo *exception)
1768 #define HoughLineImageTag "HoughLine/Image"
1774 message[MaxTextExtent],
1775 path[MaxTextExtent];
1784 *lines_image = NULL;
1813 Create the accumulator.
1815 assert(image != (const Image *) NULL);
1816 assert(image->signature == MagickSignature);
1817 if (image->debug != MagickFalse)
1818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1819 assert(exception != (ExceptionInfo *) NULL);
1820 assert(exception->signature == MagickSignature);
1821 accumulator_width=180;
1822 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1823 image->rows : image->columns))/2.0);
1824 accumulator_height=(size_t) (2.0*hough_height);
1825 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1826 sizeof(double),exception);
1827 if (accumulator == (MatrixInfo *) NULL)
1828 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1829 if (NullMatrix(accumulator) == MagickFalse)
1831 accumulator=DestroyMatrixInfo(accumulator);
1832 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1835 Populate the accumulator.
1839 center.x=(double) image->columns/2.0;
1840 center.y=(double) image->rows/2.0;
1841 image_view=AcquireVirtualCacheView(image,exception);
1842 for (y=0; y < (ssize_t) image->rows; y++)
1844 register const Quantum
1850 if (status == MagickFalse)
1852 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1853 if (p == (Quantum *) NULL)
1858 for (x=0; x < (ssize_t) image->columns; x++)
1860 if (GetPixelIntensity(image,p) > (QuantumRange/2.0))
1865 for (i=0; i < 180; i++)
1871 radius=(((double) x-center.x)*cos(DegreesToRadians((double) i)))+
1872 (((double) y-center.y)*sin(DegreesToRadians((double) i)));
1873 (void) GetMatrixElement(accumulator,i,(ssize_t)
1874 MagickRound(radius+hough_height),&count);
1876 (void) SetMatrixElement(accumulator,i,(ssize_t)
1877 MagickRound(radius+hough_height),&count);
1880 p+=GetPixelChannels(image);
1882 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1887 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1888 #pragma omp critical (MagickCore_CannyEdgeImage)
1890 proceed=SetImageProgress(image,CannyEdgeImageTag,progress++,
1892 if (proceed == MagickFalse)
1896 image_view=DestroyCacheView(image_view);
1897 if (status == MagickFalse)
1899 accumulator=DestroyMatrixInfo(accumulator);
1900 return((Image *) NULL);
1903 Generate line segments from accumulator.
1905 file=AcquireUniqueFileResource(path);
1908 accumulator=DestroyMatrixInfo(accumulator);
1909 return((Image *) NULL);
1911 (void) FormatLocaleString(message,MaxTextExtent,
1912 "# Hough line transform: %.20gx%.20g%+.20g\n",(double) width,
1913 (double) height,(double) threshold);
1914 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1916 (void) FormatLocaleString(message,MaxTextExtent,"viewbox 0 0 %.20g %.20g\n",
1917 (double) image->columns,(double) image->rows);
1918 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1920 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1922 line_count=threshold;
1923 for (y=0; y < (ssize_t) accumulator_height; y++)
1928 for (x=0; x < (ssize_t) accumulator_width; x++)
1933 (void) GetMatrixElement(accumulator,x,y,&count);
1934 if (count >= (double) line_count)
1946 Is point a local maxima?
1949 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
1954 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
1956 if ((u != 0) || (v !=0))
1958 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
1966 if (u < (ssize_t) (width/2))
1969 (void) GetMatrixElement(accumulator,x,y,&count);
1972 if ((x >= 45) && (x <= 135))
1975 y = (r-x cos(t))/sin(t)
1978 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
1979 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1980 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1981 line.x2=(double) image->columns;
1982 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
1983 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
1984 sin(DegreesToRadians((double) x))+(image->rows/2.0);
1989 x = (r-y cos(t))/sin(t)
1992 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
1993 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1994 cos(DegreesToRadians((double) x))+(image->columns/2.0);
1995 line.y2=(double) image->rows;
1996 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
1997 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
1998 cos(DegreesToRadians((double) x))+(image->columns/2.0);
2000 (void) FormatLocaleString(message,MaxTextExtent,
2001 "line %g,%g %g,%g # %g\n",line.x1,line.y1,line.x2,line.y2,maxima);
2002 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2009 Render lines to image canvas.
2011 image_info=AcquireImageInfo();
2012 image_info->background_color=image->background_color;
2013 (void) FormatLocaleString(image_info->filename,MaxTextExtent,"mvg:%s",path);
2014 artifact=GetImageArtifact(image,"background");
2015 if (artifact != (const char *) NULL)
2016 (void) SetImageOption(image_info,"background",artifact);
2017 artifact=GetImageArtifact(image,"fill");
2018 if (artifact != (const char *) NULL)
2019 (void) SetImageOption(image_info,"fill",artifact);
2020 artifact=GetImageArtifact(image,"stroke");
2021 if (artifact != (const char *) NULL)
2022 (void) SetImageOption(image_info,"stroke",artifact);
2023 artifact=GetImageArtifact(image,"strokewidth");
2024 if (artifact != (const char *) NULL)
2025 (void) SetImageOption(image_info,"strokewidth",artifact);
2026 lines_image=ReadImage(image_info,exception);
2027 artifact=GetImageArtifact(image,"hough-lines:accumulator");
2028 if ((lines_image != (Image *) NULL) &&
2029 (IsStringTrue(artifact) != MagickFalse))
2034 accumulator_image=MatrixToImage(accumulator,exception);
2035 if (accumulator_image != (Image *) NULL)
2036 AppendImageToList(&lines_image,accumulator_image);
2041 accumulator=DestroyMatrixInfo(accumulator);
2042 image_info=DestroyImageInfo(image_info);
2043 (void) RelinquishUniqueFileResource(path);
2044 return(GetFirstImageInList(lines_image));
2048 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2052 % M e a n S h i f t I m a g e %
2056 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2058 % MeanShiftImage() delineate arbitrarily shaped clusters in the image. For
2059 % each pixel, it visits all the pixels in the neighborhood specified by
2060 % the window centered at the pixel and excludes those that are outside the
2061 % radius=(window-1)/2 surrounding the pixel. From those pixels, it finds those
2062 % that are within the specified color distance from the current mean, and
2063 % computes a new x,y centroid from those coordinates and a new mean. This new
2064 % x,y centroid is used as the center for a new window. This process iterates
2065 % until it converges and the final mean is replaces the (original window
2066 % center) pixel value. It repeats this process for the next pixel, etc.,
2067 % until it processes all pixels in the image. Results are typically better with
2068 % colorspaces other than sRGB. We recommend YIQ, YUV or YCbCr.
2070 % The format of the MeanShiftImage method is:
2072 % Image *MeanShiftImage(const Image *image,const size_t width,
2073 % const size_t height,const double color_distance,
2074 % ExceptionInfo *exception)
2076 % A description of each parameter follows:
2078 % o image: the image.
2080 % o width, height: find pixels in this neighborhood.
2082 % o color_distance: the color distance.
2084 % o exception: return any errors or warnings in this structure.
2087 MagickExport Image *MeanShiftImage(const Image *image,const size_t width,
2088 const size_t height,const double color_distance,ExceptionInfo *exception)
2090 #define MaxMeanShiftIterations 100
2091 #define MeanShiftImageTag "MeanShift/Image"
2110 assert(image != (const Image *) NULL);
2111 assert(image->signature == MagickSignature);
2112 if (image->debug != MagickFalse)
2113 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2114 assert(exception != (ExceptionInfo *) NULL);
2115 assert(exception->signature == MagickSignature);
2116 mean_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2117 if (mean_image == (Image *) NULL)
2118 return((Image *) NULL);
2119 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2121 mean_image=DestroyImage(mean_image);
2122 return((Image *) NULL);
2126 image_view=AcquireVirtualCacheView(image,exception);
2127 pixel_view=AcquireVirtualCacheView(image,exception);
2128 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2129 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2130 #pragma omp parallel for schedule(static,4) shared(status,progress) \
2131 magick_threads(mean_image,mean_image,mean_image->rows,1)
2133 for (y=0; y < (ssize_t) mean_image->rows; y++)
2135 register const Quantum
2144 if (status == MagickFalse)
2146 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2147 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2149 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2154 for (x=0; x < (ssize_t) mean_image->columns; x++)
2167 GetPixelInfo(image,&mean_pixel);
2168 GetPixelInfoPixel(image,p,&mean_pixel);
2169 mean_location.x=(double) x;
2170 mean_location.y=(double) y;
2171 for (i=0; i < MaxMeanShiftIterations; i++)
2189 GetPixelInfo(image,&sum_pixel);
2190 previous_location=mean_location;
2191 previous_pixel=mean_pixel;
2193 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2198 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2200 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2205 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2206 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2207 mean_location.y+v),&pixel,exception);
2208 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2209 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2210 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2211 if (distance <= (color_distance*color_distance))
2213 sum_location.x+=mean_location.x+u;
2214 sum_location.y+=mean_location.y+v;
2215 sum_pixel.red+=pixel.red;
2216 sum_pixel.green+=pixel.green;
2217 sum_pixel.blue+=pixel.blue;
2218 sum_pixel.alpha+=pixel.alpha;
2225 mean_location.x=gamma*sum_location.x;
2226 mean_location.y=gamma*sum_location.y;
2227 mean_pixel.red=gamma*sum_pixel.red;
2228 mean_pixel.green=gamma*sum_pixel.green;
2229 mean_pixel.blue=gamma*sum_pixel.blue;
2230 mean_pixel.alpha=gamma*sum_pixel.alpha;
2231 distance=(mean_location.x-previous_location.x)*
2232 (mean_location.x-previous_location.x)+
2233 (mean_location.y-previous_location.y)*
2234 (mean_location.y-previous_location.y)+
2235 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2236 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2237 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2238 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2239 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2240 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2241 if (distance <= 3.0)
2244 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2245 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2246 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2247 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2248 p+=GetPixelChannels(image);
2249 q+=GetPixelChannels(mean_image);
2251 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2253 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2258 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2259 #pragma omp critical (MagickCore_MeanShiftImage)
2261 proceed=SetImageProgress(image,MeanShiftImageTag,progress++,
2263 if (proceed == MagickFalse)
2267 mean_view=DestroyCacheView(mean_view);
2268 pixel_view=DestroyCacheView(pixel_view);
2269 image_view=DestroyCacheView(image_view);