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"
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,
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)
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,
241 #define CannyEdgeImageTag "CannyEdge/Image"
250 geometry[MagickPathExtent];
276 assert(image != (
const Image *) NULL);
277 assert(image->signature == MagickCoreSignature);
279 assert(exception->signature == MagickCoreSignature);
280 if (IsEventLogging() != MagickFalse)
281 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
285 (void) FormatLocaleString(geometry,MagickPathExtent,
286 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
287 kernel_info=AcquireKernelInfo(geometry,exception);
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);
303 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
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) shared(status) \
314 magick_number_threads(edge_image,edge_image,edge_image->rows,1)
316 for (y=0; y < (ssize_t) edge_image->rows; y++)
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++)
343 *magick_restrict kernel_pixels;
360 (void) memset(&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);
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) shared(status) \
426 magick_number_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)
461 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
462 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
470 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
471 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
479 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
480 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
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);
517 lower_threshold=lower_percent*(max-min)+min;
518 upper_threshold=upper_percent*(max-min)+min;
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++)
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)
561 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
562 if (proceed == MagickFalse)
566 edge_view=DestroyCacheView(edge_view);
570 canny_cache=DestroyMatrixInfo(canny_cache);
614 static inline double MagickLog10(
const double x)
616 #define Log10Epsilon (1.0e-11)
618 if (fabs(x) < Log10Epsilon)
619 return(log10(Log10Epsilon));
620 return(log10(fabs(x)));
672 assert(image != (
Image *) NULL);
673 assert(image->signature == MagickCoreSignature);
674 if (IsEventLogging() != MagickFalse)
675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
676 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
678 length=MaxPixelChannels+1UL;
680 sizeof(*channel_features));
682 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
683 (void) memset(channel_features,0,length*
684 sizeof(*channel_features));
688 grays=(
PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*grays));
693 (void) ThrowMagickException(exception,GetMagickModule(),
694 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
695 return(channel_features);
697 for (i=0; i <= (ssize_t) MaxMap; i++)
700 grays[i].green=(~0U);
702 grays[i].alpha=(~0U);
703 grays[i].black=(~0U);
706 image_view=AcquireVirtualCacheView(image,exception);
707 #if defined(MAGICKCORE_OPENMP_SUPPORT)
708 #pragma omp parallel for schedule(static) shared(status) \
709 magick_number_threads(image,image,image->rows,1)
711 for (r=0; r < (ssize_t) image->rows; r++)
719 if (status == MagickFalse)
721 p=GetCacheViewVirtualPixels(image_view,0,r,image->columns,1,exception);
722 if (p == (
const Quantum *) NULL)
727 for (x=0; x < (ssize_t) image->columns; x++)
729 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
730 ScaleQuantumToMap(GetPixelRed(image,p));
731 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
732 ScaleQuantumToMap(GetPixelGreen(image,p));
733 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
734 ScaleQuantumToMap(GetPixelBlue(image,p));
735 if (image->colorspace == CMYKColorspace)
736 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
737 ScaleQuantumToMap(GetPixelBlack(image,p));
738 if (image->alpha_trait != UndefinedPixelTrait)
739 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
740 ScaleQuantumToMap(GetPixelAlpha(image,p));
741 p+=GetPixelChannels(image);
744 image_view=DestroyCacheView(image_view);
745 if (status == MagickFalse)
747 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
750 return(channel_features);
752 (void) memset(&gray,0,
sizeof(gray));
753 for (i=0; i <= (ssize_t) MaxMap; i++)
755 if (grays[i].red != ~0U)
756 grays[gray.red++].red=grays[i].red;
757 if (grays[i].green != ~0U)
758 grays[gray.green++].green=grays[i].green;
759 if (grays[i].blue != ~0U)
760 grays[gray.blue++].blue=grays[i].blue;
761 if (image->colorspace == CMYKColorspace)
762 if (grays[i].black != ~0U)
763 grays[gray.black++].black=grays[i].black;
764 if (image->alpha_trait != UndefinedPixelTrait)
765 if (grays[i].alpha != ~0U)
766 grays[gray.alpha++].alpha=grays[i].alpha;
771 number_grays=gray.red;
772 if (gray.green > number_grays)
773 number_grays=gray.green;
774 if (gray.blue > number_grays)
775 number_grays=gray.blue;
776 if (image->colorspace == CMYKColorspace)
777 if (gray.black > number_grays)
778 number_grays=gray.black;
779 if (image->alpha_trait != UndefinedPixelTrait)
780 if (gray.alpha > number_grays)
781 number_grays=gray.alpha;
783 sizeof(*cooccurrence));
785 2*
sizeof(*density_x));
787 2*
sizeof(*density_xy));
789 2*
sizeof(*density_y));
801 for (i=0; i < (ssize_t) number_grays; i++)
815 for (i=0; i < (ssize_t) number_grays; i++)
817 RelinquishMagickMemory(cooccurrence[i]);
821 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
824 (void) ThrowMagickException(exception,GetMagickModule(),
825 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
826 return(channel_features);
828 (void) memset(&correlation,0,
sizeof(correlation));
829 (void) memset(density_x,0,2*(number_grays+1)*
sizeof(*density_x));
830 (void) memset(density_xy,0,2*(number_grays+1)*
sizeof(*density_xy));
831 (void) memset(density_y,0,2*(number_grays+1)*
sizeof(*density_y));
832 (void) memset(&mean,0,
sizeof(mean));
833 (void) memset(sum,0,number_grays*
sizeof(*sum));
834 (void) memset(&sum_squares,0,
sizeof(sum_squares));
835 (void) memset(density_xy,0,2*number_grays*
sizeof(*density_xy));
836 (void) memset(&entropy_x,0,
sizeof(entropy_x));
837 (void) memset(&entropy_xy,0,
sizeof(entropy_xy));
838 (void) memset(&entropy_xy1,0,
sizeof(entropy_xy1));
839 (void) memset(&entropy_xy2,0,
sizeof(entropy_xy2));
840 (void) memset(&entropy_y,0,
sizeof(entropy_y));
841 (void) memset(&variance,0,
sizeof(variance));
842 for (i=0; i < (ssize_t) number_grays; i++)
845 sizeof(**cooccurrence));
850 (void) memset(cooccurrence[i],0,number_grays*
851 sizeof(**cooccurrence));
852 (void) memset(Q[i],0,number_grays*
sizeof(**Q));
854 if (i < (ssize_t) number_grays)
856 for (i--; i >= 0; i--)
862 RelinquishMagickMemory(cooccurrence[i]);
870 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
873 (void) ThrowMagickException(exception,GetMagickModule(),
874 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
875 return(channel_features);
881 image_view=AcquireVirtualCacheView(image,exception);
882 for (r=0; r < (ssize_t) image->rows; r++)
895 if (status == MagickFalse)
897 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,r,image->columns+
898 2*distance,distance+2,exception);
899 if (p == (
const Quantum *) NULL)
904 p+=distance*GetPixelChannels(image);;
905 for (x=0; x < (ssize_t) image->columns; x++)
907 for (i=0; i < 4; i++)
917 offset=(ssize_t) distance;
925 offset=(ssize_t) (image->columns+2*distance);
933 offset=(ssize_t) ((image->columns+2*distance)-distance);
941 offset=(ssize_t) ((image->columns+2*distance)+distance);
947 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
949 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
951 cooccurrence[u][v].direction[i].red++;
952 cooccurrence[v][u].direction[i].red++;
955 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
957 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
959 cooccurrence[u][v].direction[i].green++;
960 cooccurrence[v][u].direction[i].green++;
963 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
965 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
967 cooccurrence[u][v].direction[i].blue++;
968 cooccurrence[v][u].direction[i].blue++;
969 if (image->colorspace == CMYKColorspace)
973 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
975 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
977 cooccurrence[u][v].direction[i].black++;
978 cooccurrence[v][u].direction[i].black++;
980 if (image->alpha_trait != UndefinedPixelTrait)
984 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
986 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
988 cooccurrence[u][v].direction[i].alpha++;
989 cooccurrence[v][u].direction[i].alpha++;
992 p+=GetPixelChannels(image);
995 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
996 image_view=DestroyCacheView(image_view);
997 if (status == MagickFalse)
999 for (i=0; i < (ssize_t) number_grays; i++)
1001 RelinquishMagickMemory(cooccurrence[i]);
1005 (void) ThrowMagickException(exception,GetMagickModule(),
1006 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1007 return(channel_features);
1012 for (i=0; i < 4; i++)
1028 normalize=2.0*image->rows*(image->columns-distance);
1036 normalize=2.0*(image->rows-distance)*image->columns;
1044 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1052 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1056 normalize=PerceptibleReciprocal(normalize);
1057 for (y=0; y < (ssize_t) number_grays; y++)
1062 for (x=0; x < (ssize_t) number_grays; x++)
1064 cooccurrence[x][y].direction[i].red*=normalize;
1065 cooccurrence[x][y].direction[i].green*=normalize;
1066 cooccurrence[x][y].direction[i].blue*=normalize;
1067 if (image->colorspace == CMYKColorspace)
1068 cooccurrence[x][y].direction[i].black*=normalize;
1069 if (image->alpha_trait != UndefinedPixelTrait)
1070 cooccurrence[x][y].direction[i].alpha*=normalize;
1077 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1078 #pragma omp parallel for schedule(static) shared(status) \
1079 magick_number_threads(image,image,number_grays,1)
1081 for (i=0; i < 4; i++)
1086 for (y=0; y < (ssize_t) number_grays; y++)
1091 for (x=0; x < (ssize_t) number_grays; x++)
1096 channel_features[RedPixelChannel].angular_second_moment[i]+=
1097 cooccurrence[x][y].direction[i].red*
1098 cooccurrence[x][y].direction[i].red;
1099 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1100 cooccurrence[x][y].direction[i].green*
1101 cooccurrence[x][y].direction[i].green;
1102 channel_features[BluePixelChannel].angular_second_moment[i]+=
1103 cooccurrence[x][y].direction[i].blue*
1104 cooccurrence[x][y].direction[i].blue;
1105 if (image->colorspace == CMYKColorspace)
1106 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1107 cooccurrence[x][y].direction[i].black*
1108 cooccurrence[x][y].direction[i].black;
1109 if (image->alpha_trait != UndefinedPixelTrait)
1110 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1111 cooccurrence[x][y].direction[i].alpha*
1112 cooccurrence[x][y].direction[i].alpha;
1116 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1117 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1118 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1119 if (image->colorspace == CMYKColorspace)
1120 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1121 if (image->alpha_trait != UndefinedPixelTrait)
1122 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1123 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1124 correlation.direction[i].green+=x*y*
1125 cooccurrence[x][y].direction[i].green;
1126 correlation.direction[i].blue+=x*y*
1127 cooccurrence[x][y].direction[i].blue;
1128 if (image->colorspace == CMYKColorspace)
1129 correlation.direction[i].black+=x*y*
1130 cooccurrence[x][y].direction[i].black;
1131 if (image->alpha_trait != UndefinedPixelTrait)
1132 correlation.direction[i].alpha+=x*y*
1133 cooccurrence[x][y].direction[i].alpha;
1137 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1138 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1139 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1140 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1141 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1142 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1143 if (image->colorspace == CMYKColorspace)
1144 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1145 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1146 if (image->alpha_trait != UndefinedPixelTrait)
1147 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1148 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1152 density_xy[y+x+2].direction[i].red+=
1153 cooccurrence[x][y].direction[i].red;
1154 density_xy[y+x+2].direction[i].green+=
1155 cooccurrence[x][y].direction[i].green;
1156 density_xy[y+x+2].direction[i].blue+=
1157 cooccurrence[x][y].direction[i].blue;
1158 if (image->colorspace == CMYKColorspace)
1159 density_xy[y+x+2].direction[i].black+=
1160 cooccurrence[x][y].direction[i].black;
1161 if (image->alpha_trait != UndefinedPixelTrait)
1162 density_xy[y+x+2].direction[i].alpha+=
1163 cooccurrence[x][y].direction[i].alpha;
1167 channel_features[RedPixelChannel].entropy[i]-=
1168 cooccurrence[x][y].direction[i].red*
1169 MagickLog10(cooccurrence[x][y].direction[i].red);
1170 channel_features[GreenPixelChannel].entropy[i]-=
1171 cooccurrence[x][y].direction[i].green*
1172 MagickLog10(cooccurrence[x][y].direction[i].green);
1173 channel_features[BluePixelChannel].entropy[i]-=
1174 cooccurrence[x][y].direction[i].blue*
1175 MagickLog10(cooccurrence[x][y].direction[i].blue);
1176 if (image->colorspace == CMYKColorspace)
1177 channel_features[BlackPixelChannel].entropy[i]-=
1178 cooccurrence[x][y].direction[i].black*
1179 MagickLog10(cooccurrence[x][y].direction[i].black);
1180 if (image->alpha_trait != UndefinedPixelTrait)
1181 channel_features[AlphaPixelChannel].entropy[i]-=
1182 cooccurrence[x][y].direction[i].alpha*
1183 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1187 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1188 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1189 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1190 if (image->alpha_trait != UndefinedPixelTrait)
1191 density_x[x].direction[i].alpha+=
1192 cooccurrence[x][y].direction[i].alpha;
1193 if (image->colorspace == CMYKColorspace)
1194 density_x[x].direction[i].black+=
1195 cooccurrence[x][y].direction[i].black;
1196 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1197 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1198 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1199 if (image->colorspace == CMYKColorspace)
1200 density_y[y].direction[i].black+=
1201 cooccurrence[x][y].direction[i].black;
1202 if (image->alpha_trait != UndefinedPixelTrait)
1203 density_y[y].direction[i].alpha+=
1204 cooccurrence[x][y].direction[i].alpha;
1206 mean.direction[i].red+=y*sum[y].direction[i].red;
1207 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1208 mean.direction[i].green+=y*sum[y].direction[i].green;
1209 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1210 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1211 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1212 if (image->colorspace == CMYKColorspace)
1214 mean.direction[i].black+=y*sum[y].direction[i].black;
1215 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1217 if (image->alpha_trait != UndefinedPixelTrait)
1219 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1220 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1226 channel_features[RedPixelChannel].correlation[i]=
1227 (correlation.direction[i].red-mean.direction[i].red*
1228 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1229 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1230 sum_squares.direction[i].red-(mean.direction[i].red*
1231 mean.direction[i].red)));
1232 channel_features[GreenPixelChannel].correlation[i]=
1233 (correlation.direction[i].green-mean.direction[i].green*
1234 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1235 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1236 sum_squares.direction[i].green-(mean.direction[i].green*
1237 mean.direction[i].green)));
1238 channel_features[BluePixelChannel].correlation[i]=
1239 (correlation.direction[i].blue-mean.direction[i].blue*
1240 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1241 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1242 sum_squares.direction[i].blue-(mean.direction[i].blue*
1243 mean.direction[i].blue)));
1244 if (image->colorspace == CMYKColorspace)
1245 channel_features[BlackPixelChannel].correlation[i]=
1246 (correlation.direction[i].black-mean.direction[i].black*
1247 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1248 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1249 sum_squares.direction[i].black-(mean.direction[i].black*
1250 mean.direction[i].black)));
1251 if (image->alpha_trait != UndefinedPixelTrait)
1252 channel_features[AlphaPixelChannel].correlation[i]=
1253 (correlation.direction[i].alpha-mean.direction[i].alpha*
1254 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1255 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1256 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1257 mean.direction[i].alpha)));
1262 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1263 #pragma omp parallel for schedule(static) shared(status) \
1264 magick_number_threads(image,image,number_grays,1)
1266 for (i=0; i < 4; i++)
1271 for (x=2; x < (ssize_t) (2*number_grays); x++)
1276 channel_features[RedPixelChannel].sum_average[i]+=
1277 x*density_xy[x].direction[i].red;
1278 channel_features[GreenPixelChannel].sum_average[i]+=
1279 x*density_xy[x].direction[i].green;
1280 channel_features[BluePixelChannel].sum_average[i]+=
1281 x*density_xy[x].direction[i].blue;
1282 if (image->colorspace == CMYKColorspace)
1283 channel_features[BlackPixelChannel].sum_average[i]+=
1284 x*density_xy[x].direction[i].black;
1285 if (image->alpha_trait != UndefinedPixelTrait)
1286 channel_features[AlphaPixelChannel].sum_average[i]+=
1287 x*density_xy[x].direction[i].alpha;
1291 channel_features[RedPixelChannel].sum_entropy[i]-=
1292 density_xy[x].direction[i].red*
1293 MagickLog10(density_xy[x].direction[i].red);
1294 channel_features[GreenPixelChannel].sum_entropy[i]-=
1295 density_xy[x].direction[i].green*
1296 MagickLog10(density_xy[x].direction[i].green);
1297 channel_features[BluePixelChannel].sum_entropy[i]-=
1298 density_xy[x].direction[i].blue*
1299 MagickLog10(density_xy[x].direction[i].blue);
1300 if (image->colorspace == CMYKColorspace)
1301 channel_features[BlackPixelChannel].sum_entropy[i]-=
1302 density_xy[x].direction[i].black*
1303 MagickLog10(density_xy[x].direction[i].black);
1304 if (image->alpha_trait != UndefinedPixelTrait)
1305 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1306 density_xy[x].direction[i].alpha*
1307 MagickLog10(density_xy[x].direction[i].alpha);
1311 channel_features[RedPixelChannel].sum_variance[i]+=
1312 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1313 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1314 density_xy[x].direction[i].red;
1315 channel_features[GreenPixelChannel].sum_variance[i]+=
1316 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1317 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1318 density_xy[x].direction[i].green;
1319 channel_features[BluePixelChannel].sum_variance[i]+=
1320 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1321 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1322 density_xy[x].direction[i].blue;
1323 if (image->colorspace == CMYKColorspace)
1324 channel_features[BlackPixelChannel].sum_variance[i]+=
1325 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1326 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1327 density_xy[x].direction[i].black;
1328 if (image->alpha_trait != UndefinedPixelTrait)
1329 channel_features[AlphaPixelChannel].sum_variance[i]+=
1330 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1331 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1332 density_xy[x].direction[i].alpha;
1338 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1339 #pragma omp parallel for schedule(static) shared(status) \
1340 magick_number_threads(image,image,number_grays,1)
1342 for (i=0; i < 4; i++)
1347 for (y=0; y < (ssize_t) number_grays; y++)
1352 for (x=0; x < (ssize_t) number_grays; x++)
1357 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1358 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1359 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1360 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1361 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1362 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1363 if (image->colorspace == CMYKColorspace)
1364 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1365 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1366 if (image->alpha_trait != UndefinedPixelTrait)
1367 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1368 (y-mean.direction[i].alpha+1)*
1369 cooccurrence[x][y].direction[i].alpha;
1373 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1374 cooccurrence[x][y].direction[i].red;
1375 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1376 cooccurrence[x][y].direction[i].green;
1377 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1378 cooccurrence[x][y].direction[i].blue;
1379 if (image->colorspace == CMYKColorspace)
1380 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1381 cooccurrence[x][y].direction[i].black;
1382 if (image->alpha_trait != UndefinedPixelTrait)
1383 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1384 cooccurrence[x][y].direction[i].alpha;
1388 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1389 MagickLog10(cooccurrence[x][y].direction[i].red);
1390 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1391 MagickLog10(cooccurrence[x][y].direction[i].green);
1392 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1393 MagickLog10(cooccurrence[x][y].direction[i].blue);
1394 if (image->colorspace == CMYKColorspace)
1395 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1396 MagickLog10(cooccurrence[x][y].direction[i].black);
1397 if (image->alpha_trait != UndefinedPixelTrait)
1398 entropy_xy.direction[i].alpha-=
1399 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1400 cooccurrence[x][y].direction[i].alpha);
1401 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1402 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1403 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1404 MagickLog10(density_x[x].direction[i].green*
1405 density_y[y].direction[i].green));
1406 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1407 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1408 if (image->colorspace == CMYKColorspace)
1409 entropy_xy1.direction[i].black-=(
1410 cooccurrence[x][y].direction[i].black*MagickLog10(
1411 density_x[x].direction[i].black*density_y[y].direction[i].black));
1412 if (image->alpha_trait != UndefinedPixelTrait)
1413 entropy_xy1.direction[i].alpha-=(
1414 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1415 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1416 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1417 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1418 density_y[y].direction[i].red));
1419 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1420 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1421 density_y[y].direction[i].green));
1422 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1423 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1424 density_y[y].direction[i].blue));
1425 if (image->colorspace == CMYKColorspace)
1426 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1427 density_y[y].direction[i].black*MagickLog10(
1428 density_x[x].direction[i].black*density_y[y].direction[i].black));
1429 if (image->alpha_trait != UndefinedPixelTrait)
1430 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1431 density_y[y].direction[i].alpha*MagickLog10(
1432 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1435 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1436 variance.direction[i].red;
1437 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1438 variance.direction[i].green;
1439 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1440 variance.direction[i].blue;
1441 if (image->colorspace == CMYKColorspace)
1442 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1443 variance.direction[i].black;
1444 if (image->alpha_trait != UndefinedPixelTrait)
1445 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1446 variance.direction[i].alpha;
1451 (void) memset(&variance,0,
sizeof(variance));
1452 (void) memset(&sum_squares,0,
sizeof(sum_squares));
1453 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1454 #pragma omp parallel for schedule(static) shared(status) \
1455 magick_number_threads(image,image,number_grays,1)
1457 for (i=0; i < 4; i++)
1462 for (x=0; x < (ssize_t) number_grays; x++)
1467 variance.direction[i].red+=density_xy[x].direction[i].red;
1468 variance.direction[i].green+=density_xy[x].direction[i].green;
1469 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1470 if (image->colorspace == CMYKColorspace)
1471 variance.direction[i].black+=density_xy[x].direction[i].black;
1472 if (image->alpha_trait != UndefinedPixelTrait)
1473 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1474 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1475 density_xy[x].direction[i].red;
1476 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1477 density_xy[x].direction[i].green;
1478 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1479 density_xy[x].direction[i].blue;
1480 if (image->colorspace == CMYKColorspace)
1481 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1482 density_xy[x].direction[i].black;
1483 if (image->alpha_trait != UndefinedPixelTrait)
1484 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1485 density_xy[x].direction[i].alpha;
1489 channel_features[RedPixelChannel].difference_entropy[i]-=
1490 density_xy[x].direction[i].red*
1491 MagickLog10(density_xy[x].direction[i].red);
1492 channel_features[GreenPixelChannel].difference_entropy[i]-=
1493 density_xy[x].direction[i].green*
1494 MagickLog10(density_xy[x].direction[i].green);
1495 channel_features[BluePixelChannel].difference_entropy[i]-=
1496 density_xy[x].direction[i].blue*
1497 MagickLog10(density_xy[x].direction[i].blue);
1498 if (image->colorspace == CMYKColorspace)
1499 channel_features[BlackPixelChannel].difference_entropy[i]-=
1500 density_xy[x].direction[i].black*
1501 MagickLog10(density_xy[x].direction[i].black);
1502 if (image->alpha_trait != UndefinedPixelTrait)
1503 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1504 density_xy[x].direction[i].alpha*
1505 MagickLog10(density_xy[x].direction[i].alpha);
1509 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1510 MagickLog10(density_x[x].direction[i].red));
1511 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1512 MagickLog10(density_x[x].direction[i].green));
1513 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1514 MagickLog10(density_x[x].direction[i].blue));
1515 if (image->colorspace == CMYKColorspace)
1516 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1517 MagickLog10(density_x[x].direction[i].black));
1518 if (image->alpha_trait != UndefinedPixelTrait)
1519 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1520 MagickLog10(density_x[x].direction[i].alpha));
1521 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1522 MagickLog10(density_y[x].direction[i].red));
1523 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1524 MagickLog10(density_y[x].direction[i].green));
1525 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1526 MagickLog10(density_y[x].direction[i].blue));
1527 if (image->colorspace == CMYKColorspace)
1528 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1529 MagickLog10(density_y[x].direction[i].black));
1530 if (image->alpha_trait != UndefinedPixelTrait)
1531 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1532 MagickLog10(density_y[x].direction[i].alpha));
1537 channel_features[RedPixelChannel].difference_variance[i]=
1538 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1539 (variance.direction[i].red*variance.direction[i].red))/
1540 ((double) number_grays*number_grays*number_grays*number_grays);
1541 channel_features[GreenPixelChannel].difference_variance[i]=
1542 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1543 (variance.direction[i].green*variance.direction[i].green))/
1544 ((double) number_grays*number_grays*number_grays*number_grays);
1545 channel_features[BluePixelChannel].difference_variance[i]=
1546 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1547 (variance.direction[i].blue*variance.direction[i].blue))/
1548 ((double) number_grays*number_grays*number_grays*number_grays);
1549 if (image->colorspace == CMYKColorspace)
1550 channel_features[BlackPixelChannel].difference_variance[i]=
1551 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1552 (variance.direction[i].black*variance.direction[i].black))/
1553 ((double) number_grays*number_grays*number_grays*number_grays);
1554 if (image->alpha_trait != UndefinedPixelTrait)
1555 channel_features[AlphaPixelChannel].difference_variance[i]=
1556 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1557 (variance.direction[i].alpha*variance.direction[i].alpha))/
1558 ((double) number_grays*number_grays*number_grays*number_grays);
1562 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1563 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1564 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1565 entropy_x.direction[i].red : entropy_y.direction[i].red);
1566 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1567 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1568 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1569 entropy_x.direction[i].green : entropy_y.direction[i].green);
1570 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1571 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1572 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1573 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1574 if (image->colorspace == CMYKColorspace)
1575 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1576 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1577 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1578 entropy_x.direction[i].black : entropy_y.direction[i].black);
1579 if (image->alpha_trait != UndefinedPixelTrait)
1580 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1581 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1582 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1583 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1584 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1585 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].red-
1586 entropy_xy.direction[i].red)))));
1587 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1588 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].green-
1589 entropy_xy.direction[i].green)))));
1590 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1591 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].blue-
1592 entropy_xy.direction[i].blue)))));
1593 if (image->colorspace == CMYKColorspace)
1594 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1595 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].black-
1596 entropy_xy.direction[i].black)))));
1597 if (image->alpha_trait != UndefinedPixelTrait)
1598 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1599 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].alpha-
1600 entropy_xy.direction[i].alpha)))));
1605 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1606 #pragma omp parallel for schedule(static) shared(status) \
1607 magick_number_threads(image,image,number_grays,1)
1609 for (i=0; i < 4; i++)
1614 for (z=0; z < (ssize_t) number_grays; z++)
1622 (void) memset(&pixel,0,
sizeof(pixel));
1623 for (y=0; y < (ssize_t) number_grays; y++)
1628 for (x=0; x < (ssize_t) number_grays; x++)
1633 if (((y-x) == z) || ((x-y) == z))
1635 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1636 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1637 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1638 if (image->colorspace == CMYKColorspace)
1639 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1640 if (image->alpha_trait != UndefinedPixelTrait)
1641 pixel.direction[i].alpha+=
1642 cooccurrence[x][y].direction[i].alpha;
1647 if ((fabs(density_x[z].direction[i].red) > MagickEpsilon) &&
1648 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1649 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1650 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1651 density_y[x].direction[i].red;
1652 if ((fabs(density_x[z].direction[i].green) > MagickEpsilon) &&
1653 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1654 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1655 cooccurrence[y][x].direction[i].green/
1656 density_x[z].direction[i].green/density_y[x].direction[i].red;
1657 if ((fabs(density_x[z].direction[i].blue) > MagickEpsilon) &&
1658 (fabs(density_y[x].direction[i].blue) > MagickEpsilon))
1659 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1660 cooccurrence[y][x].direction[i].blue/
1661 density_x[z].direction[i].blue/density_y[x].direction[i].blue;
1662 if (image->colorspace == CMYKColorspace)
1663 if ((fabs(density_x[z].direction[i].black) > MagickEpsilon) &&
1664 (fabs(density_y[x].direction[i].black) > MagickEpsilon))
1665 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1666 cooccurrence[y][x].direction[i].black/
1667 density_x[z].direction[i].black/density_y[x].direction[i].black;
1668 if (image->alpha_trait != UndefinedPixelTrait)
1669 if ((fabs(density_x[z].direction[i].alpha) > MagickEpsilon) &&
1670 (fabs(density_y[x].direction[i].alpha) > MagickEpsilon))
1671 Q[z][y].direction[i].alpha+=
1672 cooccurrence[z][x].direction[i].alpha*
1673 cooccurrence[y][x].direction[i].alpha/
1674 density_x[z].direction[i].alpha/
1675 density_y[x].direction[i].alpha;
1678 channel_features[RedPixelChannel].contrast[i]+=z*z*
1679 pixel.direction[i].red;
1680 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1681 pixel.direction[i].green;
1682 channel_features[BluePixelChannel].contrast[i]+=z*z*
1683 pixel.direction[i].blue;
1684 if (image->colorspace == CMYKColorspace)
1685 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1686 pixel.direction[i].black;
1687 if (image->alpha_trait != UndefinedPixelTrait)
1688 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1689 pixel.direction[i].alpha;
1695 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1696 sqrt((
double) -1.0);
1697 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1698 sqrt((
double) -1.0);
1699 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1700 sqrt((
double) -1.0);
1701 if (image->colorspace == CMYKColorspace)
1702 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1703 sqrt((
double) -1.0);
1704 if (image->alpha_trait != UndefinedPixelTrait)
1705 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1706 sqrt((
double) -1.0);
1712 for (i=0; i < (ssize_t) number_grays; i++)
1718 for (i=0; i < (ssize_t) number_grays; i++)
1720 RelinquishMagickMemory(cooccurrence[i]);
1722 return(channel_features);
1765 static inline double MagickRound(
double x)
1770 if ((x-floor(x)) < (ceil(x)-x))
1775 static Image *RenderHoughLines(
const ImageInfo *image_info,
const size_t columns,
1778 #define BoundingBox "viewbox"
1792 image=AcquireImage(image_info,exception);
1793 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1794 if (status == MagickFalse)
1796 image=DestroyImageList(image);
1797 return((
Image *) NULL);
1799 image->columns=columns;
1801 draw_info=CloneDrawInfo(image_info,(
DrawInfo *) NULL);
1802 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
1804 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
1806 image->columns=(size_t) (draw_info->affine.sx*image->columns);
1807 image->rows=(size_t) (draw_info->affine.sy*image->rows);
1808 status=SetImageExtent(image,image->columns,image->rows,exception);
1809 if (status == MagickFalse)
1810 return(DestroyImageList(image));
1811 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1813 image=DestroyImageList(image);
1814 return((
Image *) NULL);
1819 if (GetBlobStreamData(image) == (
unsigned char *) NULL)
1820 draw_info->primitive=FileToString(image->filename,~0UL,exception);
1823 draw_info->primitive=(
char *) AcquireQuantumMemory(1,(
size_t)
1824 GetBlobSize(image)+1);
1825 if (draw_info->primitive != (
char *) NULL)
1827 (void) memcpy(draw_info->primitive,GetBlobStreamData(image),
1828 (size_t) GetBlobSize(image));
1829 draw_info->primitive[GetBlobSize(image)]=
'\0';
1832 (void) DrawImage(image,draw_info,exception);
1833 draw_info=DestroyDrawInfo(draw_info);
1834 (void) CloseBlob(image);
1835 return(GetFirstImageInList(image));
1838 MagickExport
Image *HoughLineImage(
const Image *image,
const size_t width,
1839 const size_t height,
const size_t threshold,
ExceptionInfo *exception)
1841 #define HoughLineImageTag "HoughLine/Image"
1847 message[MagickPathExtent],
1848 path[MagickPathExtent];
1857 *lines_image = NULL;
1888 assert(image != (
const Image *) NULL);
1889 assert(image->signature == MagickCoreSignature);
1891 assert(exception->signature == MagickCoreSignature);
1892 if (IsEventLogging() != MagickFalse)
1893 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1894 accumulator_width=180;
1895 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1896 image->rows : image->columns))/2.0);
1897 accumulator_height=(size_t) (2.0*hough_height);
1898 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1899 sizeof(
double),exception);
1901 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1902 if (NullMatrix(accumulator) == MagickFalse)
1904 accumulator=DestroyMatrixInfo(accumulator);
1905 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1912 center.x=(double) image->columns/2.0;
1913 center.y=(
double) image->rows/2.0;
1914 image_view=AcquireVirtualCacheView(image,exception);
1915 for (y=0; y < (ssize_t) image->rows; y++)
1923 if (status == MagickFalse)
1925 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1926 if (p == (Quantum *) NULL)
1931 for (x=0; x < (ssize_t) image->columns; x++)
1933 if (GetPixelIntensity(image,p) > (QuantumRange/2.0))
1938 for (i=0; i < 180; i++)
1944 radius=(((double) x-center.x)*cos(DegreesToRadians((
double) i)))+
1945 (((
double) y-center.y)*sin(DegreesToRadians((
double) i)));
1946 (void) GetMatrixElement(accumulator,i,(ssize_t)
1947 MagickRound(radius+hough_height),&count);
1949 (void) SetMatrixElement(accumulator,i,(ssize_t)
1950 MagickRound(radius+hough_height),&count);
1953 p+=GetPixelChannels(image);
1955 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1960 #if defined(MAGICKCORE_OPENMP_SUPPORT)
1964 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
1965 if (proceed == MagickFalse)
1969 image_view=DestroyCacheView(image_view);
1970 if (status == MagickFalse)
1972 accumulator=DestroyMatrixInfo(accumulator);
1973 return((
Image *) NULL);
1978 file=AcquireUniqueFileResource(path);
1981 accumulator=DestroyMatrixInfo(accumulator);
1982 return((
Image *) NULL);
1984 (void) FormatLocaleString(message,MagickPathExtent,
1985 "# Hough line transform: %.20gx%.20g%+.20g\n",(
double) width,
1986 (double) height,(
double) threshold);
1987 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1989 (void) FormatLocaleString(message,MagickPathExtent,
1990 "viewbox 0 0 %.20g %.20g\n",(
double) image->columns,(double) image->rows);
1991 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1993 (void) FormatLocaleString(message,MagickPathExtent,
1994 "# x1,y1 x2,y2 # count angle distance\n");
1995 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1997 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1999 line_count=threshold;
2000 for (y=0; y < (ssize_t) accumulator_height; y++)
2005 for (x=0; x < (ssize_t) accumulator_width; x++)
2010 (void) GetMatrixElement(accumulator,x,y,&count);
2011 if (count >= (
double) line_count)
2026 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2031 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2033 if ((u != 0) || (v !=0))
2035 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
2043 if (u < (ssize_t) (width/2))
2046 (void) GetMatrixElement(accumulator,x,y,&count);
2049 if ((x >= 45) && (x <= 135))
2055 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
2056 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2057 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2058 line.x2=(double) image->columns;
2059 line.y2=((
double) (y-(accumulator_height/2.0))-((line.x2-
2060 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2061 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2069 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
2070 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2071 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2072 line.y2=(double) image->rows;
2073 line.x2=((
double) (y-(accumulator_height/2.0))-((line.y2-
2074 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2075 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2077 (void) FormatLocaleString(message,MagickPathExtent,
2078 "line %g,%g %g,%g # %g %g %g\n",line.x1,line.y1,line.x2,line.y2,
2079 maxima,(
double) x,(double) y);
2080 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2089 image_info=AcquireImageInfo();
2090 image_info->background_color=image->background_color;
2091 (void) FormatLocaleString(image_info->filename,MagickPathExtent,
"%s",path);
2092 artifact=GetImageArtifact(image,
"background");
2093 if (artifact != (
const char *) NULL)
2094 (void) SetImageOption(image_info,
"background",artifact);
2095 artifact=GetImageArtifact(image,
"fill");
2096 if (artifact != (
const char *) NULL)
2097 (void) SetImageOption(image_info,
"fill",artifact);
2098 artifact=GetImageArtifact(image,
"stroke");
2099 if (artifact != (
const char *) NULL)
2100 (void) SetImageOption(image_info,
"stroke",artifact);
2101 artifact=GetImageArtifact(image,
"strokewidth");
2102 if (artifact != (
const char *) NULL)
2103 (void) SetImageOption(image_info,
"strokewidth",artifact);
2104 lines_image=RenderHoughLines(image_info,image->columns,image->rows,exception);
2105 artifact=GetImageArtifact(image,
"hough-lines:accumulator");
2106 if ((lines_image != (
Image *) NULL) &&
2107 (IsStringTrue(artifact) != MagickFalse))
2112 accumulator_image=MatrixToImage(accumulator,exception);
2113 if (accumulator_image != (
Image *) NULL)
2114 AppendImageToList(&lines_image,accumulator_image);
2119 accumulator=DestroyMatrixInfo(accumulator);
2120 image_info=DestroyImageInfo(image_info);
2121 (void) RelinquishUniqueFileResource(path);
2122 return(GetFirstImageInList(lines_image));
2165 MagickExport
Image *MeanShiftImage(
const Image *image,
const size_t width,
2166 const size_t height,
const double color_distance,
ExceptionInfo *exception)
2168 #define MaxMeanShiftIterations 100
2169 #define MeanShiftImageTag "MeanShift/Image"
2188 assert(image != (
const Image *) NULL);
2189 assert(image->signature == MagickCoreSignature);
2191 assert(exception->signature == MagickCoreSignature);
2192 if (IsEventLogging() != MagickFalse)
2193 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2194 mean_image=CloneImage(image,0,0,MagickTrue,exception);
2195 if (mean_image == (
Image *) NULL)
2196 return((
Image *) NULL);
2197 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2199 mean_image=DestroyImage(mean_image);
2200 return((
Image *) NULL);
2204 image_view=AcquireVirtualCacheView(image,exception);
2205 pixel_view=AcquireVirtualCacheView(image,exception);
2206 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2207 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2208 #pragma omp parallel for schedule(static) shared(status,progress) \
2209 magick_number_threads(mean_image,mean_image,mean_image->rows,1)
2211 for (y=0; y < (ssize_t) mean_image->rows; y++)
2222 if (status == MagickFalse)
2224 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2225 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2227 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2232 for (x=0; x < (ssize_t) mean_image->columns; x++)
2245 GetPixelInfo(image,&mean_pixel);
2246 GetPixelInfoPixel(image,p,&mean_pixel);
2247 mean_location.x=(double) x;
2248 mean_location.y=(double) y;
2249 for (i=0; i < MaxMeanShiftIterations; i++)
2267 GetPixelInfo(image,&sum_pixel);
2268 previous_location=mean_location;
2269 previous_pixel=mean_pixel;
2271 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2276 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2278 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2283 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2284 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2285 mean_location.y+v),&pixel,exception);
2286 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2287 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2288 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2289 if (distance <= (color_distance*color_distance))
2291 sum_location.x+=mean_location.x+u;
2292 sum_location.y+=mean_location.y+v;
2293 sum_pixel.red+=pixel.red;
2294 sum_pixel.green+=pixel.green;
2295 sum_pixel.blue+=pixel.blue;
2296 sum_pixel.alpha+=pixel.alpha;
2302 gamma=PerceptibleReciprocal(count);
2303 mean_location.x=gamma*sum_location.x;
2304 mean_location.y=gamma*sum_location.y;
2305 mean_pixel.red=gamma*sum_pixel.red;
2306 mean_pixel.green=gamma*sum_pixel.green;
2307 mean_pixel.blue=gamma*sum_pixel.blue;
2308 mean_pixel.alpha=gamma*sum_pixel.alpha;
2309 distance=(mean_location.x-previous_location.x)*
2310 (mean_location.x-previous_location.x)+
2311 (mean_location.y-previous_location.y)*
2312 (mean_location.y-previous_location.y)+
2313 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2314 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2315 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2316 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2317 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2318 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2319 if (distance <= 3.0)
2322 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2323 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2324 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2325 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2326 p+=GetPixelChannels(image);
2327 q+=GetPixelChannels(mean_image);
2329 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2331 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2336 #if defined(MAGICKCORE_OPENMP_SUPPORT)
2340 proceed=SetImageProgress(image,MeanShiftImageTag,progress,image->rows);
2341 if (proceed == MagickFalse)
2345 mean_view=DestroyCacheView(mean_view);
2346 pixel_view=DestroyCacheView(pixel_view);
2347 image_view=DestroyCacheView(image_view);