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/nt-base-private.h"
80#include "MagickCore/option.h"
81#include "MagickCore/paint.h"
82#include "MagickCore/pixel-accessor.h"
83#include "MagickCore/profile.h"
84#include "MagickCore/property.h"
85#include "MagickCore/quantize.h"
86#include "MagickCore/quantum-private.h"
87#include "MagickCore/random_.h"
88#include "MagickCore/resource_.h"
89#include "MagickCore/segment.h"
90#include "MagickCore/semaphore.h"
91#include "MagickCore/signature-private.h"
92#include "MagickCore/statistic-private.h"
93#include "MagickCore/string_.h"
94#include "MagickCore/thread-private.h"
95#include "MagickCore/timer.h"
96#include "MagickCore/utility.h"
97#include "MagickCore/version.h"
149static inline MagickBooleanType IsAuthenticPixel(
const Image *image,
150 const ssize_t x,
const ssize_t y)
152 if ((x < 0) || (x >= (ssize_t) image->columns))
154 if ((y < 0) || (y >= (ssize_t) image->rows))
159static MagickBooleanType TraceEdges(
Image *edge_image,
CacheView *edge_view,
160 MatrixInfo *canny_cache,
const ssize_t x,
const ssize_t y,
176 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
177 if (q == (Quantum *) NULL)
180 status=SyncCacheViewAuthenticPixels(edge_view,exception);
181 if (status == MagickFalse)
183 if (GetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
187 if (SetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
195 status=GetMatrixElement(canny_cache,i,0,&edge);
196 if (status == MagickFalse)
198 for (v=(-1); v <= 1; v++)
203 for (u=(-1); u <= 1; u++)
205 if ((u == 0) && (v == 0))
207 if (IsAuthenticPixel(edge_image,edge.x+u,edge.y+v) == MagickFalse)
212 q=GetCacheViewAuthenticPixels(edge_view,edge.x+u,edge.y+v,1,1,
214 if (q == (Quantum *) NULL)
216 status=GetMatrixElement(canny_cache,edge.x+u,edge.y+v,&pixel);
217 if (status == MagickFalse)
219 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
220 (pixel.intensity >= lower_threshold))
223 status=SyncCacheViewAuthenticPixels(edge_view,exception);
224 if (status == MagickFalse)
228 status=SetMatrixElement(canny_cache,i,0,&edge);
229 if (status == MagickFalse)
239MagickExport
Image *CannyEdgeImage(
const Image *image,
const double radius,
240 const double sigma,
const double lower_percent,
const double upper_percent,
243#define CannyEdgeImageTag "CannyEdge/Image"
252 geometry[MagickPathExtent];
278 assert(image != (
const Image *) NULL);
279 assert(image->signature == MagickCoreSignature);
281 assert(exception->signature == MagickCoreSignature);
282 if (IsEventLogging() != MagickFalse)
283 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
287 (void) FormatLocaleString(geometry,MagickPathExtent,
288 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
289 kernel_info=AcquireKernelInfo(geometry,exception);
291 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
292 edge_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
293 kernel_info=DestroyKernelInfo(kernel_info);
294 if (edge_image == (
Image *) NULL)
295 return((
Image *) NULL);
296 if (TransformImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
298 edge_image=DestroyImage(edge_image);
299 return((
Image *) NULL);
301 (void) SetImageAlphaChannel(edge_image,OffAlphaChannel,exception);
305 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
309 edge_image=DestroyImage(edge_image);
310 return((
Image *) NULL);
313 edge_view=AcquireVirtualCacheView(edge_image,exception);
314#if defined(MAGICKCORE_OPENMP_SUPPORT)
315 #pragma omp parallel for schedule(static) shared(status) \
316 magick_number_threads(edge_image,edge_image,edge_image->rows,1)
318 for (y=0; y < (ssize_t) edge_image->rows; y++)
326 if (status == MagickFalse)
328 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
330 if (p == (
const Quantum *) NULL)
335 for (x=0; x < (ssize_t) edge_image->columns; x++)
345 *magick_restrict kernel_pixels;
362 (void) memset(&pixel,0,
sizeof(pixel));
366 for (v=0; v < 2; v++)
371 for (u=0; u < 2; u++)
376 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
377 dx+=0.5*Gx[v][u]*intensity;
378 dy+=0.5*Gy[v][u]*intensity;
380 kernel_pixels+=edge_image->columns+1;
382 pixel.magnitude=hypot(dx,dy);
384 if (fabs(dx) > MagickEpsilon)
392 if (slope < -2.41421356237)
395 if (slope < -0.414213562373)
402 if (slope > 2.41421356237)
405 if (slope > 0.414213562373)
411 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
413 p+=GetPixelChannels(edge_image);
416 edge_view=DestroyCacheView(edge_view);
422 (void) GetMatrixElement(canny_cache,0,0,&element);
423 max=element.intensity;
424 min=element.intensity;
425 edge_view=AcquireAuthenticCacheView(edge_image,exception);
426#if defined(MAGICKCORE_OPENMP_SUPPORT)
427 #pragma omp parallel for schedule(static) shared(status) \
428 magick_number_threads(edge_image,edge_image,edge_image->rows,1)
430 for (y=0; y < (ssize_t) edge_image->rows; y++)
438 if (status == MagickFalse)
440 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
442 if (q == (Quantum *) NULL)
447 for (x=0; x < (ssize_t) edge_image->columns; x++)
454 (void) GetMatrixElement(canny_cache,x,y,&pixel);
455 switch (pixel.orientation)
463 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
464 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
472 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
473 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
481 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
482 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
490 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
491 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
495 pixel.intensity=pixel.magnitude;
496 if ((pixel.magnitude < alpha_pixel.magnitude) ||
497 (pixel.magnitude < beta_pixel.magnitude))
499 (void) SetMatrixElement(canny_cache,x,y,&pixel);
500#if defined(MAGICKCORE_OPENMP_SUPPORT)
501 #pragma omp critical (MagickCore_CannyEdgeImage)
504 if (pixel.intensity < min)
506 if (pixel.intensity > max)
510 q+=GetPixelChannels(edge_image);
512 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
515 edge_view=DestroyCacheView(edge_view);
519 lower_threshold=lower_percent*(max-min)+min;
520 upper_threshold=upper_percent*(max-min)+min;
524 edge_view=AcquireAuthenticCacheView(edge_image,exception);
525 for (y=0; y < (ssize_t) edge_image->rows; y++)
530 if (status == MagickFalse)
532 for (x=0; x < (ssize_t) edge_image->columns; x++)
543 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
544 if (p == (
const Quantum *) NULL)
546 status=GetMatrixElement(canny_cache,x,y,&pixel);
547 if (status == MagickFalse)
549 if ((GetPixelIntensity(edge_image,p) == 0.0) &&
550 (pixel.intensity >= upper_threshold))
551 status=TraceEdges(edge_image,edge_view,canny_cache,x,y,lower_threshold,
554 if (image->progress_monitor != (MagickProgressMonitor) NULL)
559#if defined(MAGICKCORE_OPENMP_SUPPORT)
563 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
564 if (proceed == MagickFalse)
568 edge_view=DestroyCacheView(edge_view);
572 canny_cache=DestroyMatrixInfo(canny_cache);
664 assert(image != (
Image *) NULL);
665 assert(image->signature == MagickCoreSignature);
666 if (IsEventLogging() != MagickFalse)
667 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
668 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
670 length=MaxPixelChannels+1UL;
672 sizeof(*channel_features));
674 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
675 (void) memset(channel_features,0,length*
676 sizeof(*channel_features));
680 grays=(
PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*grays));
685 (void) ThrowMagickException(exception,GetMagickModule(),
686 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
687 return(channel_features);
689 for (i=0; i <= (ssize_t) MaxMap; i++)
692 grays[i].green=(~0U);
694 grays[i].alpha=(~0U);
695 grays[i].black=(~0U);
698 image_view=AcquireVirtualCacheView(image,exception);
699#if defined(MAGICKCORE_OPENMP_SUPPORT)
700 #pragma omp parallel for schedule(static) shared(status) \
701 magick_number_threads(image,image,image->rows,1)
703 for (r=0; r < (ssize_t) image->rows; r++)
711 if (status == MagickFalse)
713 p=GetCacheViewVirtualPixels(image_view,0,r,image->columns,1,exception);
714 if (p == (
const Quantum *) NULL)
719 for (x=0; x < (ssize_t) image->columns; x++)
721 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
722 ScaleQuantumToMap(GetPixelRed(image,p));
723 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
724 ScaleQuantumToMap(GetPixelGreen(image,p));
725 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
726 ScaleQuantumToMap(GetPixelBlue(image,p));
727 if (image->colorspace == CMYKColorspace)
728 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
729 ScaleQuantumToMap(GetPixelBlack(image,p));
730 if (image->alpha_trait != UndefinedPixelTrait)
731 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
732 ScaleQuantumToMap(GetPixelAlpha(image,p));
733 p+=GetPixelChannels(image);
736 image_view=DestroyCacheView(image_view);
737 if (status == MagickFalse)
739 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
742 return(channel_features);
744 (void) memset(&gray,0,
sizeof(gray));
745 for (i=0; i <= (ssize_t) MaxMap; i++)
747 if (grays[i].red != ~0U)
748 grays[gray.red++].red=grays[i].red;
749 if (grays[i].green != ~0U)
750 grays[gray.green++].green=grays[i].green;
751 if (grays[i].blue != ~0U)
752 grays[gray.blue++].blue=grays[i].blue;
753 if (image->colorspace == CMYKColorspace)
754 if (grays[i].black != ~0U)
755 grays[gray.black++].black=grays[i].black;
756 if (image->alpha_trait != UndefinedPixelTrait)
757 if (grays[i].alpha != ~0U)
758 grays[gray.alpha++].alpha=grays[i].alpha;
763 number_grays=gray.red;
764 if (gray.green > number_grays)
765 number_grays=gray.green;
766 if (gray.blue > number_grays)
767 number_grays=gray.blue;
768 if (image->colorspace == CMYKColorspace)
769 if (gray.black > number_grays)
770 number_grays=gray.black;
771 if (image->alpha_trait != UndefinedPixelTrait)
772 if (gray.alpha > number_grays)
773 number_grays=gray.alpha;
775 sizeof(*cooccurrence));
777 2*
sizeof(*density_x));
779 2*
sizeof(*density_xy));
781 2*
sizeof(*density_y));
793 for (i=0; i < (ssize_t) number_grays; i++)
807 for (i=0; i < (ssize_t) number_grays; i++)
809 RelinquishMagickMemory(cooccurrence[i]);
813 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
816 (void) ThrowMagickException(exception,GetMagickModule(),
817 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
818 return(channel_features);
820 (void) memset(&correlation,0,
sizeof(correlation));
821 (void) memset(density_x,0,2*(number_grays+1)*
sizeof(*density_x));
822 (void) memset(density_xy,0,2*(number_grays+1)*
sizeof(*density_xy));
823 (void) memset(density_y,0,2*(number_grays+1)*
sizeof(*density_y));
824 (void) memset(&mean,0,
sizeof(mean));
825 (void) memset(sum,0,number_grays*
sizeof(*sum));
826 (void) memset(&sum_squares,0,
sizeof(sum_squares));
827 (void) memset(density_xy,0,2*number_grays*
sizeof(*density_xy));
828 (void) memset(&entropy_x,0,
sizeof(entropy_x));
829 (void) memset(&entropy_xy,0,
sizeof(entropy_xy));
830 (void) memset(&entropy_xy1,0,
sizeof(entropy_xy1));
831 (void) memset(&entropy_xy2,0,
sizeof(entropy_xy2));
832 (void) memset(&entropy_y,0,
sizeof(entropy_y));
833 (void) memset(&variance,0,
sizeof(variance));
834 for (i=0; i < (ssize_t) number_grays; i++)
837 sizeof(**cooccurrence));
842 (void) memset(cooccurrence[i],0,number_grays*
843 sizeof(**cooccurrence));
844 (void) memset(Q[i],0,number_grays*
sizeof(**Q));
846 if (i < (ssize_t) number_grays)
848 for (i--; i >= 0; i--)
854 RelinquishMagickMemory(cooccurrence[i]);
862 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
865 (void) ThrowMagickException(exception,GetMagickModule(),
866 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
867 return(channel_features);
873 image_view=AcquireVirtualCacheView(image,exception);
874 for (r=0; r < (ssize_t) image->rows; r++)
887 if (status == MagickFalse)
889 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,r,image->columns+
890 2*distance,distance+2,exception);
891 if (p == (
const Quantum *) NULL)
896 p+=distance*GetPixelChannels(image);;
897 for (x=0; x < (ssize_t) image->columns; x++)
899 for (i=0; i < 4; i++)
909 offset=(ssize_t) distance;
917 offset=(ssize_t) (image->columns+2*distance);
925 offset=(ssize_t) ((image->columns+2*distance)-distance);
933 offset=(ssize_t) ((image->columns+2*distance)+distance);
939 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
941 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*(ssize_t) GetPixelChannels(image))))
943 cooccurrence[u][v].direction[i].red++;
944 cooccurrence[v][u].direction[i].red++;
947 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
949 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*(ssize_t) GetPixelChannels(image))))
951 cooccurrence[u][v].direction[i].green++;
952 cooccurrence[v][u].direction[i].green++;
955 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
957 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*(ssize_t) GetPixelChannels(image))))
959 cooccurrence[u][v].direction[i].blue++;
960 cooccurrence[v][u].direction[i].blue++;
961 if (image->colorspace == CMYKColorspace)
965 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
967 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*(ssize_t) GetPixelChannels(image))))
969 cooccurrence[u][v].direction[i].black++;
970 cooccurrence[v][u].direction[i].black++;
972 if (image->alpha_trait != UndefinedPixelTrait)
976 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
978 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*(ssize_t) GetPixelChannels(image))))
980 cooccurrence[u][v].direction[i].alpha++;
981 cooccurrence[v][u].direction[i].alpha++;
984 p+=GetPixelChannels(image);
987 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
988 image_view=DestroyCacheView(image_view);
989 if (status == MagickFalse)
991 for (i=0; i < (ssize_t) number_grays; i++)
993 RelinquishMagickMemory(cooccurrence[i]);
997 (void) ThrowMagickException(exception,GetMagickModule(),
998 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
999 return(channel_features);
1004 for (i=0; i < 4; i++)
1020 normalize=2.0*image->rows*(image->columns-distance);
1028 normalize=2.0*(image->rows-distance)*image->columns;
1036 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1044 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1048 normalize=PerceptibleReciprocal(normalize);
1049 for (y=0; y < (ssize_t) number_grays; y++)
1054 for (x=0; x < (ssize_t) number_grays; x++)
1056 cooccurrence[x][y].direction[i].red*=normalize;
1057 cooccurrence[x][y].direction[i].green*=normalize;
1058 cooccurrence[x][y].direction[i].blue*=normalize;
1059 if (image->colorspace == CMYKColorspace)
1060 cooccurrence[x][y].direction[i].black*=normalize;
1061 if (image->alpha_trait != UndefinedPixelTrait)
1062 cooccurrence[x][y].direction[i].alpha*=normalize;
1069#if defined(MAGICKCORE_OPENMP_SUPPORT)
1070 #pragma omp parallel for schedule(static) shared(status) \
1071 magick_number_threads(image,image,number_grays,1)
1073 for (i=0; i < 4; i++)
1078 for (y=0; y < (ssize_t) number_grays; y++)
1083 for (x=0; x < (ssize_t) number_grays; x++)
1088 channel_features[RedPixelChannel].angular_second_moment[i]+=
1089 cooccurrence[x][y].direction[i].red*
1090 cooccurrence[x][y].direction[i].red;
1091 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1092 cooccurrence[x][y].direction[i].green*
1093 cooccurrence[x][y].direction[i].green;
1094 channel_features[BluePixelChannel].angular_second_moment[i]+=
1095 cooccurrence[x][y].direction[i].blue*
1096 cooccurrence[x][y].direction[i].blue;
1097 if (image->colorspace == CMYKColorspace)
1098 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1099 cooccurrence[x][y].direction[i].black*
1100 cooccurrence[x][y].direction[i].black;
1101 if (image->alpha_trait != UndefinedPixelTrait)
1102 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1103 cooccurrence[x][y].direction[i].alpha*
1104 cooccurrence[x][y].direction[i].alpha;
1108 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1109 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1110 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1111 if (image->colorspace == CMYKColorspace)
1112 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1113 if (image->alpha_trait != UndefinedPixelTrait)
1114 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1115 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1116 correlation.direction[i].green+=x*y*
1117 cooccurrence[x][y].direction[i].green;
1118 correlation.direction[i].blue+=x*y*
1119 cooccurrence[x][y].direction[i].blue;
1120 if (image->colorspace == CMYKColorspace)
1121 correlation.direction[i].black+=x*y*
1122 cooccurrence[x][y].direction[i].black;
1123 if (image->alpha_trait != UndefinedPixelTrait)
1124 correlation.direction[i].alpha+=x*y*
1125 cooccurrence[x][y].direction[i].alpha;
1129 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1130 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1131 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1132 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1133 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1134 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1135 if (image->colorspace == CMYKColorspace)
1136 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1137 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1138 if (image->alpha_trait != UndefinedPixelTrait)
1139 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1140 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1144 density_xy[y+x+2].direction[i].red+=
1145 cooccurrence[x][y].direction[i].red;
1146 density_xy[y+x+2].direction[i].green+=
1147 cooccurrence[x][y].direction[i].green;
1148 density_xy[y+x+2].direction[i].blue+=
1149 cooccurrence[x][y].direction[i].blue;
1150 if (image->colorspace == CMYKColorspace)
1151 density_xy[y+x+2].direction[i].black+=
1152 cooccurrence[x][y].direction[i].black;
1153 if (image->alpha_trait != UndefinedPixelTrait)
1154 density_xy[y+x+2].direction[i].alpha+=
1155 cooccurrence[x][y].direction[i].alpha;
1159 channel_features[RedPixelChannel].entropy[i]-=
1160 cooccurrence[x][y].direction[i].red*
1161 MagickLog10(cooccurrence[x][y].direction[i].red);
1162 channel_features[GreenPixelChannel].entropy[i]-=
1163 cooccurrence[x][y].direction[i].green*
1164 MagickLog10(cooccurrence[x][y].direction[i].green);
1165 channel_features[BluePixelChannel].entropy[i]-=
1166 cooccurrence[x][y].direction[i].blue*
1167 MagickLog10(cooccurrence[x][y].direction[i].blue);
1168 if (image->colorspace == CMYKColorspace)
1169 channel_features[BlackPixelChannel].entropy[i]-=
1170 cooccurrence[x][y].direction[i].black*
1171 MagickLog10(cooccurrence[x][y].direction[i].black);
1172 if (image->alpha_trait != UndefinedPixelTrait)
1173 channel_features[AlphaPixelChannel].entropy[i]-=
1174 cooccurrence[x][y].direction[i].alpha*
1175 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1179 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1180 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1181 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1182 if (image->alpha_trait != UndefinedPixelTrait)
1183 density_x[x].direction[i].alpha+=
1184 cooccurrence[x][y].direction[i].alpha;
1185 if (image->colorspace == CMYKColorspace)
1186 density_x[x].direction[i].black+=
1187 cooccurrence[x][y].direction[i].black;
1188 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1189 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1190 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1191 if (image->colorspace == CMYKColorspace)
1192 density_y[y].direction[i].black+=
1193 cooccurrence[x][y].direction[i].black;
1194 if (image->alpha_trait != UndefinedPixelTrait)
1195 density_y[y].direction[i].alpha+=
1196 cooccurrence[x][y].direction[i].alpha;
1198 mean.direction[i].red+=y*sum[y].direction[i].red;
1199 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1200 mean.direction[i].green+=y*sum[y].direction[i].green;
1201 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1202 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1203 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1204 if (image->colorspace == CMYKColorspace)
1206 mean.direction[i].black+=y*sum[y].direction[i].black;
1207 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1209 if (image->alpha_trait != UndefinedPixelTrait)
1211 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1212 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1218 channel_features[RedPixelChannel].correlation[i]=
1219 (correlation.direction[i].red-mean.direction[i].red*
1220 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1221 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1222 sum_squares.direction[i].red-(mean.direction[i].red*
1223 mean.direction[i].red)));
1224 channel_features[GreenPixelChannel].correlation[i]=
1225 (correlation.direction[i].green-mean.direction[i].green*
1226 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1227 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1228 sum_squares.direction[i].green-(mean.direction[i].green*
1229 mean.direction[i].green)));
1230 channel_features[BluePixelChannel].correlation[i]=
1231 (correlation.direction[i].blue-mean.direction[i].blue*
1232 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1233 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1234 sum_squares.direction[i].blue-(mean.direction[i].blue*
1235 mean.direction[i].blue)));
1236 if (image->colorspace == CMYKColorspace)
1237 channel_features[BlackPixelChannel].correlation[i]=
1238 (correlation.direction[i].black-mean.direction[i].black*
1239 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1240 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1241 sum_squares.direction[i].black-(mean.direction[i].black*
1242 mean.direction[i].black)));
1243 if (image->alpha_trait != UndefinedPixelTrait)
1244 channel_features[AlphaPixelChannel].correlation[i]=
1245 (correlation.direction[i].alpha-mean.direction[i].alpha*
1246 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1247 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1248 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1249 mean.direction[i].alpha)));
1254#if defined(MAGICKCORE_OPENMP_SUPPORT)
1255 #pragma omp parallel for schedule(static) shared(status) \
1256 magick_number_threads(image,image,number_grays,1)
1258 for (i=0; i < 4; i++)
1263 for (x=2; x < (ssize_t) (2*number_grays); x++)
1268 channel_features[RedPixelChannel].sum_average[i]+=
1269 x*density_xy[x].direction[i].red;
1270 channel_features[GreenPixelChannel].sum_average[i]+=
1271 x*density_xy[x].direction[i].green;
1272 channel_features[BluePixelChannel].sum_average[i]+=
1273 x*density_xy[x].direction[i].blue;
1274 if (image->colorspace == CMYKColorspace)
1275 channel_features[BlackPixelChannel].sum_average[i]+=
1276 x*density_xy[x].direction[i].black;
1277 if (image->alpha_trait != UndefinedPixelTrait)
1278 channel_features[AlphaPixelChannel].sum_average[i]+=
1279 x*density_xy[x].direction[i].alpha;
1283 channel_features[RedPixelChannel].sum_entropy[i]-=
1284 density_xy[x].direction[i].red*
1285 MagickLog10(density_xy[x].direction[i].red);
1286 channel_features[GreenPixelChannel].sum_entropy[i]-=
1287 density_xy[x].direction[i].green*
1288 MagickLog10(density_xy[x].direction[i].green);
1289 channel_features[BluePixelChannel].sum_entropy[i]-=
1290 density_xy[x].direction[i].blue*
1291 MagickLog10(density_xy[x].direction[i].blue);
1292 if (image->colorspace == CMYKColorspace)
1293 channel_features[BlackPixelChannel].sum_entropy[i]-=
1294 density_xy[x].direction[i].black*
1295 MagickLog10(density_xy[x].direction[i].black);
1296 if (image->alpha_trait != UndefinedPixelTrait)
1297 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1298 density_xy[x].direction[i].alpha*
1299 MagickLog10(density_xy[x].direction[i].alpha);
1303 channel_features[RedPixelChannel].sum_variance[i]+=
1304 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1305 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1306 density_xy[x].direction[i].red;
1307 channel_features[GreenPixelChannel].sum_variance[i]+=
1308 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1309 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1310 density_xy[x].direction[i].green;
1311 channel_features[BluePixelChannel].sum_variance[i]+=
1312 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1313 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1314 density_xy[x].direction[i].blue;
1315 if (image->colorspace == CMYKColorspace)
1316 channel_features[BlackPixelChannel].sum_variance[i]+=
1317 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1318 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1319 density_xy[x].direction[i].black;
1320 if (image->alpha_trait != UndefinedPixelTrait)
1321 channel_features[AlphaPixelChannel].sum_variance[i]+=
1322 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1323 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1324 density_xy[x].direction[i].alpha;
1330#if defined(MAGICKCORE_OPENMP_SUPPORT)
1331 #pragma omp parallel for schedule(static) shared(status) \
1332 magick_number_threads(image,image,number_grays,1)
1334 for (i=0; i < 4; i++)
1339 for (y=0; y < (ssize_t) number_grays; y++)
1344 for (x=0; x < (ssize_t) number_grays; x++)
1349 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1350 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1351 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1352 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1353 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1354 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1355 if (image->colorspace == CMYKColorspace)
1356 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1357 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1358 if (image->alpha_trait != UndefinedPixelTrait)
1359 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1360 (y-mean.direction[i].alpha+1)*
1361 cooccurrence[x][y].direction[i].alpha;
1365 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1366 cooccurrence[x][y].direction[i].red;
1367 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1368 cooccurrence[x][y].direction[i].green;
1369 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1370 cooccurrence[x][y].direction[i].blue;
1371 if (image->colorspace == CMYKColorspace)
1372 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1373 cooccurrence[x][y].direction[i].black;
1374 if (image->alpha_trait != UndefinedPixelTrait)
1375 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1376 cooccurrence[x][y].direction[i].alpha;
1380 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1381 MagickLog10(cooccurrence[x][y].direction[i].red);
1382 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1383 MagickLog10(cooccurrence[x][y].direction[i].green);
1384 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1385 MagickLog10(cooccurrence[x][y].direction[i].blue);
1386 if (image->colorspace == CMYKColorspace)
1387 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1388 MagickLog10(cooccurrence[x][y].direction[i].black);
1389 if (image->alpha_trait != UndefinedPixelTrait)
1390 entropy_xy.direction[i].alpha-=
1391 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1392 cooccurrence[x][y].direction[i].alpha);
1393 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1394 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1395 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1396 MagickLog10(density_x[x].direction[i].green*
1397 density_y[y].direction[i].green));
1398 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1399 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1400 if (image->colorspace == CMYKColorspace)
1401 entropy_xy1.direction[i].black-=(
1402 cooccurrence[x][y].direction[i].black*MagickLog10(
1403 density_x[x].direction[i].black*density_y[y].direction[i].black));
1404 if (image->alpha_trait != UndefinedPixelTrait)
1405 entropy_xy1.direction[i].alpha-=(
1406 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1407 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1408 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1409 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1410 density_y[y].direction[i].red));
1411 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1412 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1413 density_y[y].direction[i].green));
1414 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1415 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1416 density_y[y].direction[i].blue));
1417 if (image->colorspace == CMYKColorspace)
1418 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1419 density_y[y].direction[i].black*MagickLog10(
1420 density_x[x].direction[i].black*density_y[y].direction[i].black));
1421 if (image->alpha_trait != UndefinedPixelTrait)
1422 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1423 density_y[y].direction[i].alpha*MagickLog10(
1424 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1427 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1428 variance.direction[i].red;
1429 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1430 variance.direction[i].green;
1431 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1432 variance.direction[i].blue;
1433 if (image->colorspace == CMYKColorspace)
1434 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1435 variance.direction[i].black;
1436 if (image->alpha_trait != UndefinedPixelTrait)
1437 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1438 variance.direction[i].alpha;
1443 (void) memset(&variance,0,
sizeof(variance));
1444 (void) memset(&sum_squares,0,
sizeof(sum_squares));
1445#if defined(MAGICKCORE_OPENMP_SUPPORT)
1446 #pragma omp parallel for schedule(static) shared(status) \
1447 magick_number_threads(image,image,number_grays,1)
1449 for (i=0; i < 4; i++)
1454 for (x=0; x < (ssize_t) number_grays; x++)
1459 variance.direction[i].red+=density_xy[x].direction[i].red;
1460 variance.direction[i].green+=density_xy[x].direction[i].green;
1461 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1462 if (image->colorspace == CMYKColorspace)
1463 variance.direction[i].black+=density_xy[x].direction[i].black;
1464 if (image->alpha_trait != UndefinedPixelTrait)
1465 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1466 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1467 density_xy[x].direction[i].red;
1468 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1469 density_xy[x].direction[i].green;
1470 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1471 density_xy[x].direction[i].blue;
1472 if (image->colorspace == CMYKColorspace)
1473 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1474 density_xy[x].direction[i].black;
1475 if (image->alpha_trait != UndefinedPixelTrait)
1476 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1477 density_xy[x].direction[i].alpha;
1481 channel_features[RedPixelChannel].difference_entropy[i]-=
1482 density_xy[x].direction[i].red*
1483 MagickLog10(density_xy[x].direction[i].red);
1484 channel_features[GreenPixelChannel].difference_entropy[i]-=
1485 density_xy[x].direction[i].green*
1486 MagickLog10(density_xy[x].direction[i].green);
1487 channel_features[BluePixelChannel].difference_entropy[i]-=
1488 density_xy[x].direction[i].blue*
1489 MagickLog10(density_xy[x].direction[i].blue);
1490 if (image->colorspace == CMYKColorspace)
1491 channel_features[BlackPixelChannel].difference_entropy[i]-=
1492 density_xy[x].direction[i].black*
1493 MagickLog10(density_xy[x].direction[i].black);
1494 if (image->alpha_trait != UndefinedPixelTrait)
1495 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1496 density_xy[x].direction[i].alpha*
1497 MagickLog10(density_xy[x].direction[i].alpha);
1501 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1502 MagickLog10(density_x[x].direction[i].red));
1503 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1504 MagickLog10(density_x[x].direction[i].green));
1505 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1506 MagickLog10(density_x[x].direction[i].blue));
1507 if (image->colorspace == CMYKColorspace)
1508 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1509 MagickLog10(density_x[x].direction[i].black));
1510 if (image->alpha_trait != UndefinedPixelTrait)
1511 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1512 MagickLog10(density_x[x].direction[i].alpha));
1513 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1514 MagickLog10(density_y[x].direction[i].red));
1515 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1516 MagickLog10(density_y[x].direction[i].green));
1517 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1518 MagickLog10(density_y[x].direction[i].blue));
1519 if (image->colorspace == CMYKColorspace)
1520 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1521 MagickLog10(density_y[x].direction[i].black));
1522 if (image->alpha_trait != UndefinedPixelTrait)
1523 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1524 MagickLog10(density_y[x].direction[i].alpha));
1529 channel_features[RedPixelChannel].difference_variance[i]=
1530 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1531 (variance.direction[i].red*variance.direction[i].red))/
1532 ((
double) number_grays*number_grays*number_grays*number_grays);
1533 channel_features[GreenPixelChannel].difference_variance[i]=
1534 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1535 (variance.direction[i].green*variance.direction[i].green))/
1536 ((
double) number_grays*number_grays*number_grays*number_grays);
1537 channel_features[BluePixelChannel].difference_variance[i]=
1538 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1539 (variance.direction[i].blue*variance.direction[i].blue))/
1540 ((
double) number_grays*number_grays*number_grays*number_grays);
1541 if (image->colorspace == CMYKColorspace)
1542 channel_features[BlackPixelChannel].difference_variance[i]=
1543 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1544 (variance.direction[i].black*variance.direction[i].black))/
1545 ((
double) number_grays*number_grays*number_grays*number_grays);
1546 if (image->alpha_trait != UndefinedPixelTrait)
1547 channel_features[AlphaPixelChannel].difference_variance[i]=
1548 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1549 (variance.direction[i].alpha*variance.direction[i].alpha))/
1550 ((
double) number_grays*number_grays*number_grays*number_grays);
1554 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1555 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1556 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1557 entropy_x.direction[i].red : entropy_y.direction[i].red);
1558 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1559 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1560 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1561 entropy_x.direction[i].green : entropy_y.direction[i].green);
1562 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1563 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1564 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1565 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1566 if (image->colorspace == CMYKColorspace)
1567 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1568 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1569 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1570 entropy_x.direction[i].black : entropy_y.direction[i].black);
1571 if (image->alpha_trait != UndefinedPixelTrait)
1572 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1573 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1574 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1575 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1576 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1577 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].red-
1578 entropy_xy.direction[i].red)))));
1579 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1580 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].green-
1581 entropy_xy.direction[i].green)))));
1582 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1583 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].blue-
1584 entropy_xy.direction[i].blue)))));
1585 if (image->colorspace == CMYKColorspace)
1586 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1587 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].black-
1588 entropy_xy.direction[i].black)))));
1589 if (image->alpha_trait != UndefinedPixelTrait)
1590 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1591 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].alpha-
1592 entropy_xy.direction[i].alpha)))));
1597#if defined(MAGICKCORE_OPENMP_SUPPORT)
1598 #pragma omp parallel for schedule(static) shared(status) \
1599 magick_number_threads(image,image,number_grays,1)
1601 for (i=0; i < 4; i++)
1606 for (z=0; z < (ssize_t) number_grays; z++)
1614 (void) memset(&pixel,0,
sizeof(pixel));
1615 for (y=0; y < (ssize_t) number_grays; y++)
1620 for (x=0; x < (ssize_t) number_grays; x++)
1625 if (((y-x) == z) || ((x-y) == z))
1627 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1628 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1629 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1630 if (image->colorspace == CMYKColorspace)
1631 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1632 if (image->alpha_trait != UndefinedPixelTrait)
1633 pixel.direction[i].alpha+=
1634 cooccurrence[x][y].direction[i].alpha;
1639 if ((fabs(density_x[z].direction[i].red) > MagickEpsilon) &&
1640 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1641 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1642 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1643 density_y[x].direction[i].red;
1644 if ((fabs(density_x[z].direction[i].green) > MagickEpsilon) &&
1645 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1646 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1647 cooccurrence[y][x].direction[i].green/
1648 density_x[z].direction[i].green/density_y[x].direction[i].red;
1649 if ((fabs(density_x[z].direction[i].blue) > MagickEpsilon) &&
1650 (fabs(density_y[x].direction[i].blue) > MagickEpsilon))
1651 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1652 cooccurrence[y][x].direction[i].blue/
1653 density_x[z].direction[i].blue/density_y[x].direction[i].blue;
1654 if (image->colorspace == CMYKColorspace)
1655 if ((fabs(density_x[z].direction[i].black) > MagickEpsilon) &&
1656 (fabs(density_y[x].direction[i].black) > MagickEpsilon))
1657 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1658 cooccurrence[y][x].direction[i].black/
1659 density_x[z].direction[i].black/density_y[x].direction[i].black;
1660 if (image->alpha_trait != UndefinedPixelTrait)
1661 if ((fabs(density_x[z].direction[i].alpha) > MagickEpsilon) &&
1662 (fabs(density_y[x].direction[i].alpha) > MagickEpsilon))
1663 Q[z][y].direction[i].alpha+=
1664 cooccurrence[z][x].direction[i].alpha*
1665 cooccurrence[y][x].direction[i].alpha/
1666 density_x[z].direction[i].alpha/
1667 density_y[x].direction[i].alpha;
1670 channel_features[RedPixelChannel].contrast[i]+=z*z*
1671 pixel.direction[i].red;
1672 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1673 pixel.direction[i].green;
1674 channel_features[BluePixelChannel].contrast[i]+=z*z*
1675 pixel.direction[i].blue;
1676 if (image->colorspace == CMYKColorspace)
1677 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1678 pixel.direction[i].black;
1679 if (image->alpha_trait != UndefinedPixelTrait)
1680 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1681 pixel.direction[i].alpha;
1687 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1689 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1691 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1693 if (image->colorspace == CMYKColorspace)
1694 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1696 if (image->alpha_trait != UndefinedPixelTrait)
1697 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1704 for (i=0; i < (ssize_t) number_grays; i++)
1710 for (i=0; i < (ssize_t) number_grays; i++)
1712 RelinquishMagickMemory(cooccurrence[i]);
1714 return(channel_features);
1757static inline double MagickRound(
double x)
1762 if ((x-floor(x)) < (ceil(x)-x))
1767static Image *RenderHoughLines(
const ImageInfo *image_info,
const size_t columns,
1770#define BoundingBox "viewbox"
1784 image=AcquireImage(image_info,exception);
1785 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1786 if (status == MagickFalse)
1788 image=DestroyImageList(image);
1789 return((
Image *) NULL);
1791 image->columns=columns;
1793 draw_info=CloneDrawInfo(image_info,(
DrawInfo *) NULL);
1794 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
1796 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
1798 image->columns=(size_t) (draw_info->affine.sx*image->columns);
1799 image->rows=(size_t) (draw_info->affine.sy*image->rows);
1800 status=SetImageExtent(image,image->columns,image->rows,exception);
1801 if (status == MagickFalse)
1802 return(DestroyImageList(image));
1803 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1805 image=DestroyImageList(image);
1806 return((
Image *) NULL);
1811 if (GetBlobStreamData(image) == (
unsigned char *) NULL)
1812 draw_info->primitive=FileToString(image->filename,~0UL,exception);
1815 draw_info->primitive=(
char *) AcquireQuantumMemory(1,(
size_t)
1816 GetBlobSize(image)+1);
1817 if (draw_info->primitive != (
char *) NULL)
1819 (void) memcpy(draw_info->primitive,GetBlobStreamData(image),
1820 (
size_t) GetBlobSize(image));
1821 draw_info->primitive[GetBlobSize(image)]=
'\0';
1824 (void) DrawImage(image,draw_info,exception);
1825 draw_info=DestroyDrawInfo(draw_info);
1826 if (CloseBlob(image) == MagickFalse)
1827 image=DestroyImageList(image);
1828 return(GetFirstImageInList(image));
1831MagickExport
Image *HoughLineImage(
const Image *image,
const size_t width,
1832 const size_t height,
const size_t threshold,
ExceptionInfo *exception)
1834#define HoughLineImageTag "HoughLine/Image"
1840 message[MagickPathExtent],
1841 path[MagickPathExtent];
1850 *lines_image = NULL;
1881 assert(image != (
const Image *) NULL);
1882 assert(image->signature == MagickCoreSignature);
1884 assert(exception->signature == MagickCoreSignature);
1885 if (IsEventLogging() != MagickFalse)
1886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1887 accumulator_width=180;
1888 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1889 image->rows : image->columns))/2.0);
1890 accumulator_height=(size_t) (2.0*hough_height);
1891 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1892 sizeof(
double),exception);
1894 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1895 if (NullMatrix(accumulator) == MagickFalse)
1897 accumulator=DestroyMatrixInfo(accumulator);
1898 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1905 center.x=(double) image->columns/2.0;
1906 center.y=(double) image->rows/2.0;
1907 image_view=AcquireVirtualCacheView(image,exception);
1908 for (y=0; y < (ssize_t) image->rows; y++)
1916 if (status == MagickFalse)
1918 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1919 if (p == (Quantum *) NULL)
1924 for (x=0; x < (ssize_t) image->columns; x++)
1926 if (GetPixelIntensity(image,p) > ((
double) QuantumRange/2.0))
1931 for (i=0; i < 180; i++)
1937 radius=(((double) x-center.x)*cos(DegreesToRadians((
double) i)))+
1938 (((
double) y-center.y)*sin(DegreesToRadians((
double) i)));
1939 (void) GetMatrixElement(accumulator,i,(ssize_t)
1940 MagickRound(radius+hough_height),&count);
1942 (void) SetMatrixElement(accumulator,i,(ssize_t)
1943 MagickRound(radius+hough_height),&count);
1946 p+=GetPixelChannels(image);
1948 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1953#if defined(MAGICKCORE_OPENMP_SUPPORT)
1957 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
1958 if (proceed == MagickFalse)
1962 image_view=DestroyCacheView(image_view);
1963 if (status == MagickFalse)
1965 accumulator=DestroyMatrixInfo(accumulator);
1966 return((
Image *) NULL);
1971 file=AcquireUniqueFileResource(path);
1974 accumulator=DestroyMatrixInfo(accumulator);
1975 return((
Image *) NULL);
1977 (void) FormatLocaleString(message,MagickPathExtent,
1978 "# Hough line transform: %.20gx%.20g%+.20g\n",(
double) width,
1979 (
double) height,(
double) threshold);
1980 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1982 (void) FormatLocaleString(message,MagickPathExtent,
1983 "viewbox 0 0 %.20g %.20g\n",(
double) image->columns,(
double) image->rows);
1984 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1986 (void) FormatLocaleString(message,MagickPathExtent,
1987 "# x1,y1 x2,y2 # count angle distance\n");
1988 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1990 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1992 line_count=threshold;
1993 for (y=0; y < (ssize_t) accumulator_height; y++)
1998 for (x=0; x < (ssize_t) accumulator_width; x++)
2003 (void) GetMatrixElement(accumulator,x,y,&count);
2004 if (count >= (
double) line_count)
2019 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2024 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2026 if ((u != 0) || (v !=0))
2028 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
2036 if (u < (ssize_t) (width/2))
2039 (void) GetMatrixElement(accumulator,x,y,&count);
2042 if ((x >= 45) && (x <= 135))
2048 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
2049 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2050 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2051 line.x2=(double) image->columns;
2052 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
2053 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2054 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2062 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
2063 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2064 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2065 line.y2=(double) image->rows;
2066 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
2067 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2068 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2070 (void) FormatLocaleString(message,MagickPathExtent,
2071 "line %g,%g %g,%g # %g %g %g\n",line.x1,line.y1,line.x2,line.y2,
2072 maxima,(
double) x,(
double) y);
2073 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2082 image_info=AcquireImageInfo();
2083 image_info->background_color=image->background_color;
2084 (void) FormatLocaleString(image_info->filename,MagickPathExtent,
"%s",path);
2085 artifact=GetImageArtifact(image,
"background");
2086 if (artifact != (
const char *) NULL)
2087 (void) SetImageOption(image_info,
"background",artifact);
2088 artifact=GetImageArtifact(image,
"fill");
2089 if (artifact != (
const char *) NULL)
2090 (void) SetImageOption(image_info,
"fill",artifact);
2091 artifact=GetImageArtifact(image,
"stroke");
2092 if (artifact != (
const char *) NULL)
2093 (void) SetImageOption(image_info,
"stroke",artifact);
2094 artifact=GetImageArtifact(image,
"strokewidth");
2095 if (artifact != (
const char *) NULL)
2096 (void) SetImageOption(image_info,
"strokewidth",artifact);
2097 lines_image=RenderHoughLines(image_info,image->columns,image->rows,exception);
2098 artifact=GetImageArtifact(image,
"hough-lines:accumulator");
2099 if ((lines_image != (
Image *) NULL) &&
2100 (IsStringTrue(artifact) != MagickFalse))
2105 accumulator_image=MatrixToImage(accumulator,exception);
2106 if (accumulator_image != (
Image *) NULL)
2107 AppendImageToList(&lines_image,accumulator_image);
2112 accumulator=DestroyMatrixInfo(accumulator);
2113 image_info=DestroyImageInfo(image_info);
2114 (void) RelinquishUniqueFileResource(path);
2115 return(GetFirstImageInList(lines_image));
2158MagickExport
Image *MeanShiftImage(
const Image *image,
const size_t width,
2159 const size_t height,
const double color_distance,
ExceptionInfo *exception)
2161#define MaxMeanShiftIterations 100
2162#define MeanShiftImageTag "MeanShift/Image"
2181 assert(image != (
const Image *) NULL);
2182 assert(image->signature == MagickCoreSignature);
2184 assert(exception->signature == MagickCoreSignature);
2185 if (IsEventLogging() != MagickFalse)
2186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2187 mean_image=CloneImage(image,0,0,MagickTrue,exception);
2188 if (mean_image == (
Image *) NULL)
2189 return((
Image *) NULL);
2190 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2192 mean_image=DestroyImage(mean_image);
2193 return((
Image *) NULL);
2197 image_view=AcquireVirtualCacheView(image,exception);
2198 pixel_view=AcquireVirtualCacheView(image,exception);
2199 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2200#if defined(MAGICKCORE_OPENMP_SUPPORT)
2201 #pragma omp parallel for schedule(static) shared(status,progress) \
2202 magick_number_threads(mean_image,mean_image,mean_image->rows,1)
2204 for (y=0; y < (ssize_t) mean_image->rows; y++)
2215 if (status == MagickFalse)
2217 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2218 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2220 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2225 for (x=0; x < (ssize_t) mean_image->columns; x++)
2238 GetPixelInfo(image,&mean_pixel);
2239 GetPixelInfoPixel(image,p,&mean_pixel);
2240 mean_location.x=(double) x;
2241 mean_location.y=(double) y;
2242 for (i=0; i < MaxMeanShiftIterations; i++)
2260 GetPixelInfo(image,&sum_pixel);
2261 previous_location=mean_location;
2262 previous_pixel=mean_pixel;
2264 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2269 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2271 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2276 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2277 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2278 mean_location.y+v),&pixel,exception);
2279 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2280 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2281 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2282 if (distance <= (color_distance*color_distance))
2284 sum_location.x+=mean_location.x+u;
2285 sum_location.y+=mean_location.y+v;
2286 sum_pixel.red+=pixel.red;
2287 sum_pixel.green+=pixel.green;
2288 sum_pixel.blue+=pixel.blue;
2289 sum_pixel.alpha+=pixel.alpha;
2296 gamma=PerceptibleReciprocal((
double) count);
2297 mean_location.x=gamma*sum_location.x;
2298 mean_location.y=gamma*sum_location.y;
2299 mean_pixel.red=gamma*sum_pixel.red;
2300 mean_pixel.green=gamma*sum_pixel.green;
2301 mean_pixel.blue=gamma*sum_pixel.blue;
2302 mean_pixel.alpha=gamma*sum_pixel.alpha;
2303 distance=(mean_location.x-previous_location.x)*
2304 (mean_location.x-previous_location.x)+
2305 (mean_location.y-previous_location.y)*
2306 (mean_location.y-previous_location.y)+
2307 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2308 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2309 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2310 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2311 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2312 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2313 if (distance <= 3.0)
2316 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2317 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2318 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2319 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2320 p+=GetPixelChannels(image);
2321 q+=GetPixelChannels(mean_image);
2323 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2325 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2330#if defined(MAGICKCORE_OPENMP_SUPPORT)
2334 proceed=SetImageProgress(image,MeanShiftImageTag,progress,image->rows);
2335 if (proceed == MagickFalse)
2339 mean_view=DestroyCacheView(mean_view);
2340 pixel_view=DestroyCacheView(pixel_view);
2341 image_view=DestroyCacheView(image_view);