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-2017 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 % https://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/channel.h"
52 #include "MagickCore/client.h"
53 #include "MagickCore/color.h"
54 #include "MagickCore/color-private.h"
55 #include "MagickCore/colorspace.h"
56 #include "MagickCore/colorspace-private.h"
57 #include "MagickCore/composite.h"
58 #include "MagickCore/composite-private.h"
59 #include "MagickCore/compress.h"
60 #include "MagickCore/constitute.h"
61 #include "MagickCore/display.h"
62 #include "MagickCore/draw.h"
63 #include "MagickCore/enhance.h"
64 #include "MagickCore/exception.h"
65 #include "MagickCore/exception-private.h"
66 #include "MagickCore/feature.h"
67 #include "MagickCore/gem.h"
68 #include "MagickCore/geometry.h"
69 #include "MagickCore/list.h"
70 #include "MagickCore/image-private.h"
71 #include "MagickCore/magic.h"
72 #include "MagickCore/magick.h"
73 #include "MagickCore/matrix.h"
74 #include "MagickCore/memory_.h"
75 #include "MagickCore/module.h"
76 #include "MagickCore/monitor.h"
77 #include "MagickCore/monitor-private.h"
78 #include "MagickCore/morphology-private.h"
79 #include "MagickCore/option.h"
80 #include "MagickCore/paint.h"
81 #include "MagickCore/pixel-accessor.h"
82 #include "MagickCore/profile.h"
83 #include "MagickCore/property.h"
84 #include "MagickCore/quantize.h"
85 #include "MagickCore/quantum-private.h"
86 #include "MagickCore/random_.h"
87 #include "MagickCore/resource_.h"
88 #include "MagickCore/segment.h"
89 #include "MagickCore/semaphore.h"
90 #include "MagickCore/signature-private.h"
91 #include "MagickCore/string_.h"
92 #include "MagickCore/thread-private.h"
93 #include "MagickCore/timer.h"
94 #include "MagickCore/utility.h"
95 #include "MagickCore/version.h"
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % C a n n y E d g e I m a g e %
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 % CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of
111 % The format of the CannyEdgeImage method is:
113 % Image *CannyEdgeImage(const Image *image,const double radius,
114 % const double sigma,const double lower_percent,
115 % const double upper_percent,ExceptionInfo *exception)
117 % A description of each parameter follows:
119 % o image: the image.
121 % o radius: the radius of the gaussian smoothing filter.
123 % o sigma: the sigma of the gaussian smoothing filter.
125 % o lower_percent: percentage of edge pixels in the lower threshold.
127 % o upper_percent: percentage of edge pixels in the upper threshold.
129 % o exception: return any errors or warnings in this structure.
133 typedef struct _CannyInfo
147 static inline MagickBooleanType IsAuthenticPixel(const Image *image,
148 const ssize_t x,const ssize_t y)
150 if ((x < 0) || (x >= (ssize_t) image->columns))
152 if ((y < 0) || (y >= (ssize_t) image->rows))
157 static MagickBooleanType TraceEdges(Image *edge_image,CacheView *edge_view,
158 MatrixInfo *canny_cache,const ssize_t x,const ssize_t y,
159 const double lower_threshold,ExceptionInfo *exception)
174 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
175 if (q == (Quantum *) NULL)
178 status=SyncCacheViewAuthenticPixels(edge_view,exception);
179 if (status == MagickFalse)
181 if (GetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
185 if (SetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
193 status=GetMatrixElement(canny_cache,i,0,&edge);
194 if (status == MagickFalse)
196 for (v=(-1); v <= 1; v++)
201 for (u=(-1); u <= 1; u++)
203 if ((u == 0) && (v == 0))
205 if (IsAuthenticPixel(edge_image,edge.x+u,edge.y+v) == MagickFalse)
208 Not an edge if gradient value is below the lower threshold.
210 q=GetCacheViewAuthenticPixels(edge_view,edge.x+u,edge.y+v,1,1,
212 if (q == (Quantum *) NULL)
214 status=GetMatrixElement(canny_cache,edge.x+u,edge.y+v,&pixel);
215 if (status == MagickFalse)
217 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
218 (pixel.intensity >= lower_threshold))
221 status=SyncCacheViewAuthenticPixels(edge_view,exception);
222 if (status == MagickFalse)
226 status=SetMatrixElement(canny_cache,i,0,&edge);
227 if (status == MagickFalse)
237 MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
238 const double sigma,const double lower_percent,const double upper_percent,
239 ExceptionInfo *exception)
241 #define CannyEdgeImageTag "CannyEdge/Image"
250 geometry[MagickPathExtent];
276 assert(image != (const Image *) NULL);
277 assert(image->signature == MagickCoreSignature);
278 if (image->debug != MagickFalse)
279 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
280 assert(exception != (ExceptionInfo *) NULL);
281 assert(exception->signature == MagickCoreSignature);
285 (void) FormatLocaleString(geometry,MagickPathExtent,
286 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
287 kernel_info=AcquireKernelInfo(geometry,exception);
288 if (kernel_info == (KernelInfo *) NULL)
289 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
290 edge_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
291 kernel_info=DestroyKernelInfo(kernel_info);
292 if (edge_image == (Image *) NULL)
293 return((Image *) NULL);
294 if (TransformImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
296 edge_image=DestroyImage(edge_image);
297 return((Image *) NULL);
299 (void) SetImageAlphaChannel(edge_image,OffAlphaChannel,exception);
301 Find the intensity gradient of the image.
303 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
304 sizeof(CannyInfo),exception);
305 if (canny_cache == (MatrixInfo *) NULL)
307 edge_image=DestroyImage(edge_image);
308 return((Image *) NULL);
311 edge_view=AcquireVirtualCacheView(edge_image,exception);
312 #if defined(MAGICKCORE_OPENMP_SUPPORT)
313 #pragma omp parallel for schedule(static,4) shared(status) \
314 magick_threads(edge_image,edge_image,edge_image->rows,1)
316 for (y=0; y < (ssize_t) edge_image->rows; y++)
318 register const Quantum
324 if (status == MagickFalse)
326 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
328 if (p == (const Quantum *) NULL)
333 for (x=0; x < (ssize_t) edge_image->columns; x++)
342 register const Quantum
343 *magick_restrict kernel_pixels;
360 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
364 for (v=0; v < 2; v++)
369 for (u=0; u < 2; u++)
374 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
375 dx+=0.5*Gx[v][u]*intensity;
376 dy+=0.5*Gy[v][u]*intensity;
378 kernel_pixels+=edge_image->columns+1;
380 pixel.magnitude=hypot(dx,dy);
382 if (fabs(dx) > MagickEpsilon)
390 if (slope < -2.41421356237)
393 if (slope < -0.414213562373)
400 if (slope > 2.41421356237)
403 if (slope > 0.414213562373)
409 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
411 p+=GetPixelChannels(edge_image);
414 edge_view=DestroyCacheView(edge_view);
416 Non-maxima suppression, remove pixels that are not considered to be part
420 (void) GetMatrixElement(canny_cache,0,0,&element);
421 max=element.intensity;
422 min=element.intensity;
423 edge_view=AcquireAuthenticCacheView(edge_image,exception);
424 #if defined(MAGICKCORE_OPENMP_SUPPORT)
425 #pragma omp parallel for schedule(static,4) shared(status) \
426 magick_threads(edge_image,edge_image,edge_image->rows,1)
428 for (y=0; y < (ssize_t) edge_image->rows; y++)
436 if (status == MagickFalse)
438 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
440 if (q == (Quantum *) NULL)
445 for (x=0; x < (ssize_t) edge_image->columns; x++)
452 (void) GetMatrixElement(canny_cache,x,y,&pixel);
453 switch (pixel.orientation)
459 0 degrees, north and south.
461 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
462 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
468 45 degrees, northwest and southeast.
470 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
471 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
477 90 degrees, east and west.
479 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
480 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
486 135 degrees, northeast and southwest.
488 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
489 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
493 pixel.intensity=pixel.magnitude;
494 if ((pixel.magnitude < alpha_pixel.magnitude) ||
495 (pixel.magnitude < beta_pixel.magnitude))
497 (void) SetMatrixElement(canny_cache,x,y,&pixel);
498 #if defined(MAGICKCORE_OPENMP_SUPPORT)
499 #pragma omp critical (MagickCore_CannyEdgeImage)
502 if (pixel.intensity < min)
504 if (pixel.intensity > max)
508 q+=GetPixelChannels(edge_image);
510 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
513 edge_view=DestroyCacheView(edge_view);
515 Estimate hysteresis threshold.
517 lower_threshold=lower_percent*(max-min)+min;
518 upper_threshold=upper_percent*(max-min)+min;
520 Hysteresis threshold.
522 edge_view=AcquireAuthenticCacheView(edge_image,exception);
523 for (y=0; y < (ssize_t) edge_image->rows; y++)
528 if (status == MagickFalse)
530 for (x=0; x < (ssize_t) edge_image->columns; x++)
535 register const Quantum
539 Edge if pixel gradient higher than upper threshold.
541 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
542 if (p == (const Quantum *) NULL)
544 status=GetMatrixElement(canny_cache,x,y,&pixel);
545 if (status == MagickFalse)
547 if ((GetPixelIntensity(edge_image,p) == 0.0) &&
548 (pixel.intensity >= upper_threshold))
549 status=TraceEdges(edge_image,edge_view,canny_cache,x,y,lower_threshold,
552 if (image->progress_monitor != (MagickProgressMonitor) NULL)
557 #if defined(MAGICKCORE_OPENMP_SUPPORT)
558 #pragma omp critical (MagickCore_CannyEdgeImage)
560 proceed=SetImageProgress(image,CannyEdgeImageTag,progress++,
562 if (proceed == MagickFalse)
566 edge_view=DestroyCacheView(edge_view);
570 canny_cache=DestroyMatrixInfo(canny_cache);
575 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579 % G e t I m a g e F e a t u r e s %
583 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
585 % GetImageFeatures() returns features for each channel in the image in
586 % each of four directions (horizontal, vertical, left and right diagonals)
587 % for the specified distance. The features include the angular second
588 % moment, contrast, correlation, sum of squares: variance, inverse difference
589 % moment, sum average, sum varience, sum entropy, entropy, difference variance,% difference entropy, information measures of correlation 1, information
590 % measures of correlation 2, and maximum correlation coefficient. You can
591 % access the red channel contrast, for example, like this:
593 % channel_features=GetImageFeatures(image,1,exception);
594 % contrast=channel_features[RedPixelChannel].contrast[0];
596 % Use MagickRelinquishMemory() to free the features buffer.
598 % The format of the GetImageFeatures method is:
600 % ChannelFeatures *GetImageFeatures(const Image *image,
601 % const size_t distance,ExceptionInfo *exception)
603 % A description of each parameter follows:
605 % o image: the image.
607 % o distance: the distance.
609 % o exception: return any errors or warnings in this structure.
613 static inline double MagickLog10(const double x)
615 #define Log10Epsilon (1.0e-11)
617 if (fabs(x) < Log10Epsilon)
618 return(log10(Log10Epsilon));
619 return(log10(fabs(x)));
622 MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
623 const size_t distance,ExceptionInfo *exception)
625 typedef struct _ChannelStatistics
628 direction[4]; /* horizontal, vertical, left and right diagonals */
671 assert(image != (Image *) NULL);
672 assert(image->signature == MagickCoreSignature);
673 if (image->debug != MagickFalse)
674 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
675 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
676 return((ChannelFeatures *) NULL);
677 length=MaxPixelChannels+1UL;
678 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
679 sizeof(*channel_features));
680 if (channel_features == (ChannelFeatures *) NULL)
681 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
682 (void) ResetMagickMemory(channel_features,0,length*
683 sizeof(*channel_features));
687 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
688 if (grays == (PixelPacket *) NULL)
690 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
692 (void) ThrowMagickException(exception,GetMagickModule(),
693 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
694 return(channel_features);
696 for (i=0; i <= (ssize_t) MaxMap; i++)
699 grays[i].green=(~0U);
701 grays[i].alpha=(~0U);
702 grays[i].black=(~0U);
705 image_view=AcquireVirtualCacheView(image,exception);
706 #if defined(MAGICKCORE_OPENMP_SUPPORT)
707 #pragma omp parallel for schedule(static,4) shared(status) \
708 magick_threads(image,image,image->rows,1)
710 for (r=0; r < (ssize_t) image->rows; r++)
712 register const Quantum
718 if (status == MagickFalse)
720 p=GetCacheViewVirtualPixels(image_view,0,r,image->columns,1,exception);
721 if (p == (const Quantum *) NULL)
726 for (x=0; x < (ssize_t) image->columns; x++)
728 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
729 ScaleQuantumToMap(GetPixelRed(image,p));
730 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
731 ScaleQuantumToMap(GetPixelGreen(image,p));
732 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
733 ScaleQuantumToMap(GetPixelBlue(image,p));
734 if (image->colorspace == CMYKColorspace)
735 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
736 ScaleQuantumToMap(GetPixelBlack(image,p));
737 if (image->alpha_trait != UndefinedPixelTrait)
738 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
739 ScaleQuantumToMap(GetPixelAlpha(image,p));
740 p+=GetPixelChannels(image);
743 image_view=DestroyCacheView(image_view);
744 if (status == MagickFalse)
746 grays=(PixelPacket *) RelinquishMagickMemory(grays);
747 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
749 return(channel_features);
751 (void) ResetMagickMemory(&gray,0,sizeof(gray));
752 for (i=0; i <= (ssize_t) MaxMap; i++)
754 if (grays[i].red != ~0U)
755 grays[gray.red++].red=grays[i].red;
756 if (grays[i].green != ~0U)
757 grays[gray.green++].green=grays[i].green;
758 if (grays[i].blue != ~0U)
759 grays[gray.blue++].blue=grays[i].blue;
760 if (image->colorspace == CMYKColorspace)
761 if (grays[i].black != ~0U)
762 grays[gray.black++].black=grays[i].black;
763 if (image->alpha_trait != UndefinedPixelTrait)
764 if (grays[i].alpha != ~0U)
765 grays[gray.alpha++].alpha=grays[i].alpha;
768 Allocate spatial dependence matrix.
770 number_grays=gray.red;
771 if (gray.green > number_grays)
772 number_grays=gray.green;
773 if (gray.blue > number_grays)
774 number_grays=gray.blue;
775 if (image->colorspace == CMYKColorspace)
776 if (gray.black > number_grays)
777 number_grays=gray.black;
778 if (image->alpha_trait != UndefinedPixelTrait)
779 if (gray.alpha > number_grays)
780 number_grays=gray.alpha;
781 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
782 sizeof(*cooccurrence));
783 density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
785 density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
786 sizeof(*density_xy));
787 density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
789 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
790 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
791 if ((cooccurrence == (ChannelStatistics **) NULL) ||
792 (density_x == (ChannelStatistics *) NULL) ||
793 (density_xy == (ChannelStatistics *) NULL) ||
794 (density_y == (ChannelStatistics *) NULL) ||
795 (Q == (ChannelStatistics **) NULL) ||
796 (sum == (ChannelStatistics *) NULL))
798 if (Q != (ChannelStatistics **) NULL)
800 for (i=0; i < (ssize_t) number_grays; i++)
801 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
802 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
804 if (sum != (ChannelStatistics *) NULL)
805 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
806 if (density_y != (ChannelStatistics *) NULL)
807 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
808 if (density_xy != (ChannelStatistics *) NULL)
809 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
810 if (density_x != (ChannelStatistics *) NULL)
811 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
812 if (cooccurrence != (ChannelStatistics **) NULL)
814 for (i=0; i < (ssize_t) number_grays; i++)
815 cooccurrence[i]=(ChannelStatistics *)
816 RelinquishMagickMemory(cooccurrence[i]);
817 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
820 grays=(PixelPacket *) RelinquishMagickMemory(grays);
821 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
823 (void) ThrowMagickException(exception,GetMagickModule(),
824 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
825 return(channel_features);
827 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
828 (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x));
829 (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
830 (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y));
831 (void) ResetMagickMemory(&mean,0,sizeof(mean));
832 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
833 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
834 (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy));
835 (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x));
836 (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy));
837 (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1));
838 (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2));
839 (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y));
840 (void) ResetMagickMemory(&variance,0,sizeof(variance));
841 for (i=0; i < (ssize_t) number_grays; i++)
843 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
844 sizeof(**cooccurrence));
845 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
846 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
847 (Q[i] == (ChannelStatistics *) NULL))
849 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
850 sizeof(**cooccurrence));
851 (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q));
853 if (i < (ssize_t) number_grays)
855 for (i--; i >= 0; i--)
857 if (Q[i] != (ChannelStatistics *) NULL)
858 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
859 if (cooccurrence[i] != (ChannelStatistics *) NULL)
860 cooccurrence[i]=(ChannelStatistics *)
861 RelinquishMagickMemory(cooccurrence[i]);
863 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
864 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
865 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
866 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
867 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
868 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
869 grays=(PixelPacket *) RelinquishMagickMemory(grays);
870 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
872 (void) ThrowMagickException(exception,GetMagickModule(),
873 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
874 return(channel_features);
877 Initialize spatial dependence matrix.
880 image_view=AcquireVirtualCacheView(image,exception);
881 for (r=0; r < (ssize_t) image->rows; r++)
883 register const Quantum
894 if (status == MagickFalse)
896 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,r,image->columns+
897 2*distance,distance+2,exception);
898 if (p == (const Quantum *) NULL)
903 p+=distance*GetPixelChannels(image);;
904 for (x=0; x < (ssize_t) image->columns; x++)
906 for (i=0; i < 4; i++)
914 Horizontal adjacency.
916 offset=(ssize_t) distance;
924 offset=(ssize_t) (image->columns+2*distance);
930 Right diagonal adjacency.
932 offset=(ssize_t) ((image->columns+2*distance)-distance);
938 Left diagonal adjacency.
940 offset=(ssize_t) ((image->columns+2*distance)+distance);
946 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
948 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
950 cooccurrence[u][v].direction[i].red++;
951 cooccurrence[v][u].direction[i].red++;
954 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
956 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
958 cooccurrence[u][v].direction[i].green++;
959 cooccurrence[v][u].direction[i].green++;
962 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
964 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
966 cooccurrence[u][v].direction[i].blue++;
967 cooccurrence[v][u].direction[i].blue++;
968 if (image->colorspace == CMYKColorspace)
972 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
974 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
976 cooccurrence[u][v].direction[i].black++;
977 cooccurrence[v][u].direction[i].black++;
979 if (image->alpha_trait != UndefinedPixelTrait)
983 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
985 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
987 cooccurrence[u][v].direction[i].alpha++;
988 cooccurrence[v][u].direction[i].alpha++;
991 p+=GetPixelChannels(image);
994 grays=(PixelPacket *) RelinquishMagickMemory(grays);
995 image_view=DestroyCacheView(image_view);
996 if (status == MagickFalse)
998 for (i=0; i < (ssize_t) number_grays; i++)
999 cooccurrence[i]=(ChannelStatistics *)
1000 RelinquishMagickMemory(cooccurrence[i]);
1001 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1002 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
1004 (void) ThrowMagickException(exception,GetMagickModule(),
1005 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
1006 return(channel_features);
1009 Normalize spatial dependence matrix.
1011 for (i=0; i < 4; i++)
1025 Horizontal adjacency.
1027 normalize=2.0*image->rows*(image->columns-distance);
1035 normalize=2.0*(image->rows-distance)*image->columns;
1041 Right diagonal adjacency.
1043 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1049 Left diagonal adjacency.
1051 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1055 normalize=PerceptibleReciprocal(normalize);
1056 for (y=0; y < (ssize_t) number_grays; y++)
1061 for (x=0; x < (ssize_t) number_grays; x++)
1063 cooccurrence[x][y].direction[i].red*=normalize;
1064 cooccurrence[x][y].direction[i].green*=normalize;
1065 cooccurrence[x][y].direction[i].blue*=normalize;
1066 if (image->colorspace == CMYKColorspace)
1067 cooccurrence[x][y].direction[i].black*=normalize;
1068 if (image->alpha_trait != UndefinedPixelTrait)
1069 cooccurrence[x][y].direction[i].alpha*=normalize;
1074 Compute texture features.
1076 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1077 #pragma omp parallel for schedule(static,4) shared(status) \
1078 magick_threads(image,image,number_grays,1)
1080 for (i=0; i < 4; i++)
1085 for (y=0; y < (ssize_t) number_grays; y++)
1090 for (x=0; x < (ssize_t) number_grays; x++)
1093 Angular second moment: measure of homogeneity of the image.
1095 channel_features[RedPixelChannel].angular_second_moment[i]+=
1096 cooccurrence[x][y].direction[i].red*
1097 cooccurrence[x][y].direction[i].red;
1098 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1099 cooccurrence[x][y].direction[i].green*
1100 cooccurrence[x][y].direction[i].green;
1101 channel_features[BluePixelChannel].angular_second_moment[i]+=
1102 cooccurrence[x][y].direction[i].blue*
1103 cooccurrence[x][y].direction[i].blue;
1104 if (image->colorspace == CMYKColorspace)
1105 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1106 cooccurrence[x][y].direction[i].black*
1107 cooccurrence[x][y].direction[i].black;
1108 if (image->alpha_trait != UndefinedPixelTrait)
1109 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1110 cooccurrence[x][y].direction[i].alpha*
1111 cooccurrence[x][y].direction[i].alpha;
1113 Correlation: measure of linear-dependencies in the image.
1115 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1116 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1117 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1118 if (image->colorspace == CMYKColorspace)
1119 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1120 if (image->alpha_trait != UndefinedPixelTrait)
1121 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1122 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1123 correlation.direction[i].green+=x*y*
1124 cooccurrence[x][y].direction[i].green;
1125 correlation.direction[i].blue+=x*y*
1126 cooccurrence[x][y].direction[i].blue;
1127 if (image->colorspace == CMYKColorspace)
1128 correlation.direction[i].black+=x*y*
1129 cooccurrence[x][y].direction[i].black;
1130 if (image->alpha_trait != UndefinedPixelTrait)
1131 correlation.direction[i].alpha+=x*y*
1132 cooccurrence[x][y].direction[i].alpha;
1134 Inverse Difference Moment.
1136 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1137 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1138 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1139 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1140 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1141 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1142 if (image->colorspace == CMYKColorspace)
1143 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1144 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1145 if (image->alpha_trait != UndefinedPixelTrait)
1146 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1147 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1151 density_xy[y+x+2].direction[i].red+=
1152 cooccurrence[x][y].direction[i].red;
1153 density_xy[y+x+2].direction[i].green+=
1154 cooccurrence[x][y].direction[i].green;
1155 density_xy[y+x+2].direction[i].blue+=
1156 cooccurrence[x][y].direction[i].blue;
1157 if (image->colorspace == CMYKColorspace)
1158 density_xy[y+x+2].direction[i].black+=
1159 cooccurrence[x][y].direction[i].black;
1160 if (image->alpha_trait != UndefinedPixelTrait)
1161 density_xy[y+x+2].direction[i].alpha+=
1162 cooccurrence[x][y].direction[i].alpha;
1166 channel_features[RedPixelChannel].entropy[i]-=
1167 cooccurrence[x][y].direction[i].red*
1168 MagickLog10(cooccurrence[x][y].direction[i].red);
1169 channel_features[GreenPixelChannel].entropy[i]-=
1170 cooccurrence[x][y].direction[i].green*
1171 MagickLog10(cooccurrence[x][y].direction[i].green);
1172 channel_features[BluePixelChannel].entropy[i]-=
1173 cooccurrence[x][y].direction[i].blue*
1174 MagickLog10(cooccurrence[x][y].direction[i].blue);
1175 if (image->colorspace == CMYKColorspace)
1176 channel_features[BlackPixelChannel].entropy[i]-=
1177 cooccurrence[x][y].direction[i].black*
1178 MagickLog10(cooccurrence[x][y].direction[i].black);
1179 if (image->alpha_trait != UndefinedPixelTrait)
1180 channel_features[AlphaPixelChannel].entropy[i]-=
1181 cooccurrence[x][y].direction[i].alpha*
1182 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1184 Information Measures of Correlation.
1186 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1187 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1188 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1189 if (image->alpha_trait != UndefinedPixelTrait)
1190 density_x[x].direction[i].alpha+=
1191 cooccurrence[x][y].direction[i].alpha;
1192 if (image->colorspace == CMYKColorspace)
1193 density_x[x].direction[i].black+=
1194 cooccurrence[x][y].direction[i].black;
1195 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1196 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1197 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1198 if (image->colorspace == CMYKColorspace)
1199 density_y[y].direction[i].black+=
1200 cooccurrence[x][y].direction[i].black;
1201 if (image->alpha_trait != UndefinedPixelTrait)
1202 density_y[y].direction[i].alpha+=
1203 cooccurrence[x][y].direction[i].alpha;
1205 mean.direction[i].red+=y*sum[y].direction[i].red;
1206 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1207 mean.direction[i].green+=y*sum[y].direction[i].green;
1208 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1209 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1210 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1211 if (image->colorspace == CMYKColorspace)
1213 mean.direction[i].black+=y*sum[y].direction[i].black;
1214 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1216 if (image->alpha_trait != UndefinedPixelTrait)
1218 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1219 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1223 Correlation: measure of linear-dependencies in the image.
1225 channel_features[RedPixelChannel].correlation[i]=
1226 (correlation.direction[i].red-mean.direction[i].red*
1227 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1228 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1229 sum_squares.direction[i].red-(mean.direction[i].red*
1230 mean.direction[i].red)));
1231 channel_features[GreenPixelChannel].correlation[i]=
1232 (correlation.direction[i].green-mean.direction[i].green*
1233 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1234 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1235 sum_squares.direction[i].green-(mean.direction[i].green*
1236 mean.direction[i].green)));
1237 channel_features[BluePixelChannel].correlation[i]=
1238 (correlation.direction[i].blue-mean.direction[i].blue*
1239 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1240 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1241 sum_squares.direction[i].blue-(mean.direction[i].blue*
1242 mean.direction[i].blue)));
1243 if (image->colorspace == CMYKColorspace)
1244 channel_features[BlackPixelChannel].correlation[i]=
1245 (correlation.direction[i].black-mean.direction[i].black*
1246 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1247 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1248 sum_squares.direction[i].black-(mean.direction[i].black*
1249 mean.direction[i].black)));
1250 if (image->alpha_trait != UndefinedPixelTrait)
1251 channel_features[AlphaPixelChannel].correlation[i]=
1252 (correlation.direction[i].alpha-mean.direction[i].alpha*
1253 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1254 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1255 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1256 mean.direction[i].alpha)));
1259 Compute more texture features.
1261 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1262 #pragma omp parallel for schedule(static,4) shared(status) \
1263 magick_threads(image,image,number_grays,1)
1265 for (i=0; i < 4; i++)
1270 for (x=2; x < (ssize_t) (2*number_grays); x++)
1275 channel_features[RedPixelChannel].sum_average[i]+=
1276 x*density_xy[x].direction[i].red;
1277 channel_features[GreenPixelChannel].sum_average[i]+=
1278 x*density_xy[x].direction[i].green;
1279 channel_features[BluePixelChannel].sum_average[i]+=
1280 x*density_xy[x].direction[i].blue;
1281 if (image->colorspace == CMYKColorspace)
1282 channel_features[BlackPixelChannel].sum_average[i]+=
1283 x*density_xy[x].direction[i].black;
1284 if (image->alpha_trait != UndefinedPixelTrait)
1285 channel_features[AlphaPixelChannel].sum_average[i]+=
1286 x*density_xy[x].direction[i].alpha;
1290 channel_features[RedPixelChannel].sum_entropy[i]-=
1291 density_xy[x].direction[i].red*
1292 MagickLog10(density_xy[x].direction[i].red);
1293 channel_features[GreenPixelChannel].sum_entropy[i]-=
1294 density_xy[x].direction[i].green*
1295 MagickLog10(density_xy[x].direction[i].green);
1296 channel_features[BluePixelChannel].sum_entropy[i]-=
1297 density_xy[x].direction[i].blue*
1298 MagickLog10(density_xy[x].direction[i].blue);
1299 if (image->colorspace == CMYKColorspace)
1300 channel_features[BlackPixelChannel].sum_entropy[i]-=
1301 density_xy[x].direction[i].black*
1302 MagickLog10(density_xy[x].direction[i].black);
1303 if (image->alpha_trait != UndefinedPixelTrait)
1304 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1305 density_xy[x].direction[i].alpha*
1306 MagickLog10(density_xy[x].direction[i].alpha);
1310 channel_features[RedPixelChannel].sum_variance[i]+=
1311 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1312 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1313 density_xy[x].direction[i].red;
1314 channel_features[GreenPixelChannel].sum_variance[i]+=
1315 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1316 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1317 density_xy[x].direction[i].green;
1318 channel_features[BluePixelChannel].sum_variance[i]+=
1319 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1320 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1321 density_xy[x].direction[i].blue;
1322 if (image->colorspace == CMYKColorspace)
1323 channel_features[BlackPixelChannel].sum_variance[i]+=
1324 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1325 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1326 density_xy[x].direction[i].black;
1327 if (image->alpha_trait != UndefinedPixelTrait)
1328 channel_features[AlphaPixelChannel].sum_variance[i]+=
1329 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1330 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1331 density_xy[x].direction[i].alpha;
1335 Compute more texture features.
1337 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1338 #pragma omp parallel for schedule(static,4) shared(status) \
1339 magick_threads(image,image,number_grays,1)
1341 for (i=0; i < 4; i++)
1346 for (y=0; y < (ssize_t) number_grays; y++)
1351 for (x=0; x < (ssize_t) number_grays; x++)
1354 Sum of Squares: Variance
1356 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1357 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1358 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1359 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1360 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1361 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1362 if (image->colorspace == CMYKColorspace)
1363 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1364 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1365 if (image->alpha_trait != UndefinedPixelTrait)
1366 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1367 (y-mean.direction[i].alpha+1)*
1368 cooccurrence[x][y].direction[i].alpha;
1370 Sum average / Difference Variance.
1372 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1373 cooccurrence[x][y].direction[i].red;
1374 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1375 cooccurrence[x][y].direction[i].green;
1376 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1377 cooccurrence[x][y].direction[i].blue;
1378 if (image->colorspace == CMYKColorspace)
1379 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1380 cooccurrence[x][y].direction[i].black;
1381 if (image->alpha_trait != UndefinedPixelTrait)
1382 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1383 cooccurrence[x][y].direction[i].alpha;
1385 Information Measures of Correlation.
1387 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1388 MagickLog10(cooccurrence[x][y].direction[i].red);
1389 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1390 MagickLog10(cooccurrence[x][y].direction[i].green);
1391 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1392 MagickLog10(cooccurrence[x][y].direction[i].blue);
1393 if (image->colorspace == CMYKColorspace)
1394 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1395 MagickLog10(cooccurrence[x][y].direction[i].black);
1396 if (image->alpha_trait != UndefinedPixelTrait)
1397 entropy_xy.direction[i].alpha-=
1398 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1399 cooccurrence[x][y].direction[i].alpha);
1400 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1401 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1402 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1403 MagickLog10(density_x[x].direction[i].green*
1404 density_y[y].direction[i].green));
1405 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1406 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1407 if (image->colorspace == CMYKColorspace)
1408 entropy_xy1.direction[i].black-=(
1409 cooccurrence[x][y].direction[i].black*MagickLog10(
1410 density_x[x].direction[i].black*density_y[y].direction[i].black));
1411 if (image->alpha_trait != UndefinedPixelTrait)
1412 entropy_xy1.direction[i].alpha-=(
1413 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1414 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1415 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1416 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1417 density_y[y].direction[i].red));
1418 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1419 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1420 density_y[y].direction[i].green));
1421 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1422 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1423 density_y[y].direction[i].blue));
1424 if (image->colorspace == CMYKColorspace)
1425 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1426 density_y[y].direction[i].black*MagickLog10(
1427 density_x[x].direction[i].black*density_y[y].direction[i].black));
1428 if (image->alpha_trait != UndefinedPixelTrait)
1429 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1430 density_y[y].direction[i].alpha*MagickLog10(
1431 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1434 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1435 variance.direction[i].red;
1436 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1437 variance.direction[i].green;
1438 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1439 variance.direction[i].blue;
1440 if (image->colorspace == CMYKColorspace)
1441 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1442 variance.direction[i].black;
1443 if (image->alpha_trait != UndefinedPixelTrait)
1444 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1445 variance.direction[i].alpha;
1448 Compute more texture features.
1450 (void) ResetMagickMemory(&variance,0,sizeof(variance));
1451 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
1452 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1453 #pragma omp parallel for schedule(static,4) shared(status) \
1454 magick_threads(image,image,number_grays,1)
1456 for (i=0; i < 4; i++)
1461 for (x=0; x < (ssize_t) number_grays; x++)
1464 Difference variance.
1466 variance.direction[i].red+=density_xy[x].direction[i].red;
1467 variance.direction[i].green+=density_xy[x].direction[i].green;
1468 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1469 if (image->colorspace == CMYKColorspace)
1470 variance.direction[i].black+=density_xy[x].direction[i].black;
1471 if (image->alpha_trait != UndefinedPixelTrait)
1472 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1473 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1474 density_xy[x].direction[i].red;
1475 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1476 density_xy[x].direction[i].green;
1477 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1478 density_xy[x].direction[i].blue;
1479 if (image->colorspace == CMYKColorspace)
1480 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1481 density_xy[x].direction[i].black;
1482 if (image->alpha_trait != UndefinedPixelTrait)
1483 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1484 density_xy[x].direction[i].alpha;
1488 channel_features[RedPixelChannel].difference_entropy[i]-=
1489 density_xy[x].direction[i].red*
1490 MagickLog10(density_xy[x].direction[i].red);
1491 channel_features[GreenPixelChannel].difference_entropy[i]-=
1492 density_xy[x].direction[i].green*
1493 MagickLog10(density_xy[x].direction[i].green);
1494 channel_features[BluePixelChannel].difference_entropy[i]-=
1495 density_xy[x].direction[i].blue*
1496 MagickLog10(density_xy[x].direction[i].blue);
1497 if (image->colorspace == CMYKColorspace)
1498 channel_features[BlackPixelChannel].difference_entropy[i]-=
1499 density_xy[x].direction[i].black*
1500 MagickLog10(density_xy[x].direction[i].black);
1501 if (image->alpha_trait != UndefinedPixelTrait)
1502 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1503 density_xy[x].direction[i].alpha*
1504 MagickLog10(density_xy[x].direction[i].alpha);
1506 Information Measures of Correlation.
1508 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1509 MagickLog10(density_x[x].direction[i].red));
1510 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1511 MagickLog10(density_x[x].direction[i].green));
1512 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1513 MagickLog10(density_x[x].direction[i].blue));
1514 if (image->colorspace == CMYKColorspace)
1515 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1516 MagickLog10(density_x[x].direction[i].black));
1517 if (image->alpha_trait != UndefinedPixelTrait)
1518 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1519 MagickLog10(density_x[x].direction[i].alpha));
1520 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1521 MagickLog10(density_y[x].direction[i].red));
1522 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1523 MagickLog10(density_y[x].direction[i].green));
1524 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1525 MagickLog10(density_y[x].direction[i].blue));
1526 if (image->colorspace == CMYKColorspace)
1527 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1528 MagickLog10(density_y[x].direction[i].black));
1529 if (image->alpha_trait != UndefinedPixelTrait)
1530 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1531 MagickLog10(density_y[x].direction[i].alpha));
1534 Difference variance.
1536 channel_features[RedPixelChannel].difference_variance[i]=
1537 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1538 (variance.direction[i].red*variance.direction[i].red))/
1539 ((double) number_grays*number_grays*number_grays*number_grays);
1540 channel_features[GreenPixelChannel].difference_variance[i]=
1541 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1542 (variance.direction[i].green*variance.direction[i].green))/
1543 ((double) number_grays*number_grays*number_grays*number_grays);
1544 channel_features[BluePixelChannel].difference_variance[i]=
1545 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1546 (variance.direction[i].blue*variance.direction[i].blue))/
1547 ((double) number_grays*number_grays*number_grays*number_grays);
1548 if (image->colorspace == CMYKColorspace)
1549 channel_features[BlackPixelChannel].difference_variance[i]=
1550 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1551 (variance.direction[i].black*variance.direction[i].black))/
1552 ((double) number_grays*number_grays*number_grays*number_grays);
1553 if (image->alpha_trait != UndefinedPixelTrait)
1554 channel_features[AlphaPixelChannel].difference_variance[i]=
1555 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1556 (variance.direction[i].alpha*variance.direction[i].alpha))/
1557 ((double) number_grays*number_grays*number_grays*number_grays);
1559 Information Measures of Correlation.
1561 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1562 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1563 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1564 entropy_x.direction[i].red : entropy_y.direction[i].red);
1565 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1566 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1567 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1568 entropy_x.direction[i].green : entropy_y.direction[i].green);
1569 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1570 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1571 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1572 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1573 if (image->colorspace == CMYKColorspace)
1574 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1575 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1576 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1577 entropy_x.direction[i].black : entropy_y.direction[i].black);
1578 if (image->alpha_trait != UndefinedPixelTrait)
1579 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1580 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1581 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1582 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1583 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1584 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].red-
1585 entropy_xy.direction[i].red)))));
1586 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1587 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].green-
1588 entropy_xy.direction[i].green)))));
1589 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1590 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].blue-
1591 entropy_xy.direction[i].blue)))));
1592 if (image->colorspace == CMYKColorspace)
1593 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1594 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].black-
1595 entropy_xy.direction[i].black)))));
1596 if (image->alpha_trait != UndefinedPixelTrait)
1597 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1598 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].alpha-
1599 entropy_xy.direction[i].alpha)))));
1602 Compute more texture features.
1604 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1605 #pragma omp parallel for schedule(static,4) shared(status) \
1606 magick_threads(image,image,number_grays,1)
1608 for (i=0; i < 4; i++)
1613 for (z=0; z < (ssize_t) number_grays; z++)
1621 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
1622 for (y=0; y < (ssize_t) number_grays; y++)
1627 for (x=0; x < (ssize_t) number_grays; x++)
1630 Contrast: amount of local variations present in an image.
1632 if (((y-x) == z) || ((x-y) == z))
1634 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1635 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1636 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1637 if (image->colorspace == CMYKColorspace)
1638 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1639 if (image->alpha_trait != UndefinedPixelTrait)
1640 pixel.direction[i].alpha+=
1641 cooccurrence[x][y].direction[i].alpha;
1644 Maximum Correlation Coefficient.
1646 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1647 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1648 density_y[x].direction[i].red;
1649 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1650 cooccurrence[y][x].direction[i].green/
1651 density_x[z].direction[i].green/density_y[x].direction[i].red;
1652 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1653 cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/
1654 density_y[x].direction[i].blue;
1655 if (image->colorspace == CMYKColorspace)
1656 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1657 cooccurrence[y][x].direction[i].black/
1658 density_x[z].direction[i].black/density_y[x].direction[i].black;
1659 if (image->alpha_trait != UndefinedPixelTrait)
1660 Q[z][y].direction[i].alpha+=
1661 cooccurrence[z][x].direction[i].alpha*
1662 cooccurrence[y][x].direction[i].alpha/
1663 density_x[z].direction[i].alpha/
1664 density_y[x].direction[i].alpha;
1667 channel_features[RedPixelChannel].contrast[i]+=z*z*
1668 pixel.direction[i].red;
1669 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1670 pixel.direction[i].green;
1671 channel_features[BluePixelChannel].contrast[i]+=z*z*
1672 pixel.direction[i].blue;
1673 if (image->colorspace == CMYKColorspace)
1674 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1675 pixel.direction[i].black;
1676 if (image->alpha_trait != UndefinedPixelTrait)
1677 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1678 pixel.direction[i].alpha;
1681 Maximum Correlation Coefficient.
1682 Future: return second largest eigenvalue of Q.
1684 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1685 sqrt((double) -1.0);
1686 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1687 sqrt((double) -1.0);
1688 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1689 sqrt((double) -1.0);
1690 if (image->colorspace == CMYKColorspace)
1691 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1692 sqrt((double) -1.0);
1693 if (image->alpha_trait != UndefinedPixelTrait)
1694 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1695 sqrt((double) -1.0);
1698 Relinquish resources.
1700 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
1701 for (i=0; i < (ssize_t) number_grays; i++)
1702 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1703 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1704 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1705 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1706 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
1707 for (i=0; i < (ssize_t) number_grays; i++)
1708 cooccurrence[i]=(ChannelStatistics *)
1709 RelinquishMagickMemory(cooccurrence[i]);
1710 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1711 return(channel_features);
1715 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1719 % H o u g h L i n e I m a g e %
1723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1725 % Use HoughLineImage() in conjunction with any binary edge extracted image (we
1726 % recommand Canny) to identify lines in the image. The algorithm accumulates
1727 % counts for every white pixel for every possible orientation (for angles from
1728 % 0 to 179 in 1 degree increments) and distance from the center of the image to
1729 % the corner (in 1 px increments) and stores the counts in an accumulator matrix
1730 % of angle vs distance. The size of the accumulator is 180x(diagonal/2). Next
1731 % it searches this space for peaks in counts and converts the locations of the
1732 % peaks to slope and intercept in the normal x,y input image space. Use the
1733 % slope/intercepts to find the endpoints clipped to the bounds of the image. The
1734 % lines are then drawn. The counts are a measure of the length of the lines
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 static Image *RenderHoughLines(const ImageInfo *image_info,const size_t columns,
1764 const size_t rows,ExceptionInfo *exception)
1766 #define BoundingBox "viewbox"
1780 image=AcquireImage(image_info,exception);
1781 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1782 if (status == MagickFalse)
1784 image=DestroyImageList(image);
1785 return((Image *) NULL);
1787 image->columns=columns;
1789 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
1790 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
1792 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
1794 image->columns=(size_t) (draw_info->affine.sx*image->columns);
1795 image->rows=(size_t) (draw_info->affine.sy*image->rows);
1796 status=SetImageExtent(image,image->columns,image->rows,exception);
1797 if (status == MagickFalse)
1798 return(DestroyImageList(image));
1799 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1801 image=DestroyImageList(image);
1802 return((Image *) NULL);
1807 if (GetBlobStreamData(image) == (unsigned char *) NULL)
1808 draw_info->primitive=FileToString(image->filename,~0UL,exception);
1811 draw_info->primitive=(char *) AcquireMagickMemory((size_t)
1812 GetBlobSize(image)+1);
1813 if (draw_info->primitive != (char *) NULL)
1815 (void) CopyMagickMemory(draw_info->primitive,GetBlobStreamData(image),
1816 (size_t) GetBlobSize(image));
1817 draw_info->primitive[GetBlobSize(image)]='\0';
1820 (void) DrawImage(image,draw_info,exception);
1821 draw_info=DestroyDrawInfo(draw_info);
1822 (void) CloseBlob(image);
1823 return(GetFirstImageInList(image));
1826 MagickExport Image *HoughLineImage(const Image *image,const size_t width,
1827 const size_t height,const size_t threshold,ExceptionInfo *exception)
1829 #define HoughLineImageTag "HoughLine/Image"
1835 message[MagickPathExtent],
1836 path[MagickPathExtent];
1845 *lines_image = NULL;
1874 Create the accumulator.
1876 assert(image != (const Image *) NULL);
1877 assert(image->signature == MagickCoreSignature);
1878 if (image->debug != MagickFalse)
1879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1880 assert(exception != (ExceptionInfo *) NULL);
1881 assert(exception->signature == MagickCoreSignature);
1882 accumulator_width=180;
1883 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1884 image->rows : image->columns))/2.0);
1885 accumulator_height=(size_t) (2.0*hough_height);
1886 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1887 sizeof(double),exception);
1888 if (accumulator == (MatrixInfo *) NULL)
1889 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1890 if (NullMatrix(accumulator) == MagickFalse)
1892 accumulator=DestroyMatrixInfo(accumulator);
1893 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 Populate the accumulator.
1900 center.x=(double) image->columns/2.0;
1901 center.y=(double) image->rows/2.0;
1902 image_view=AcquireVirtualCacheView(image,exception);
1903 for (y=0; y < (ssize_t) image->rows; y++)
1905 register const Quantum
1911 if (status == MagickFalse)
1913 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1914 if (p == (Quantum *) NULL)
1919 for (x=0; x < (ssize_t) image->columns; x++)
1921 if (GetPixelIntensity(image,p) > (QuantumRange/2.0))
1926 for (i=0; i < 180; i++)
1932 radius=(((double) x-center.x)*cos(DegreesToRadians((double) i)))+
1933 (((double) y-center.y)*sin(DegreesToRadians((double) i)));
1934 (void) GetMatrixElement(accumulator,i,(ssize_t)
1935 MagickRound(radius+hough_height),&count);
1937 (void) SetMatrixElement(accumulator,i,(ssize_t)
1938 MagickRound(radius+hough_height),&count);
1941 p+=GetPixelChannels(image);
1943 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1948 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1949 #pragma omp critical (MagickCore_CannyEdgeImage)
1951 proceed=SetImageProgress(image,CannyEdgeImageTag,progress++,
1953 if (proceed == MagickFalse)
1957 image_view=DestroyCacheView(image_view);
1958 if (status == MagickFalse)
1960 accumulator=DestroyMatrixInfo(accumulator);
1961 return((Image *) NULL);
1964 Generate line segments from accumulator.
1966 file=AcquireUniqueFileResource(path);
1969 accumulator=DestroyMatrixInfo(accumulator);
1970 return((Image *) NULL);
1972 (void) FormatLocaleString(message,MagickPathExtent,
1973 "# Hough line transform: %.20gx%.20g%+.20g\n",(double) width,
1974 (double) height,(double) threshold);
1975 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1977 (void) FormatLocaleString(message,MagickPathExtent,
1978 "viewbox 0 0 %.20g %.20g\n",(double) image->columns,(double) image->rows);
1979 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1981 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1983 line_count=threshold;
1984 for (y=0; y < (ssize_t) accumulator_height; y++)
1989 for (x=0; x < (ssize_t) accumulator_width; x++)
1994 (void) GetMatrixElement(accumulator,x,y,&count);
1995 if (count >= (double) line_count)
2007 Is point a local maxima?
2010 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2015 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2017 if ((u != 0) || (v !=0))
2019 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
2027 if (u < (ssize_t) (width/2))
2030 (void) GetMatrixElement(accumulator,x,y,&count);
2033 if ((x >= 45) && (x <= 135))
2036 y = (r-x cos(t))/sin(t)
2039 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
2040 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
2041 sin(DegreesToRadians((double) x))+(image->rows/2.0);
2042 line.x2=(double) image->columns;
2043 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
2044 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
2045 sin(DegreesToRadians((double) x))+(image->rows/2.0);
2050 x = (r-y cos(t))/sin(t)
2053 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
2054 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
2055 cos(DegreesToRadians((double) x))+(image->columns/2.0);
2056 line.y2=(double) image->rows;
2057 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
2058 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
2059 cos(DegreesToRadians((double) x))+(image->columns/2.0);
2061 (void) FormatLocaleString(message,MagickPathExtent,
2062 "line %g,%g %g,%g # %g\n",line.x1,line.y1,line.x2,line.y2,maxima);
2063 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2070 Render lines to image canvas.
2072 image_info=AcquireImageInfo();
2073 image_info->background_color=image->background_color;
2074 (void) FormatLocaleString(image_info->filename,MagickPathExtent,"%s",path);
2075 artifact=GetImageArtifact(image,"background");
2076 if (artifact != (const char *) NULL)
2077 (void) SetImageOption(image_info,"background",artifact);
2078 artifact=GetImageArtifact(image,"fill");
2079 if (artifact != (const char *) NULL)
2080 (void) SetImageOption(image_info,"fill",artifact);
2081 artifact=GetImageArtifact(image,"stroke");
2082 if (artifact != (const char *) NULL)
2083 (void) SetImageOption(image_info,"stroke",artifact);
2084 artifact=GetImageArtifact(image,"strokewidth");
2085 if (artifact != (const char *) NULL)
2086 (void) SetImageOption(image_info,"strokewidth",artifact);
2087 lines_image=RenderHoughLines(image_info,image->columns,image->rows,exception);
2088 artifact=GetImageArtifact(image,"hough-lines:accumulator");
2089 if ((lines_image != (Image *) NULL) &&
2090 (IsStringTrue(artifact) != MagickFalse))
2095 accumulator_image=MatrixToImage(accumulator,exception);
2096 if (accumulator_image != (Image *) NULL)
2097 AppendImageToList(&lines_image,accumulator_image);
2102 accumulator=DestroyMatrixInfo(accumulator);
2103 image_info=DestroyImageInfo(image_info);
2104 (void) RelinquishUniqueFileResource(path);
2105 return(GetFirstImageInList(lines_image));
2109 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2113 % M e a n S h i f t I m a g e %
2117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2119 % MeanShiftImage() delineate arbitrarily shaped clusters in the image. For
2120 % each pixel, it visits all the pixels in the neighborhood specified by
2121 % the window centered at the pixel and excludes those that are outside the
2122 % radius=(window-1)/2 surrounding the pixel. From those pixels, it finds those
2123 % that are within the specified color distance from the current mean, and
2124 % computes a new x,y centroid from those coordinates and a new mean. This new
2125 % x,y centroid is used as the center for a new window. This process iterates
2126 % until it converges and the final mean is replaces the (original window
2127 % center) pixel value. It repeats this process for the next pixel, etc.,
2128 % until it processes all pixels in the image. Results are typically better with
2129 % colorspaces other than sRGB. We recommend YIQ, YUV or YCbCr.
2131 % The format of the MeanShiftImage method is:
2133 % Image *MeanShiftImage(const Image *image,const size_t width,
2134 % const size_t height,const double color_distance,
2135 % ExceptionInfo *exception)
2137 % A description of each parameter follows:
2139 % o image: the image.
2141 % o width, height: find pixels in this neighborhood.
2143 % o color_distance: the color distance.
2145 % o exception: return any errors or warnings in this structure.
2148 MagickExport Image *MeanShiftImage(const Image *image,const size_t width,
2149 const size_t height,const double color_distance,ExceptionInfo *exception)
2151 #define MaxMeanShiftIterations 100
2152 #define MeanShiftImageTag "MeanShift/Image"
2171 assert(image != (const Image *) NULL);
2172 assert(image->signature == MagickCoreSignature);
2173 if (image->debug != MagickFalse)
2174 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2175 assert(exception != (ExceptionInfo *) NULL);
2176 assert(exception->signature == MagickCoreSignature);
2177 mean_image=CloneImage(image,image->columns,image->rows,MagickTrue,exception);
2178 if (mean_image == (Image *) NULL)
2179 return((Image *) NULL);
2180 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2182 mean_image=DestroyImage(mean_image);
2183 return((Image *) NULL);
2187 image_view=AcquireVirtualCacheView(image,exception);
2188 pixel_view=AcquireVirtualCacheView(image,exception);
2189 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2190 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2191 #pragma omp parallel for schedule(static,4) shared(status,progress) \
2192 magick_threads(mean_image,mean_image,mean_image->rows,1)
2194 for (y=0; y < (ssize_t) mean_image->rows; y++)
2196 register const Quantum
2205 if (status == MagickFalse)
2207 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2208 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2210 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2215 for (x=0; x < (ssize_t) mean_image->columns; x++)
2228 GetPixelInfo(image,&mean_pixel);
2229 GetPixelInfoPixel(image,p,&mean_pixel);
2230 mean_location.x=(double) x;
2231 mean_location.y=(double) y;
2232 for (i=0; i < MaxMeanShiftIterations; i++)
2250 GetPixelInfo(image,&sum_pixel);
2251 previous_location=mean_location;
2252 previous_pixel=mean_pixel;
2254 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2259 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2261 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2266 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2267 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2268 mean_location.y+v),&pixel,exception);
2269 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2270 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2271 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2272 if (distance <= (color_distance*color_distance))
2274 sum_location.x+=mean_location.x+u;
2275 sum_location.y+=mean_location.y+v;
2276 sum_pixel.red+=pixel.red;
2277 sum_pixel.green+=pixel.green;
2278 sum_pixel.blue+=pixel.blue;
2279 sum_pixel.alpha+=pixel.alpha;
2286 mean_location.x=gamma*sum_location.x;
2287 mean_location.y=gamma*sum_location.y;
2288 mean_pixel.red=gamma*sum_pixel.red;
2289 mean_pixel.green=gamma*sum_pixel.green;
2290 mean_pixel.blue=gamma*sum_pixel.blue;
2291 mean_pixel.alpha=gamma*sum_pixel.alpha;
2292 distance=(mean_location.x-previous_location.x)*
2293 (mean_location.x-previous_location.x)+
2294 (mean_location.y-previous_location.y)*
2295 (mean_location.y-previous_location.y)+
2296 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2297 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2298 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2299 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2300 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2301 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2302 if (distance <= 3.0)
2305 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2306 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2307 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2308 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2309 p+=GetPixelChannels(image);
2310 q+=GetPixelChannels(mean_image);
2312 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2314 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2319 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2320 #pragma omp critical (MagickCore_MeanShiftImage)
2322 proceed=SetImageProgress(image,MeanShiftImageTag,progress++,
2324 if (proceed == MagickFalse)
2328 mean_view=DestroyCacheView(mean_view);
2329 pixel_view=DestroyCacheView(pixel_view);
2330 image_view=DestroyCacheView(image_view);