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/utility-private.h"
98#include "MagickCore/version.h"
150static inline MagickBooleanType IsAuthenticPixel(
const Image *image,
151 const ssize_t x,
const ssize_t y)
153 if ((x < 0) || (x >= (ssize_t) image->columns))
155 if ((y < 0) || (y >= (ssize_t) image->rows))
160static MagickBooleanType TraceEdges(
Image *edge_image,
CacheView *edge_view,
161 MatrixInfo *canny_cache,
const ssize_t x,
const ssize_t y,
177 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
178 if (q == (Quantum *) NULL)
181 status=SyncCacheViewAuthenticPixels(edge_view,exception);
182 if (status == MagickFalse)
184 if (GetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
188 if (SetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
196 status=GetMatrixElement(canny_cache,i,0,&edge);
197 if (status == MagickFalse)
199 for (v=(-1); v <= 1; v++)
204 for (u=(-1); u <= 1; u++)
206 if ((u == 0) && (v == 0))
208 if (IsAuthenticPixel(edge_image,edge.x+u,edge.y+v) == MagickFalse)
213 q=GetCacheViewAuthenticPixels(edge_view,edge.x+u,edge.y+v,1,1,
215 if (q == (Quantum *) NULL)
217 status=GetMatrixElement(canny_cache,edge.x+u,edge.y+v,&pixel);
218 if (status == MagickFalse)
220 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
221 (pixel.intensity >= lower_threshold))
224 status=SyncCacheViewAuthenticPixels(edge_view,exception);
225 if (status == MagickFalse)
229 status=SetMatrixElement(canny_cache,i,0,&edge);
230 if (status == MagickFalse)
240MagickExport
Image *CannyEdgeImage(
const Image *image,
const double radius,
241 const double sigma,
const double lower_percent,
const double upper_percent,
244#define CannyEdgeImageTag "CannyEdge/Image"
253 geometry[MagickPathExtent];
279 assert(image != (
const Image *) NULL);
280 assert(image->signature == MagickCoreSignature);
282 assert(exception->signature == MagickCoreSignature);
283 if (IsEventLogging() != MagickFalse)
284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
288 (void) FormatLocaleString(geometry,MagickPathExtent,
289 "blur:%.20gx%.20g;blur:%.20gx%.20g+90",radius,sigma,radius,sigma);
290 kernel_info=AcquireKernelInfo(geometry,exception);
292 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
293 edge_image=MorphologyImage(image,ConvolveMorphology,1,kernel_info,exception);
294 kernel_info=DestroyKernelInfo(kernel_info);
295 if (edge_image == (
Image *) NULL)
296 return((
Image *) NULL);
297 if (TransformImageColorspace(edge_image,GRAYColorspace,exception) == MagickFalse)
299 edge_image=DestroyImage(edge_image);
300 return((
Image *) NULL);
302 (void) SetImageAlphaChannel(edge_image,OffAlphaChannel,exception);
306 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
310 edge_image=DestroyImage(edge_image);
311 return((
Image *) NULL);
314 edge_view=AcquireVirtualCacheView(edge_image,exception);
315#if defined(MAGICKCORE_OPENMP_SUPPORT)
316 #pragma omp parallel for schedule(static) shared(status) \
317 magick_number_threads(edge_image,edge_image,edge_image->rows,1)
319 for (y=0; y < (ssize_t) edge_image->rows; y++)
327 if (status == MagickFalse)
329 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
331 if (p == (
const Quantum *) NULL)
336 for (x=0; x < (ssize_t) edge_image->columns; x++)
346 *magick_restrict kernel_pixels;
363 (void) memset(&pixel,0,
sizeof(pixel));
367 for (v=0; v < 2; v++)
372 for (u=0; u < 2; u++)
377 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
378 dx+=0.5*Gx[v][u]*intensity;
379 dy+=0.5*Gy[v][u]*intensity;
381 kernel_pixels+=edge_image->columns+1;
383 pixel.magnitude=hypot(dx,dy);
385 if (fabs(dx) > MagickEpsilon)
393 if (slope < -2.41421356237)
396 if (slope < -0.414213562373)
403 if (slope > 2.41421356237)
406 if (slope > 0.414213562373)
412 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
414 p+=(ptrdiff_t) GetPixelChannels(edge_image);
417 edge_view=DestroyCacheView(edge_view);
423 (void) GetMatrixElement(canny_cache,0,0,&element);
424 max=element.intensity;
425 min=element.intensity;
426 edge_view=AcquireAuthenticCacheView(edge_image,exception);
427#if defined(MAGICKCORE_OPENMP_SUPPORT)
428 #pragma omp parallel for schedule(static) shared(status) \
429 magick_number_threads(edge_image,edge_image,edge_image->rows,1)
431 for (y=0; y < (ssize_t) edge_image->rows; y++)
439 if (status == MagickFalse)
441 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
443 if (q == (Quantum *) NULL)
448 for (x=0; x < (ssize_t) edge_image->columns; x++)
455 (void) GetMatrixElement(canny_cache,x,y,&pixel);
456 switch (pixel.orientation)
464 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
465 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
473 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
474 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
482 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
483 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
491 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
492 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
496 pixel.intensity=pixel.magnitude;
497 if ((pixel.magnitude < alpha_pixel.magnitude) ||
498 (pixel.magnitude < beta_pixel.magnitude))
500 (void) SetMatrixElement(canny_cache,x,y,&pixel);
501#if defined(MAGICKCORE_OPENMP_SUPPORT)
502 #pragma omp critical (MagickCore_CannyEdgeImage)
505 if (pixel.intensity < min)
507 if (pixel.intensity > max)
511 q+=(ptrdiff_t) GetPixelChannels(edge_image);
513 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
516 edge_view=DestroyCacheView(edge_view);
520 lower_threshold=lower_percent*(max-min)+min;
521 upper_threshold=upper_percent*(max-min)+min;
525 edge_view=AcquireAuthenticCacheView(edge_image,exception);
526 for (y=0; y < (ssize_t) edge_image->rows; y++)
531 if (status == MagickFalse)
533 for (x=0; x < (ssize_t) edge_image->columns; x++)
544 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
545 if (p == (
const Quantum *) NULL)
547 status=GetMatrixElement(canny_cache,x,y,&pixel);
548 if (status == MagickFalse)
550 if ((GetPixelIntensity(edge_image,p) == 0.0) &&
551 (pixel.intensity >= upper_threshold))
552 status=TraceEdges(edge_image,edge_view,canny_cache,x,y,lower_threshold,
555 if (image->progress_monitor != (MagickProgressMonitor) NULL)
560#if defined(MAGICKCORE_OPENMP_SUPPORT)
564 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
565 if (proceed == MagickFalse)
569 edge_view=DestroyCacheView(edge_view);
573 canny_cache=DestroyMatrixInfo(canny_cache);
665 assert(image != (
Image *) NULL);
666 assert(image->signature == MagickCoreSignature);
667 if (IsEventLogging() != MagickFalse)
668 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
669 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
671 length=MaxPixelChannels+1UL;
673 sizeof(*channel_features));
676 (void) ThrowMagickException(exception,GetMagickModule(),
677 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
678 return(channel_features);
680 (void) memset(channel_features,0,length*
681 sizeof(*channel_features));
685 grays=(
PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,
sizeof(*grays));
690 (void) ThrowMagickException(exception,GetMagickModule(),
691 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
692 return(channel_features);
694 for (i=0; i <= (ssize_t) MaxMap; i++)
697 grays[i].green=(~0U);
699 grays[i].alpha=(~0U);
700 grays[i].black=(~0U);
703 image_view=AcquireVirtualCacheView(image,exception);
704#if defined(MAGICKCORE_OPENMP_SUPPORT)
705 #pragma omp parallel for schedule(static) shared(status) \
706 magick_number_threads(image,image,image->rows,1)
708 for (r=0; r < (ssize_t) image->rows; r++)
716 if (status == MagickFalse)
718 p=GetCacheViewVirtualPixels(image_view,0,r,image->columns,1,exception);
719 if (p == (
const Quantum *) NULL)
724 for (x=0; x < (ssize_t) image->columns; x++)
726 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
727 ScaleQuantumToMap(GetPixelRed(image,p));
728 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
729 ScaleQuantumToMap(GetPixelGreen(image,p));
730 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
731 ScaleQuantumToMap(GetPixelBlue(image,p));
732 if (image->colorspace == CMYKColorspace)
733 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
734 ScaleQuantumToMap(GetPixelBlack(image,p));
735 if (image->alpha_trait != UndefinedPixelTrait)
736 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
737 ScaleQuantumToMap(GetPixelAlpha(image,p));
738 p+=(ptrdiff_t) GetPixelChannels(image);
741 image_view=DestroyCacheView(image_view);
742 if (status == MagickFalse)
744 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
747 return(channel_features);
749 (void) memset(&gray,0,
sizeof(gray));
750 for (i=0; i <= (ssize_t) MaxMap; i++)
752 if (grays[i].red != ~0U)
753 grays[gray.red++].red=grays[i].red;
754 if (grays[i].green != ~0U)
755 grays[gray.green++].green=grays[i].green;
756 if (grays[i].blue != ~0U)
757 grays[gray.blue++].blue=grays[i].blue;
758 if (image->colorspace == CMYKColorspace)
759 if (grays[i].black != ~0U)
760 grays[gray.black++].black=grays[i].black;
761 if (image->alpha_trait != UndefinedPixelTrait)
762 if (grays[i].alpha != ~0U)
763 grays[gray.alpha++].alpha=grays[i].alpha;
768 number_grays=gray.red;
769 if (gray.green > number_grays)
770 number_grays=gray.green;
771 if (gray.blue > number_grays)
772 number_grays=gray.blue;
773 if (image->colorspace == CMYKColorspace)
774 if (gray.black > number_grays)
775 number_grays=gray.black;
776 if (image->alpha_trait != UndefinedPixelTrait)
777 if (gray.alpha > number_grays)
778 number_grays=gray.alpha;
780 sizeof(*cooccurrence));
782 2*
sizeof(*density_x));
784 2*
sizeof(*density_xy));
786 2*
sizeof(*density_y));
798 for (i=0; i < (ssize_t) number_grays; i++)
812 for (i=0; i < (ssize_t) number_grays; i++)
814 RelinquishMagickMemory(cooccurrence[i]);
818 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
821 (void) ThrowMagickException(exception,GetMagickModule(),
822 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
823 return(channel_features);
825 (void) memset(&correlation,0,
sizeof(correlation));
826 (void) memset(density_x,0,2*(number_grays+1)*
sizeof(*density_x));
827 (void) memset(density_xy,0,2*(number_grays+1)*
sizeof(*density_xy));
828 (void) memset(density_y,0,2*(number_grays+1)*
sizeof(*density_y));
829 (void) memset(&mean,0,
sizeof(mean));
830 (void) memset(sum,0,number_grays*
sizeof(*sum));
831 (void) memset(&sum_squares,0,
sizeof(sum_squares));
832 (void) memset(density_xy,0,2*number_grays*
sizeof(*density_xy));
833 (void) memset(&entropy_x,0,
sizeof(entropy_x));
834 (void) memset(&entropy_xy,0,
sizeof(entropy_xy));
835 (void) memset(&entropy_xy1,0,
sizeof(entropy_xy1));
836 (void) memset(&entropy_xy2,0,
sizeof(entropy_xy2));
837 (void) memset(&entropy_y,0,
sizeof(entropy_y));
838 (void) memset(&variance,0,
sizeof(variance));
839 for (i=0; i < (ssize_t) number_grays; i++)
842 sizeof(**cooccurrence));
847 (void) memset(cooccurrence[i],0,number_grays*
848 sizeof(**cooccurrence));
849 (void) memset(Q[i],0,number_grays*
sizeof(**Q));
851 if (i < (ssize_t) number_grays)
853 for (i--; i >= 0; i--)
859 RelinquishMagickMemory(cooccurrence[i]);
867 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
870 (void) ThrowMagickException(exception,GetMagickModule(),
871 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
872 return(channel_features);
878 image_view=AcquireVirtualCacheView(image,exception);
879 for (r=0; r < (ssize_t) image->rows; r++)
892 if (status == MagickFalse)
894 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,r,image->columns+
895 2*distance,distance+2,exception);
896 if (p == (
const Quantum *) NULL)
901 p+=(ptrdiff_t) distance*GetPixelChannels(image);;
902 for (x=0; x < (ssize_t) image->columns; x++)
904 for (i=0; i < 4; i++)
914 offset=(ssize_t) distance;
922 offset=(ssize_t) (image->columns+2*distance);
930 offset=(ssize_t) ((image->columns+2*distance)-distance);
938 offset=(ssize_t) ((image->columns+2*distance)+distance);
944 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
946 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*(ssize_t) GetPixelChannels(image))))
948 cooccurrence[u][v].direction[i].red++;
949 cooccurrence[v][u].direction[i].red++;
952 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
954 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*(ssize_t) GetPixelChannels(image))))
956 cooccurrence[u][v].direction[i].green++;
957 cooccurrence[v][u].direction[i].green++;
960 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
962 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*(ssize_t) GetPixelChannels(image))))
964 cooccurrence[u][v].direction[i].blue++;
965 cooccurrence[v][u].direction[i].blue++;
966 if (image->colorspace == CMYKColorspace)
970 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
972 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*(ssize_t) GetPixelChannels(image))))
974 cooccurrence[u][v].direction[i].black++;
975 cooccurrence[v][u].direction[i].black++;
977 if (image->alpha_trait != UndefinedPixelTrait)
981 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
983 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*(ssize_t) GetPixelChannels(image))))
985 cooccurrence[u][v].direction[i].alpha++;
986 cooccurrence[v][u].direction[i].alpha++;
989 p+=(ptrdiff_t) GetPixelChannels(image);
992 grays=(
PixelPacket *) RelinquishMagickMemory(grays);
993 image_view=DestroyCacheView(image_view);
994 if (status == MagickFalse)
996 for (i=0; i < (ssize_t) number_grays; i++)
998 RelinquishMagickMemory(cooccurrence[i]);
1002 (void) ThrowMagickException(exception,GetMagickModule(),
1003 ResourceLimitError,
"MemoryAllocationFailed",
"`%s'",image->filename);
1004 return(channel_features);
1009 for (i=0; i < 4; i++)
1025 normalize=2.0*image->rows*(image->columns-distance);
1033 normalize=2.0*(image->rows-distance)*image->columns;
1041 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1049 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1053 normalize=PerceptibleReciprocal(normalize);
1054 for (y=0; y < (ssize_t) number_grays; y++)
1059 for (x=0; x < (ssize_t) number_grays; x++)
1061 cooccurrence[x][y].direction[i].red*=normalize;
1062 cooccurrence[x][y].direction[i].green*=normalize;
1063 cooccurrence[x][y].direction[i].blue*=normalize;
1064 if (image->colorspace == CMYKColorspace)
1065 cooccurrence[x][y].direction[i].black*=normalize;
1066 if (image->alpha_trait != UndefinedPixelTrait)
1067 cooccurrence[x][y].direction[i].alpha*=normalize;
1074#if defined(MAGICKCORE_OPENMP_SUPPORT)
1075 #pragma omp parallel for schedule(static) shared(status) \
1076 magick_number_threads(image,image,number_grays,1)
1078 for (i=0; i < 4; i++)
1083 for (y=0; y < (ssize_t) number_grays; y++)
1088 for (x=0; x < (ssize_t) number_grays; x++)
1093 channel_features[RedPixelChannel].angular_second_moment[i]+=
1094 cooccurrence[x][y].direction[i].red*
1095 cooccurrence[x][y].direction[i].red;
1096 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1097 cooccurrence[x][y].direction[i].green*
1098 cooccurrence[x][y].direction[i].green;
1099 channel_features[BluePixelChannel].angular_second_moment[i]+=
1100 cooccurrence[x][y].direction[i].blue*
1101 cooccurrence[x][y].direction[i].blue;
1102 if (image->colorspace == CMYKColorspace)
1103 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1104 cooccurrence[x][y].direction[i].black*
1105 cooccurrence[x][y].direction[i].black;
1106 if (image->alpha_trait != UndefinedPixelTrait)
1107 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1108 cooccurrence[x][y].direction[i].alpha*
1109 cooccurrence[x][y].direction[i].alpha;
1113 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1114 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1115 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1116 if (image->colorspace == CMYKColorspace)
1117 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1118 if (image->alpha_trait != UndefinedPixelTrait)
1119 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1120 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1121 correlation.direction[i].green+=x*y*
1122 cooccurrence[x][y].direction[i].green;
1123 correlation.direction[i].blue+=x*y*
1124 cooccurrence[x][y].direction[i].blue;
1125 if (image->colorspace == CMYKColorspace)
1126 correlation.direction[i].black+=x*y*
1127 cooccurrence[x][y].direction[i].black;
1128 if (image->alpha_trait != UndefinedPixelTrait)
1129 correlation.direction[i].alpha+=x*y*
1130 cooccurrence[x][y].direction[i].alpha;
1134 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1135 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1136 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1137 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1138 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1139 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1140 if (image->colorspace == CMYKColorspace)
1141 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1142 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1143 if (image->alpha_trait != UndefinedPixelTrait)
1144 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1145 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1149 density_xy[y+x+2].direction[i].red+=
1150 cooccurrence[x][y].direction[i].red;
1151 density_xy[y+x+2].direction[i].green+=
1152 cooccurrence[x][y].direction[i].green;
1153 density_xy[y+x+2].direction[i].blue+=
1154 cooccurrence[x][y].direction[i].blue;
1155 if (image->colorspace == CMYKColorspace)
1156 density_xy[y+x+2].direction[i].black+=
1157 cooccurrence[x][y].direction[i].black;
1158 if (image->alpha_trait != UndefinedPixelTrait)
1159 density_xy[y+x+2].direction[i].alpha+=
1160 cooccurrence[x][y].direction[i].alpha;
1164 channel_features[RedPixelChannel].entropy[i]-=
1165 cooccurrence[x][y].direction[i].red*
1166 MagickLog10(cooccurrence[x][y].direction[i].red);
1167 channel_features[GreenPixelChannel].entropy[i]-=
1168 cooccurrence[x][y].direction[i].green*
1169 MagickLog10(cooccurrence[x][y].direction[i].green);
1170 channel_features[BluePixelChannel].entropy[i]-=
1171 cooccurrence[x][y].direction[i].blue*
1172 MagickLog10(cooccurrence[x][y].direction[i].blue);
1173 if (image->colorspace == CMYKColorspace)
1174 channel_features[BlackPixelChannel].entropy[i]-=
1175 cooccurrence[x][y].direction[i].black*
1176 MagickLog10(cooccurrence[x][y].direction[i].black);
1177 if (image->alpha_trait != UndefinedPixelTrait)
1178 channel_features[AlphaPixelChannel].entropy[i]-=
1179 cooccurrence[x][y].direction[i].alpha*
1180 MagickLog10(cooccurrence[x][y].direction[i].alpha);
1184 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1185 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1186 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1187 if (image->alpha_trait != UndefinedPixelTrait)
1188 density_x[x].direction[i].alpha+=
1189 cooccurrence[x][y].direction[i].alpha;
1190 if (image->colorspace == CMYKColorspace)
1191 density_x[x].direction[i].black+=
1192 cooccurrence[x][y].direction[i].black;
1193 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1194 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1195 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1196 if (image->colorspace == CMYKColorspace)
1197 density_y[y].direction[i].black+=
1198 cooccurrence[x][y].direction[i].black;
1199 if (image->alpha_trait != UndefinedPixelTrait)
1200 density_y[y].direction[i].alpha+=
1201 cooccurrence[x][y].direction[i].alpha;
1203 mean.direction[i].red+=y*sum[y].direction[i].red;
1204 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1205 mean.direction[i].green+=y*sum[y].direction[i].green;
1206 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1207 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1208 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1209 if (image->colorspace == CMYKColorspace)
1211 mean.direction[i].black+=y*sum[y].direction[i].black;
1212 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1214 if (image->alpha_trait != UndefinedPixelTrait)
1216 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1217 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1223 channel_features[RedPixelChannel].correlation[i]=
1224 (correlation.direction[i].red-mean.direction[i].red*
1225 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1226 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1227 sum_squares.direction[i].red-(mean.direction[i].red*
1228 mean.direction[i].red)));
1229 channel_features[GreenPixelChannel].correlation[i]=
1230 (correlation.direction[i].green-mean.direction[i].green*
1231 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1232 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1233 sum_squares.direction[i].green-(mean.direction[i].green*
1234 mean.direction[i].green)));
1235 channel_features[BluePixelChannel].correlation[i]=
1236 (correlation.direction[i].blue-mean.direction[i].blue*
1237 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1238 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1239 sum_squares.direction[i].blue-(mean.direction[i].blue*
1240 mean.direction[i].blue)));
1241 if (image->colorspace == CMYKColorspace)
1242 channel_features[BlackPixelChannel].correlation[i]=
1243 (correlation.direction[i].black-mean.direction[i].black*
1244 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1245 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1246 sum_squares.direction[i].black-(mean.direction[i].black*
1247 mean.direction[i].black)));
1248 if (image->alpha_trait != UndefinedPixelTrait)
1249 channel_features[AlphaPixelChannel].correlation[i]=
1250 (correlation.direction[i].alpha-mean.direction[i].alpha*
1251 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1252 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1253 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1254 mean.direction[i].alpha)));
1259#if defined(MAGICKCORE_OPENMP_SUPPORT)
1260 #pragma omp parallel for schedule(static) shared(status) \
1261 magick_number_threads(image,image,number_grays,1)
1263 for (i=0; i < 4; i++)
1268 for (x=2; x < (ssize_t) (2*number_grays); x++)
1273 channel_features[RedPixelChannel].sum_average[i]+=
1274 x*density_xy[x].direction[i].red;
1275 channel_features[GreenPixelChannel].sum_average[i]+=
1276 x*density_xy[x].direction[i].green;
1277 channel_features[BluePixelChannel].sum_average[i]+=
1278 x*density_xy[x].direction[i].blue;
1279 if (image->colorspace == CMYKColorspace)
1280 channel_features[BlackPixelChannel].sum_average[i]+=
1281 x*density_xy[x].direction[i].black;
1282 if (image->alpha_trait != UndefinedPixelTrait)
1283 channel_features[AlphaPixelChannel].sum_average[i]+=
1284 x*density_xy[x].direction[i].alpha;
1288 channel_features[RedPixelChannel].sum_entropy[i]-=
1289 density_xy[x].direction[i].red*
1290 MagickLog10(density_xy[x].direction[i].red);
1291 channel_features[GreenPixelChannel].sum_entropy[i]-=
1292 density_xy[x].direction[i].green*
1293 MagickLog10(density_xy[x].direction[i].green);
1294 channel_features[BluePixelChannel].sum_entropy[i]-=
1295 density_xy[x].direction[i].blue*
1296 MagickLog10(density_xy[x].direction[i].blue);
1297 if (image->colorspace == CMYKColorspace)
1298 channel_features[BlackPixelChannel].sum_entropy[i]-=
1299 density_xy[x].direction[i].black*
1300 MagickLog10(density_xy[x].direction[i].black);
1301 if (image->alpha_trait != UndefinedPixelTrait)
1302 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1303 density_xy[x].direction[i].alpha*
1304 MagickLog10(density_xy[x].direction[i].alpha);
1308 channel_features[RedPixelChannel].sum_variance[i]+=
1309 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1310 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1311 density_xy[x].direction[i].red;
1312 channel_features[GreenPixelChannel].sum_variance[i]+=
1313 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1314 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1315 density_xy[x].direction[i].green;
1316 channel_features[BluePixelChannel].sum_variance[i]+=
1317 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1318 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1319 density_xy[x].direction[i].blue;
1320 if (image->colorspace == CMYKColorspace)
1321 channel_features[BlackPixelChannel].sum_variance[i]+=
1322 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1323 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1324 density_xy[x].direction[i].black;
1325 if (image->alpha_trait != UndefinedPixelTrait)
1326 channel_features[AlphaPixelChannel].sum_variance[i]+=
1327 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1328 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1329 density_xy[x].direction[i].alpha;
1335#if defined(MAGICKCORE_OPENMP_SUPPORT)
1336 #pragma omp parallel for schedule(static) shared(status) \
1337 magick_number_threads(image,image,number_grays,1)
1339 for (i=0; i < 4; i++)
1344 for (y=0; y < (ssize_t) number_grays; y++)
1349 for (x=0; x < (ssize_t) number_grays; x++)
1354 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1355 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1356 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1357 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1358 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1359 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1360 if (image->colorspace == CMYKColorspace)
1361 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1362 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1363 if (image->alpha_trait != UndefinedPixelTrait)
1364 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1365 (y-mean.direction[i].alpha+1)*
1366 cooccurrence[x][y].direction[i].alpha;
1370 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1371 cooccurrence[x][y].direction[i].red;
1372 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1373 cooccurrence[x][y].direction[i].green;
1374 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1375 cooccurrence[x][y].direction[i].blue;
1376 if (image->colorspace == CMYKColorspace)
1377 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1378 cooccurrence[x][y].direction[i].black;
1379 if (image->alpha_trait != UndefinedPixelTrait)
1380 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1381 cooccurrence[x][y].direction[i].alpha;
1385 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1386 MagickLog10(cooccurrence[x][y].direction[i].red);
1387 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1388 MagickLog10(cooccurrence[x][y].direction[i].green);
1389 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1390 MagickLog10(cooccurrence[x][y].direction[i].blue);
1391 if (image->colorspace == CMYKColorspace)
1392 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1393 MagickLog10(cooccurrence[x][y].direction[i].black);
1394 if (image->alpha_trait != UndefinedPixelTrait)
1395 entropy_xy.direction[i].alpha-=
1396 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1397 cooccurrence[x][y].direction[i].alpha);
1398 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1399 MagickLog10(density_x[x].direction[i].red*density_y[y].direction[i].red));
1400 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1401 MagickLog10(density_x[x].direction[i].green*
1402 density_y[y].direction[i].green));
1403 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1404 MagickLog10(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1405 if (image->colorspace == CMYKColorspace)
1406 entropy_xy1.direction[i].black-=(
1407 cooccurrence[x][y].direction[i].black*MagickLog10(
1408 density_x[x].direction[i].black*density_y[y].direction[i].black));
1409 if (image->alpha_trait != UndefinedPixelTrait)
1410 entropy_xy1.direction[i].alpha-=(
1411 cooccurrence[x][y].direction[i].alpha*MagickLog10(
1412 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1413 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1414 density_y[y].direction[i].red*MagickLog10(density_x[x].direction[i].red*
1415 density_y[y].direction[i].red));
1416 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1417 density_y[y].direction[i].green*MagickLog10(density_x[x].direction[i].green*
1418 density_y[y].direction[i].green));
1419 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1420 density_y[y].direction[i].blue*MagickLog10(density_x[x].direction[i].blue*
1421 density_y[y].direction[i].blue));
1422 if (image->colorspace == CMYKColorspace)
1423 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1424 density_y[y].direction[i].black*MagickLog10(
1425 density_x[x].direction[i].black*density_y[y].direction[i].black));
1426 if (image->alpha_trait != UndefinedPixelTrait)
1427 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1428 density_y[y].direction[i].alpha*MagickLog10(
1429 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1432 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1433 variance.direction[i].red;
1434 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1435 variance.direction[i].green;
1436 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1437 variance.direction[i].blue;
1438 if (image->colorspace == CMYKColorspace)
1439 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1440 variance.direction[i].black;
1441 if (image->alpha_trait != UndefinedPixelTrait)
1442 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1443 variance.direction[i].alpha;
1448 (void) memset(&variance,0,
sizeof(variance));
1449 (void) memset(&sum_squares,0,
sizeof(sum_squares));
1450#if defined(MAGICKCORE_OPENMP_SUPPORT)
1451 #pragma omp parallel for schedule(static) shared(status) \
1452 magick_number_threads(image,image,number_grays,1)
1454 for (i=0; i < 4; i++)
1459 for (x=0; x < (ssize_t) number_grays; x++)
1464 variance.direction[i].red+=density_xy[x].direction[i].red;
1465 variance.direction[i].green+=density_xy[x].direction[i].green;
1466 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1467 if (image->colorspace == CMYKColorspace)
1468 variance.direction[i].black+=density_xy[x].direction[i].black;
1469 if (image->alpha_trait != UndefinedPixelTrait)
1470 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1471 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1472 density_xy[x].direction[i].red;
1473 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1474 density_xy[x].direction[i].green;
1475 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1476 density_xy[x].direction[i].blue;
1477 if (image->colorspace == CMYKColorspace)
1478 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1479 density_xy[x].direction[i].black;
1480 if (image->alpha_trait != UndefinedPixelTrait)
1481 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1482 density_xy[x].direction[i].alpha;
1486 channel_features[RedPixelChannel].difference_entropy[i]-=
1487 density_xy[x].direction[i].red*
1488 MagickLog10(density_xy[x].direction[i].red);
1489 channel_features[GreenPixelChannel].difference_entropy[i]-=
1490 density_xy[x].direction[i].green*
1491 MagickLog10(density_xy[x].direction[i].green);
1492 channel_features[BluePixelChannel].difference_entropy[i]-=
1493 density_xy[x].direction[i].blue*
1494 MagickLog10(density_xy[x].direction[i].blue);
1495 if (image->colorspace == CMYKColorspace)
1496 channel_features[BlackPixelChannel].difference_entropy[i]-=
1497 density_xy[x].direction[i].black*
1498 MagickLog10(density_xy[x].direction[i].black);
1499 if (image->alpha_trait != UndefinedPixelTrait)
1500 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1501 density_xy[x].direction[i].alpha*
1502 MagickLog10(density_xy[x].direction[i].alpha);
1506 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1507 MagickLog10(density_x[x].direction[i].red));
1508 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1509 MagickLog10(density_x[x].direction[i].green));
1510 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1511 MagickLog10(density_x[x].direction[i].blue));
1512 if (image->colorspace == CMYKColorspace)
1513 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1514 MagickLog10(density_x[x].direction[i].black));
1515 if (image->alpha_trait != UndefinedPixelTrait)
1516 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1517 MagickLog10(density_x[x].direction[i].alpha));
1518 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1519 MagickLog10(density_y[x].direction[i].red));
1520 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1521 MagickLog10(density_y[x].direction[i].green));
1522 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1523 MagickLog10(density_y[x].direction[i].blue));
1524 if (image->colorspace == CMYKColorspace)
1525 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1526 MagickLog10(density_y[x].direction[i].black));
1527 if (image->alpha_trait != UndefinedPixelTrait)
1528 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1529 MagickLog10(density_y[x].direction[i].alpha));
1534 channel_features[RedPixelChannel].difference_variance[i]=
1535 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1536 (variance.direction[i].red*variance.direction[i].red))/
1537 ((double) number_grays*number_grays*number_grays*number_grays);
1538 channel_features[GreenPixelChannel].difference_variance[i]=
1539 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1540 (variance.direction[i].green*variance.direction[i].green))/
1541 ((double) number_grays*number_grays*number_grays*number_grays);
1542 channel_features[BluePixelChannel].difference_variance[i]=
1543 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1544 (variance.direction[i].blue*variance.direction[i].blue))/
1545 ((double) number_grays*number_grays*number_grays*number_grays);
1546 if (image->colorspace == CMYKColorspace)
1547 channel_features[BlackPixelChannel].difference_variance[i]=
1548 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1549 (variance.direction[i].black*variance.direction[i].black))/
1550 ((double) number_grays*number_grays*number_grays*number_grays);
1551 if (image->alpha_trait != UndefinedPixelTrait)
1552 channel_features[AlphaPixelChannel].difference_variance[i]=
1553 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1554 (variance.direction[i].alpha*variance.direction[i].alpha))/
1555 ((double) number_grays*number_grays*number_grays*number_grays);
1559 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1560 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1561 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1562 entropy_x.direction[i].red : entropy_y.direction[i].red);
1563 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1564 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1565 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1566 entropy_x.direction[i].green : entropy_y.direction[i].green);
1567 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1568 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1569 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1570 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1571 if (image->colorspace == CMYKColorspace)
1572 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1573 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1574 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1575 entropy_x.direction[i].black : entropy_y.direction[i].black);
1576 if (image->alpha_trait != UndefinedPixelTrait)
1577 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1578 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1579 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1580 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1581 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1582 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].red-
1583 entropy_xy.direction[i].red)))));
1584 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1585 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].green-
1586 entropy_xy.direction[i].green)))));
1587 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1588 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].blue-
1589 entropy_xy.direction[i].blue)))));
1590 if (image->colorspace == CMYKColorspace)
1591 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1592 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].black-
1593 entropy_xy.direction[i].black)))));
1594 if (image->alpha_trait != UndefinedPixelTrait)
1595 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1596 (sqrt(fabs(1.0-exp(-2.0*(
double) (entropy_xy2.direction[i].alpha-
1597 entropy_xy.direction[i].alpha)))));
1602#if defined(MAGICKCORE_OPENMP_SUPPORT)
1603 #pragma omp parallel for schedule(static) shared(status) \
1604 magick_number_threads(image,image,number_grays,1)
1606 for (i=0; i < 4; i++)
1611 for (z=0; z < (ssize_t) number_grays; z++)
1619 (void) memset(&pixel,0,
sizeof(pixel));
1620 for (y=0; y < (ssize_t) number_grays; y++)
1625 for (x=0; x < (ssize_t) number_grays; x++)
1630 if (((y-x) == z) || ((x-y) == z))
1632 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1633 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1634 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1635 if (image->colorspace == CMYKColorspace)
1636 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1637 if (image->alpha_trait != UndefinedPixelTrait)
1638 pixel.direction[i].alpha+=
1639 cooccurrence[x][y].direction[i].alpha;
1644 if ((fabs(density_x[z].direction[i].red) > MagickEpsilon) &&
1645 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1646 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1647 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1648 density_y[x].direction[i].red;
1649 if ((fabs(density_x[z].direction[i].green) > MagickEpsilon) &&
1650 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1651 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1652 cooccurrence[y][x].direction[i].green/
1653 density_x[z].direction[i].green/density_y[x].direction[i].red;
1654 if ((fabs(density_x[z].direction[i].blue) > MagickEpsilon) &&
1655 (fabs(density_y[x].direction[i].blue) > MagickEpsilon))
1656 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1657 cooccurrence[y][x].direction[i].blue/
1658 density_x[z].direction[i].blue/density_y[x].direction[i].blue;
1659 if (image->colorspace == CMYKColorspace)
1660 if ((fabs(density_x[z].direction[i].black) > MagickEpsilon) &&
1661 (fabs(density_y[x].direction[i].black) > MagickEpsilon))
1662 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1663 cooccurrence[y][x].direction[i].black/
1664 density_x[z].direction[i].black/density_y[x].direction[i].black;
1665 if (image->alpha_trait != UndefinedPixelTrait)
1666 if ((fabs(density_x[z].direction[i].alpha) > MagickEpsilon) &&
1667 (fabs(density_y[x].direction[i].alpha) > MagickEpsilon))
1668 Q[z][y].direction[i].alpha+=
1669 cooccurrence[z][x].direction[i].alpha*
1670 cooccurrence[y][x].direction[i].alpha/
1671 density_x[z].direction[i].alpha/
1672 density_y[x].direction[i].alpha;
1675 channel_features[RedPixelChannel].contrast[i]+=z*z*
1676 pixel.direction[i].red;
1677 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1678 pixel.direction[i].green;
1679 channel_features[BluePixelChannel].contrast[i]+=z*z*
1680 pixel.direction[i].blue;
1681 if (image->colorspace == CMYKColorspace)
1682 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1683 pixel.direction[i].black;
1684 if (image->alpha_trait != UndefinedPixelTrait)
1685 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1686 pixel.direction[i].alpha;
1692 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1694 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1696 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1698 if (image->colorspace == CMYKColorspace)
1699 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1701 if (image->alpha_trait != UndefinedPixelTrait)
1702 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1709 for (i=0; i < (ssize_t) number_grays; i++)
1715 for (i=0; i < (ssize_t) number_grays; i++)
1717 RelinquishMagickMemory(cooccurrence[i]);
1719 return(channel_features);
1762static inline double MagickRound(
double x)
1767 if ((x-floor(x)) < (ceil(x)-x))
1772static Image *RenderHoughLines(
const ImageInfo *image_info,
const size_t columns,
1775#define BoundingBox "viewbox"
1789 image=AcquireImage(image_info,exception);
1790 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1791 if (status == MagickFalse)
1793 image=DestroyImageList(image);
1794 return((
Image *) NULL);
1796 image->columns=columns;
1798 draw_info=CloneDrawInfo(image_info,(
DrawInfo *) NULL);
1799 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
1801 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
1803 image->columns=(size_t) (draw_info->affine.sx*image->columns);
1804 image->rows=(size_t) (draw_info->affine.sy*image->rows);
1805 status=SetImageExtent(image,image->columns,image->rows,exception);
1806 if (status == MagickFalse)
1807 return(DestroyImageList(image));
1808 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1810 draw_info=DestroyDrawInfo(draw_info);
1811 image=DestroyImageList(image);
1812 return((
Image *) NULL);
1817 if (GetBlobStreamData(image) == (
unsigned char *) NULL)
1818 draw_info->primitive=FileToString(image->filename,~0UL,exception);
1821 draw_info->primitive=(
char *) AcquireQuantumMemory(1,(
size_t)
1822 GetBlobSize(image)+1);
1823 if (draw_info->primitive != (
char *) NULL)
1825 (void) memcpy(draw_info->primitive,GetBlobStreamData(image),
1826 (size_t) GetBlobSize(image));
1827 draw_info->primitive[GetBlobSize(image)]=
'\0';
1830 (void) DrawImage(image,draw_info,exception);
1831 draw_info=DestroyDrawInfo(draw_info);
1832 if (CloseBlob(image) == MagickFalse)
1833 image=DestroyImageList(image);
1834 return(GetFirstImageInList(image));
1837MagickExport
Image *HoughLineImage(
const Image *image,
const size_t width,
1838 const size_t height,
const size_t threshold,
ExceptionInfo *exception)
1840#define HoughLineImageTag "HoughLine/Image"
1846 message[MagickPathExtent],
1847 path[MagickPathExtent];
1856 *lines_image = NULL;
1887 assert(image != (
const Image *) NULL);
1888 assert(image->signature == MagickCoreSignature);
1890 assert(exception->signature == MagickCoreSignature);
1891 if (IsEventLogging() != MagickFalse)
1892 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1893 accumulator_width=180;
1894 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1895 image->rows : image->columns))/2.0);
1896 accumulator_height=(size_t) (2.0*hough_height);
1897 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1898 sizeof(
double),exception);
1900 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1901 if (NullMatrix(accumulator) == MagickFalse)
1903 accumulator=DestroyMatrixInfo(accumulator);
1904 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1911 center.x=(double) image->columns/2.0;
1912 center.y=(double) image->rows/2.0;
1913 image_view=AcquireVirtualCacheView(image,exception);
1914 for (y=0; y < (ssize_t) image->rows; y++)
1922 if (status == MagickFalse)
1924 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1925 if (p == (Quantum *) NULL)
1930 for (x=0; x < (ssize_t) image->columns; x++)
1932 if (GetPixelIntensity(image,p) > ((double) QuantumRange/2.0))
1937 for (i=0; i < 180; i++)
1943 radius=(((double) x-center.x)*cos(DegreesToRadians((
double) i)))+
1944 (((
double) y-center.y)*sin(DegreesToRadians((
double) i)));
1945 (void) GetMatrixElement(accumulator,i,(ssize_t)
1946 MagickRound(radius+hough_height),&count);
1948 (void) SetMatrixElement(accumulator,i,(ssize_t)
1949 MagickRound(radius+hough_height),&count);
1952 p+=(ptrdiff_t) GetPixelChannels(image);
1954 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1959#if defined(MAGICKCORE_OPENMP_SUPPORT)
1963 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
1964 if (proceed == MagickFalse)
1968 image_view=DestroyCacheView(image_view);
1969 if (status == MagickFalse)
1971 accumulator=DestroyMatrixInfo(accumulator);
1972 return((
Image *) NULL);
1977 file=AcquireUniqueFileResource(path);
1980 accumulator=DestroyMatrixInfo(accumulator);
1981 return((
Image *) NULL);
1983 (void) FormatLocaleString(message,MagickPathExtent,
1984 "# Hough line transform: %.20gx%.20g%+.20g\n",(
double) width,
1985 (double) height,(
double) threshold);
1986 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1988 (void) FormatLocaleString(message,MagickPathExtent,
1989 "viewbox 0 0 %.20g %.20g\n",(
double) image->columns,(double) image->rows);
1990 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1992 (void) FormatLocaleString(message,MagickPathExtent,
1993 "# x1,y1 x2,y2 # count angle distance\n");
1994 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1996 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1998 line_count=threshold;
1999 for (y=0; y < (ssize_t) accumulator_height; y++)
2004 for (x=0; x < (ssize_t) accumulator_width; x++)
2009 (void) GetMatrixElement(accumulator,x,y,&count);
2010 if (count >= (
double) line_count)
2025 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2030 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2032 if ((u != 0) || (v !=0))
2034 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
2042 if (u < (ssize_t) (width/2))
2045 (void) GetMatrixElement(accumulator,x,y,&count);
2048 if ((x >= 45) && (x <= 135))
2054 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
2055 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2056 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2057 line.x2=(double) image->columns;
2058 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
2059 (image->columns/2.0))*cos(DegreesToRadians((
double) x))))/
2060 sin(DegreesToRadians((
double) x))+(image->rows/2.0);
2068 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
2069 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2070 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2071 line.y2=(double) image->rows;
2072 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
2073 (image->rows/2.0))*sin(DegreesToRadians((
double) x))))/
2074 cos(DegreesToRadians((
double) x))+(image->columns/2.0);
2076 (void) FormatLocaleString(message,MagickPathExtent,
2077 "line %g,%g %g,%g # %g %g %g\n",line.x1,line.y1,line.x2,line.y2,
2078 maxima,(
double) x,(double) y);
2079 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2084 (void) close_utf8(file);
2088 image_info=AcquireImageInfo();
2089 image_info->background_color=image->background_color;
2090 (void) FormatLocaleString(image_info->filename,MagickPathExtent,
"%s",path);
2091 artifact=GetImageArtifact(image,
"background");
2092 if (artifact != (
const char *) NULL)
2093 (void) SetImageOption(image_info,
"background",artifact);
2094 artifact=GetImageArtifact(image,
"fill");
2095 if (artifact != (
const char *) NULL)
2096 (void) SetImageOption(image_info,
"fill",artifact);
2097 artifact=GetImageArtifact(image,
"stroke");
2098 if (artifact != (
const char *) NULL)
2099 (void) SetImageOption(image_info,
"stroke",artifact);
2100 artifact=GetImageArtifact(image,
"strokewidth");
2101 if (artifact != (
const char *) NULL)
2102 (void) SetImageOption(image_info,
"strokewidth",artifact);
2103 lines_image=RenderHoughLines(image_info,image->columns,image->rows,exception);
2104 artifact=GetImageArtifact(image,
"hough-lines:accumulator");
2105 if ((lines_image != (
Image *) NULL) &&
2106 (IsStringTrue(artifact) != MagickFalse))
2111 accumulator_image=MatrixToImage(accumulator,exception);
2112 if (accumulator_image != (
Image *) NULL)
2113 AppendImageToList(&lines_image,accumulator_image);
2118 accumulator=DestroyMatrixInfo(accumulator);
2119 image_info=DestroyImageInfo(image_info);
2120 (void) RelinquishUniqueFileResource(path);
2121 return(GetFirstImageInList(lines_image));
2164MagickExport
Image *MeanShiftImage(
const Image *image,
const size_t width,
2165 const size_t height,
const double color_distance,
ExceptionInfo *exception)
2167#define MaxMeanShiftIterations 100
2168#define MeanShiftImageTag "MeanShift/Image"
2187 assert(image != (
const Image *) NULL);
2188 assert(image->signature == MagickCoreSignature);
2190 assert(exception->signature == MagickCoreSignature);
2191 if (IsEventLogging() != MagickFalse)
2192 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
2193 mean_image=CloneImage(image,0,0,MagickTrue,exception);
2194 if (mean_image == (
Image *) NULL)
2195 return((
Image *) NULL);
2196 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2198 mean_image=DestroyImage(mean_image);
2199 return((
Image *) NULL);
2203 image_view=AcquireVirtualCacheView(image,exception);
2204 pixel_view=AcquireVirtualCacheView(image,exception);
2205 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2206#if defined(MAGICKCORE_OPENMP_SUPPORT)
2207 #pragma omp parallel for schedule(static) shared(status,progress) \
2208 magick_number_threads(mean_image,mean_image,mean_image->rows,1)
2210 for (y=0; y < (ssize_t) mean_image->rows; y++)
2221 if (status == MagickFalse)
2223 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2224 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2226 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
2231 for (x=0; x < (ssize_t) mean_image->columns; x++)
2244 GetPixelInfo(image,&mean_pixel);
2245 GetPixelInfoPixel(image,p,&mean_pixel);
2246 mean_location.x=(double) x;
2247 mean_location.y=(double) y;
2248 for (i=0; i < MaxMeanShiftIterations; i++)
2266 GetPixelInfo(image,&sum_pixel);
2267 previous_location=mean_location;
2268 previous_pixel=mean_pixel;
2270 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2275 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2277 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2282 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2283 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2284 mean_location.y+v),&pixel,exception);
2285 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2286 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2287 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2288 if (distance <= (color_distance*color_distance))
2290 sum_location.x+=mean_location.x+u;
2291 sum_location.y+=mean_location.y+v;
2292 sum_pixel.red+=pixel.red;
2293 sum_pixel.green+=pixel.green;
2294 sum_pixel.blue+=pixel.blue;
2295 sum_pixel.alpha+=pixel.alpha;
2302 gamma=PerceptibleReciprocal((
double) 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+=(ptrdiff_t) GetPixelChannels(image);
2327 q+=(ptrdiff_t) 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);