39#include "MagickCore/studio.h"
40#include "MagickCore/artifact.h"
41#include "MagickCore/blob.h"
42#include "MagickCore/cache-view.h"
43#include "MagickCore/color.h"
44#include "MagickCore/color-private.h"
45#include "MagickCore/colormap.h"
46#include "MagickCore/colorspace.h"
47#include "MagickCore/constitute.h"
48#include "MagickCore/decorate.h"
49#include "MagickCore/distort.h"
50#include "MagickCore/draw.h"
51#include "MagickCore/enhance.h"
52#include "MagickCore/exception.h"
53#include "MagickCore/exception-private.h"
54#include "MagickCore/effect.h"
55#include "MagickCore/gem.h"
56#include "MagickCore/geometry.h"
57#include "MagickCore/image-private.h"
58#include "MagickCore/list.h"
59#include "MagickCore/log.h"
60#include "MagickCore/matrix.h"
61#include "MagickCore/memory_.h"
62#include "MagickCore/memory-private.h"
63#include "MagickCore/monitor.h"
64#include "MagickCore/monitor-private.h"
65#include "MagickCore/montage.h"
66#include "MagickCore/morphology.h"
67#include "MagickCore/morphology-private.h"
68#include "MagickCore/opencl-private.h"
69#include "MagickCore/paint.h"
70#include "MagickCore/pixel-accessor.h"
71#include "MagickCore/property.h"
72#include "MagickCore/quantum.h"
73#include "MagickCore/resource_.h"
74#include "MagickCore/signature-private.h"
75#include "MagickCore/string_.h"
76#include "MagickCore/string-private.h"
77#include "MagickCore/thread-private.h"
78#include "MagickCore/token.h"
79#include "MagickCore/vision.h"
119static int CCObjectInfoCompare(
const void *x,
const void *y)
128 return((
int) (q->bounding_box.y-(ssize_t) p->bounding_box.y));
130 return((
int) (q->bounding_box.x-(ssize_t) p->bounding_box.x));
132 return((
int) (q->bounding_box.height-p->bounding_box.height));
134 return((
int) (q->bounding_box.width-p->bounding_box.width));
136 return((
int) (q->area-(ssize_t) p->area));
138 return((
int) (p->area-(ssize_t) q->area));
140 return((
int) (p->bounding_box.width-q->bounding_box.width));
142 return((
int) (p->bounding_box.height-q->bounding_box.height));
144 return((
int) (p->bounding_box.x-(ssize_t) q->bounding_box.x));
146 return((
int) (p->bounding_box.y-(ssize_t) q->bounding_box.y));
147 return((
int) (q->area-(ssize_t) p->area));
150static void PerimeterThreshold(
const Image *component_image,
160#if defined(MAGICKCORE_OPENMP_SUPPORT)
161 #pragma omp parallel for schedule(dynamic) shared(status) \
162 magick_number_threads(component_image,component_image,component_image->colors,1)
164 for (i=0; i < (ssize_t) component_image->colors; i++)
173 pattern[4] = { 1, 0, 0, 0 };
181 if (status == MagickFalse)
183 component_view=AcquireAuthenticCacheView(component_image,exception);
184 bounding_box=
object[i].bounding_box;
185 for (y=(-1); y < (ssize_t) bounding_box.height; y++)
193 p=GetCacheViewVirtualPixels(component_view,bounding_box.x-1,
194 bounding_box.y+y,bounding_box.width+2,2,exception);
195 if (p == (
const Quantum *) NULL)
200 for (x=(-1); x < (ssize_t) bounding_box.width; x++)
216 for (v=0; v < 2; v++)
221 for (u=0; u < 2; u++)
226 offset=v*((ssize_t) bounding_box.width+2)*
227 (ssize_t) GetPixelChannels(component_image)+u*
228 (ssize_t) GetPixelChannels(component_image);
229 pixels[2*v+u]=GetPixelIndex(component_image,p+offset);
230 if ((ssize_t) pixels[2*v+u] == i)
239 if ((((ssize_t) pixels[0] == i) && ((ssize_t) pixels[3] == i)) ||
240 (((ssize_t) pixels[1] == i) && ((ssize_t) pixels[2] == i)))
248 p+=GetPixelChannels(component_image);
251 component_view=DestroyCacheView(component_view);
252 object[i].metric[metric_index]=ceil(MagickSQ1_2*pattern[1]+1.0*pattern[2]+
253 MagickSQ1_2*pattern[3]+MagickSQ2*pattern[0]-0.5);
257static void CircularityThreshold(
const Image *component_image,
267#if defined(MAGICKCORE_OPENMP_SUPPORT)
268 #pragma omp parallel for schedule(dynamic) shared(status) \
269 magick_number_threads(component_image,component_image,component_image->colors,1)
271 for (i=0; i < (ssize_t) component_image->colors; i++)
280 pattern[4] = { 1, 0, 0, 0 };
288 if (status == MagickFalse)
290 component_view=AcquireAuthenticCacheView(component_image,exception);
291 bounding_box=
object[i].bounding_box;
292 for (y=(-1); y < (ssize_t) bounding_box.height; y++)
300 p=GetCacheViewVirtualPixels(component_view,bounding_box.x-1,
301 bounding_box.y+y,bounding_box.width+2,2,exception);
302 if (p == (
const Quantum *) NULL)
307 for (x=(-1); x < (ssize_t) bounding_box.width; x++)
323 for (v=0; v < 2; v++)
328 for (u=0; u < 2; u++)
333 offset=v*((ssize_t) bounding_box.width+2)*
334 (ssize_t) GetPixelChannels(component_image)+u*
335 (ssize_t) GetPixelChannels(component_image);
336 pixels[2*v+u]=GetPixelIndex(component_image,p+offset);
337 if ((ssize_t) pixels[2*v+u] == i)
346 if ((((ssize_t) pixels[0] == i) && ((ssize_t) pixels[3] == i)) ||
347 (((ssize_t) pixels[1] == i) && ((ssize_t) pixels[2] == i)))
355 p+=GetPixelChannels(component_image);
358 component_view=DestroyCacheView(component_view);
359 object[i].metric[metric_index]=ceil(MagickSQ1_2*pattern[1]+1.0*pattern[2]+
360 MagickSQ1_2*pattern[3]+MagickSQ2*pattern[0]-0.5);
361 object[i].metric[metric_index]=4.0*MagickPI*
object[i].area/
362 (
object[i].metric[metric_index]*
object[i].metric[metric_index]);
366static void MajorAxisThreshold(
const Image *component_image,
376#if defined(MAGICKCORE_OPENMP_SUPPORT)
377 #pragma omp parallel for schedule(dynamic) shared(status) \
378 magick_number_threads(component_image,component_image,component_image->colors,1)
380 for (i=0; i < (ssize_t) component_image->colors; i++)
394 centroid = { 0.0, 0.0 };
411 if (status == MagickFalse)
413 component_view=AcquireAuthenticCacheView(component_image,exception);
414 bounding_box=
object[i].bounding_box;
415 for (y=0; y < (ssize_t) bounding_box.height; y++)
417 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
418 bounding_box.y+y,bounding_box.width,1,exception);
419 if (p == (
const Quantum *) NULL)
424 for (x=0; x < (ssize_t) bounding_box.width; x++)
426 if ((ssize_t) GetPixelIndex(component_image,p) == i)
432 p+=GetPixelChannels(component_image);
435 centroid.x=M10*PerceptibleReciprocal(M00);
436 centroid.y=M01*PerceptibleReciprocal(M00);
437 for (y=0; y < (ssize_t) bounding_box.height; y++)
439 if (status == MagickFalse)
441 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
442 bounding_box.y+y,bounding_box.width,1,exception);
443 if (p == (
const Quantum *) NULL)
448 for (x=0; x < (ssize_t) bounding_box.width; x++)
450 if ((ssize_t) GetPixelIndex(component_image,p) == i)
452 M11+=(x-centroid.x)*(y-centroid.y);
453 M20+=(x-centroid.x)*(x-centroid.x);
454 M02+=(y-centroid.y)*(y-centroid.y);
456 p+=GetPixelChannels(component_image);
459 component_view=DestroyCacheView(component_view);
460 object[i].metric[metric_index]=sqrt((2.0*PerceptibleReciprocal(M00))*
461 ((M20+M02)+sqrt(4.0*M11*M11+(M20-M02)*(M20-M02))));
465static void MinorAxisThreshold(
const Image *component_image,
475#if defined(MAGICKCORE_OPENMP_SUPPORT)
476 #pragma omp parallel for schedule(dynamic) shared(status) \
477 magick_number_threads(component_image,component_image,component_image->colors,1)
479 for (i=0; i < (ssize_t) component_image->colors; i++)
493 centroid = { 0.0, 0.0 };
510 if (status == MagickFalse)
512 component_view=AcquireAuthenticCacheView(component_image,exception);
513 bounding_box=
object[i].bounding_box;
514 for (y=0; y < (ssize_t) bounding_box.height; y++)
516 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
517 bounding_box.y+y,bounding_box.width,1,exception);
518 if (p == (
const Quantum *) NULL)
523 for (x=0; x < (ssize_t) bounding_box.width; x++)
525 if ((ssize_t) GetPixelIndex(component_image,p) == i)
531 p+=GetPixelChannels(component_image);
534 centroid.x=M10*PerceptibleReciprocal(M00);
535 centroid.y=M01*PerceptibleReciprocal(M00);
536 for (y=0; y < (ssize_t) bounding_box.height; y++)
538 if (status == MagickFalse)
540 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
541 bounding_box.y+y,bounding_box.width,1,exception);
542 if (p == (
const Quantum *) NULL)
547 for (x=0; x < (ssize_t) bounding_box.width; x++)
549 if ((ssize_t) GetPixelIndex(component_image,p) == i)
551 M11+=(x-centroid.x)*(y-centroid.y);
552 M20+=(x-centroid.x)*(x-centroid.x);
553 M02+=(y-centroid.y)*(y-centroid.y);
555 p+=GetPixelChannels(component_image);
558 component_view=DestroyCacheView(component_view);
559 object[i].metric[metric_index]=sqrt((2.0*PerceptibleReciprocal(M00))*
560 ((M20+M02)-sqrt(4.0*M11*M11+(M20-M02)*(M20-M02))));
564static void EccentricityThreshold(
const Image *component_image,
574#if defined(MAGICKCORE_OPENMP_SUPPORT)
575 #pragma omp parallel for schedule(dynamic) shared(status) \
576 magick_number_threads(component_image,component_image,component_image->colors,1)
578 for (i=0; i < (ssize_t) component_image->colors; i++)
592 centroid = { 0.0, 0.0 },
593 ellipse_axis = { 0.0, 0.0 };
610 if (status == MagickFalse)
612 component_view=AcquireAuthenticCacheView(component_image,exception);
613 bounding_box=
object[i].bounding_box;
614 for (y=0; y < (ssize_t) bounding_box.height; y++)
616 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
617 bounding_box.y+y,bounding_box.width,1,exception);
618 if (p == (
const Quantum *) NULL)
623 for (x=0; x < (ssize_t) bounding_box.width; x++)
625 if ((ssize_t) GetPixelIndex(component_image,p) == i)
631 p+=GetPixelChannels(component_image);
634 centroid.x=M10*PerceptibleReciprocal(M00);
635 centroid.y=M01*PerceptibleReciprocal(M00);
636 for (y=0; y < (ssize_t) bounding_box.height; y++)
638 if (status == MagickFalse)
640 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
641 bounding_box.y+y,bounding_box.width,1,exception);
642 if (p == (
const Quantum *) NULL)
647 for (x=0; x < (ssize_t) bounding_box.width; x++)
649 if ((ssize_t) GetPixelIndex(component_image,p) == i)
651 M11+=(x-centroid.x)*(y-centroid.y);
652 M20+=(x-centroid.x)*(x-centroid.x);
653 M02+=(y-centroid.y)*(y-centroid.y);
655 p+=GetPixelChannels(component_image);
658 component_view=DestroyCacheView(component_view);
659 ellipse_axis.x=sqrt((2.0*PerceptibleReciprocal(M00))*((M20+M02)+
660 sqrt(4.0*M11*M11+(M20-M02)*(M20-M02))));
661 ellipse_axis.y=sqrt((2.0*PerceptibleReciprocal(M00))*((M20+M02)-
662 sqrt(4.0*M11*M11+(M20-M02)*(M20-M02))));
663 object[i].metric[metric_index]=sqrt(1.0-(ellipse_axis.y*ellipse_axis.y*
664 PerceptibleReciprocal(ellipse_axis.x*ellipse_axis.x)));
668static void AngleThreshold(
const Image *component_image,
678#if defined(MAGICKCORE_OPENMP_SUPPORT)
679 #pragma omp parallel for schedule(dynamic) shared(status) \
680 magick_number_threads(component_image,component_image,component_image->colors,1)
682 for (i=0; i < (ssize_t) component_image->colors; i++)
696 centroid = { 0.0, 0.0 };
713 if (status == MagickFalse)
715 component_view=AcquireAuthenticCacheView(component_image,exception);
716 bounding_box=
object[i].bounding_box;
717 for (y=0; y < (ssize_t) bounding_box.height; y++)
719 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
720 bounding_box.y+y,bounding_box.width,1,exception);
721 if (p == (
const Quantum *) NULL)
726 for (x=0; x < (ssize_t) bounding_box.width; x++)
728 if ((ssize_t) GetPixelIndex(component_image,p) == i)
734 p+=GetPixelChannels(component_image);
737 centroid.x=M10*PerceptibleReciprocal(M00);
738 centroid.y=M01*PerceptibleReciprocal(M00);
739 for (y=0; y < (ssize_t) bounding_box.height; y++)
741 if (status == MagickFalse)
743 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
744 bounding_box.y+y,bounding_box.width,1,exception);
745 if (p == (
const Quantum *) NULL)
750 for (x=0; x < (ssize_t) bounding_box.width; x++)
752 if ((ssize_t) GetPixelIndex(component_image,p) == i)
754 M11+=(x-centroid.x)*(y-centroid.y);
755 M20+=(x-centroid.x)*(x-centroid.x);
756 M02+=(y-centroid.y)*(y-centroid.y);
758 p+=GetPixelChannels(component_image);
761 component_view=DestroyCacheView(component_view);
762 object[i].metric[metric_index]=RadiansToDegrees(1.0/2.0*atan(2.0*M11*
763 PerceptibleReciprocal(M20-M02)));
766 if ((fabs(M20-M02) >= 0.0) && ((M20-M02) < 0.0))
767 object[i].metric[metric_index]+=90.0;
772 if (fabs(M20-M02) >= 0.0)
775 object[i].metric[metric_index]+=90.0;
777 object[i].metric[metric_index]+=180.0;
781 if ((fabs(M20-M02) >= 0.0) && ((M20-M02) < 0.0))
782 object[i].metric[metric_index]+=90.0;
786MagickExport
Image *ConnectedComponentsImage(
const Image *image,
789#define ConnectedComponentsImageTag "ConnectedComponents/Image"
804 *metrics[CCMaxMetrics];
827 connect4[2][2] = { { -1, 0 }, { 0, -1 } },
828 connect8[4][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 } },
841 assert(image != (
Image *) NULL);
842 assert(image->signature == MagickCoreSignature);
844 assert(exception->signature == MagickCoreSignature);
845 if (IsEventLogging() != MagickFalse)
846 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
849 component_image=CloneImage(image,0,0,MagickTrue,exception);
850 if (component_image == (
Image *) NULL)
851 return((
Image *) NULL);
852 component_image->depth=MAGICKCORE_QUANTUM_DEPTH;
853 if (AcquireImageColormap(component_image,MaxColormapSize,exception) == MagickFalse)
855 component_image=DestroyImage(component_image);
856 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
861 size=image->columns*image->rows;
862 if (image->columns != (size/image->rows))
864 component_image=DestroyImage(component_image);
865 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
867 equivalences=AcquireMatrixInfo(size,1,
sizeof(ssize_t),exception);
870 component_image=DestroyImage(component_image);
871 return((
Image *) NULL);
873 for (n=0; n < (ssize_t) (image->columns*image->rows); n++)
874 (
void) SetMatrixElement(equivalences,n,0,&n);
875 object=(
CCObjectInfo *) AcquireQuantumMemory(MaxColormapSize,
sizeof(*
object));
878 equivalences=DestroyMatrixInfo(equivalences);
879 component_image=DestroyImage(component_image);
880 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
882 (void) memset(
object,0,MaxColormapSize*
sizeof(*
object));
883 for (i=0; i < (ssize_t) MaxColormapSize; i++)
886 object[i].bounding_box.x=(ssize_t) image->columns;
887 object[i].bounding_box.y=(ssize_t) image->rows;
888 GetPixelInfo(image,&
object[i].color);
895 image_view=AcquireVirtualCacheView(image,exception);
896 for (n=0; n < (ssize_t) (connectivity > 4 ? 4 : 2); n++)
898 if (status == MagickFalse)
900 dx=connectivity > 4 ? connect8[n][1] : connect4[n][1];
901 dy=connectivity > 4 ? connect8[n][0] : connect4[n][0];
902 for (y=0; y < (ssize_t) image->rows; y++)
910 if (status == MagickFalse)
912 p=GetCacheViewVirtualPixels(image_view,0,y-1,image->columns,3,exception);
913 if (p == (
const Quantum *) NULL)
918 p+=GetPixelChannels(image)*image->columns;
919 for (x=0; x < (ssize_t) image->columns; x++)
936 GetPixelInfoPixel(image,p,&pixel);
937 if (((x+dx) < 0) || ((x+dx) >= (ssize_t) image->columns) ||
938 ((y+dy) < 0) || ((y+dy) >= (ssize_t) image->rows))
940 p+=GetPixelChannels(image);
943 neighbor_offset=dy*((ssize_t) GetPixelChannels(image)*(ssize_t)
944 image->columns)+dx*(ssize_t) GetPixelChannels(image);
945 GetPixelInfoPixel(image,p+neighbor_offset,&target);
946 if (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)
948 p+=GetPixelChannels(image);
954 offset=y*(ssize_t) image->columns+x;
955 neighbor_offset=dy*(ssize_t) image->columns+dx;
957 status=GetMatrixElement(equivalences,ox,0,&obj);
961 status=GetMatrixElement(equivalences,ox,0,&obj);
963 oy=offset+neighbor_offset;
964 status=GetMatrixElement(equivalences,oy,0,&obj);
968 status=GetMatrixElement(equivalences,oy,0,&obj);
972 status=SetMatrixElement(equivalences,oy,0,&ox);
977 status=SetMatrixElement(equivalences,ox,0,&oy);
981 status=GetMatrixElement(equivalences,ox,0,&obj);
984 status=GetMatrixElement(equivalences,ox,0,&obj);
985 status=SetMatrixElement(equivalences,ox,0,&root);
987 oy=offset+neighbor_offset;
988 status=GetMatrixElement(equivalences,oy,0,&obj);
991 status=GetMatrixElement(equivalences,oy,0,&obj);
992 status=SetMatrixElement(equivalences,oy,0,&root);
994 status=SetMatrixElement(equivalences,y*(ssize_t) image->columns+x,0,
996 p+=GetPixelChannels(image);
1004 component_view=AcquireAuthenticCacheView(component_image,exception);
1005 for (y=0; y < (ssize_t) component_image->rows; y++)
1016 if (status == MagickFalse)
1018 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1019 q=QueueCacheViewAuthenticPixels(component_view,0,y,component_image->columns,
1021 if ((p == (
const Quantum *) NULL) || (q == (Quantum *) NULL))
1026 for (x=0; x < (ssize_t) component_image->columns; x++)
1032 offset=y*(ssize_t) image->columns+x;
1033 status=GetMatrixElement(equivalences,offset,0,&
id);
1035 status=GetMatrixElement(equivalences,
id,0,&
id);
1039 if (
id >= (ssize_t) MaxColormapSize)
1042 status=SetMatrixElement(equivalences,offset,0,&
id);
1043 if (x <
object[
id].bounding_box.x)
1044 object[id].bounding_box.x=x;
1045 if (x >= (ssize_t)
object[
id].bounding_box.width)
1046 object[id].bounding_box.width=(size_t) x;
1047 if (y <
object[
id].bounding_box.y)
1048 object[id].bounding_box.y=y;
1049 if (y >= (ssize_t)
object[
id].bounding_box.height)
1050 object[id].bounding_box.height=(size_t) y;
1051 object[id].color.red+=QuantumScale*(double) GetPixelRed(image,p);
1052 object[id].color.green+=QuantumScale*(double) GetPixelGreen(image,p);
1053 object[id].color.blue+=QuantumScale*(double) GetPixelBlue(image,p);
1054 if (image->alpha_trait != UndefinedPixelTrait)
1055 object[id].color.alpha+=QuantumScale*(double) GetPixelAlpha(image,p);
1056 if (image->colorspace == CMYKColorspace)
1057 object[id].color.black+=QuantumScale*(double) GetPixelBlack(image,p);
1058 object[id].centroid.x+=x;
1059 object[id].centroid.y+=y;
1061 SetPixelIndex(component_image,(Quantum)
id,q);
1062 p+=GetPixelChannels(image);
1063 q+=GetPixelChannels(component_image);
1065 if (n > (ssize_t) MaxColormapSize)
1067 if (SyncCacheViewAuthenticPixels(component_view,exception) == MagickFalse)
1069 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1075 proceed=SetImageProgress(image,ConnectedComponentsImageTag,progress,
1077 if (proceed == MagickFalse)
1081 component_view=DestroyCacheView(component_view);
1082 image_view=DestroyCacheView(image_view);
1083 equivalences=DestroyMatrixInfo(equivalences);
1084 if (n > (ssize_t) MaxColormapSize)
1086 object=(
CCObjectInfo *) RelinquishMagickMemory(
object);
1087 component_image=DestroyImage(component_image);
1088 ThrowImageException(ResourceLimitError,
"TooManyObjects");
1093 component_image->colors=(size_t) n;
1094 for (i=0; i < (ssize_t) component_image->colors; i++)
1096 object[i].bounding_box.width=(size_t) ((ssize_t)
1097 object[i].bounding_box.width-(
object[i].bounding_box.x-1));
1098 object[i].bounding_box.height=(size_t) ((ssize_t)
1099 object[i].bounding_box.height-(
object[i].bounding_box.y-1));
1100 object[i].color.red/=(QuantumScale*
object[i].area);
1101 object[i].color.green/=(QuantumScale*
object[i].area);
1102 object[i].color.blue/=(QuantumScale*
object[i].area);
1103 if (image->alpha_trait != UndefinedPixelTrait)
1104 object[i].color.alpha/=(QuantumScale*
object[i].area);
1105 if (image->colorspace == CMYKColorspace)
1106 object[i].color.black/=(QuantumScale*
object[i].area);
1107 object[i].centroid.x/=
object[i].area;
1108 object[i].centroid.y/=
object[i].area;
1109 max_threshold+=
object[i].area;
1110 if (
object[i].area >
object[background_id].area)
1113 max_threshold+=MagickEpsilon;
1115 artifact=GetImageArtifact(image,
"connected-components:background-id");
1116 if (artifact != (
const char *) NULL)
1117 background_id=(ssize_t) StringToLong(artifact);
1118 artifact=GetImageArtifact(image,
"connected-components:area-threshold");
1119 if (artifact != (
const char *) NULL)
1124 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1125 for (i=0; i < (ssize_t) component_image->colors; i++)
1126 if (((
object[i].area < min_threshold) ||
1127 (
object[i].area >= max_threshold)) && (i != background_id))
1128 object[i].merge=MagickTrue;
1130 artifact=GetImageArtifact(image,
"connected-components:keep-colors");
1131 if (artifact != (
const char *) NULL)
1139 for (i=0; i < (ssize_t) component_image->colors; i++)
1140 object[i].merge=MagickTrue;
1141 for (p=artifact; ; )
1144 color[MagickPathExtent];
1152 for (q=p; *q !=
'\0'; q++)
1155 (void) CopyMagickString(color,p,(
size_t) MagickMin(q-p+1,
1157 (void) QueryColorCompliance(color,AllCompliance,&pixel,exception);
1158 for (i=0; i < (ssize_t) component_image->colors; i++)
1159 if (IsFuzzyEquivalencePixelInfo(&
object[i].color,&pixel) != MagickFalse)
1160 object[i].merge=MagickFalse;
1166 artifact=GetImageArtifact(image,
"connected-components:keep-ids");
1167 if (artifact == (
const char *) NULL)
1168 artifact=GetImageArtifact(image,
"connected-components:keep");
1169 if (artifact != (
const char *) NULL)
1174 for (i=0; i < (ssize_t) component_image->colors; i++)
1175 object[i].merge=MagickTrue;
1176 for (c=(
char *) artifact; *c !=
'\0'; )
1178 while ((isspace((
int) ((
unsigned char) *c)) != 0) || (*c ==
','))
1180 first=(ssize_t) strtol(c,&c,10);
1182 first+=(ssize_t) component_image->colors;
1184 while (isspace((
int) ((
unsigned char) *c)) != 0)
1188 last=(ssize_t) strtol(c+1,&c,10);
1190 last+=(ssize_t) component_image->colors;
1192 step=(ssize_t) (first > last ? -1 : 1);
1193 for ( ; first != (last+step); first+=step)
1194 object[first].merge=MagickFalse;
1197 artifact=GetImageArtifact(image,
"connected-components:keep-top");
1198 if (artifact != (
const char *) NULL)
1209 top_ids=(ssize_t) StringToLong(artifact);
1210 top_objects=(
CCObjectInfo *) AcquireQuantumMemory(component_image->colors,
1211 sizeof(*top_objects));
1214 object=(
CCObjectInfo *) RelinquishMagickMemory(
object);
1215 component_image=DestroyImage(component_image);
1216 ThrowImageException(ResourceLimitError,
"MemoryAllocationFailed");
1218 (void) memcpy(top_objects,
object,component_image->colors*
sizeof(*
object));
1219 qsort((
void *) top_objects,component_image->colors,
sizeof(*top_objects),
1220 CCObjectInfoCompare);
1221 for (i=top_ids+1; i < (ssize_t) component_image->colors; i++)
1222 object[top_objects[i].
id].merge=MagickTrue;
1223 top_objects=(
CCObjectInfo *) RelinquishMagickMemory(top_objects);
1225 artifact=GetImageArtifact(image,
"connected-components:remove-colors");
1226 if (artifact != (
const char *) NULL)
1234 for (p=artifact; ; )
1237 color[MagickPathExtent];
1245 for (q=p; *q !=
'\0'; q++)
1248 (void) CopyMagickString(color,p,(
size_t) MagickMin(q-p+1,
1250 (void) QueryColorCompliance(color,AllCompliance,&pixel,exception);
1251 for (i=0; i < (ssize_t) component_image->colors; i++)
1252 if (IsFuzzyEquivalencePixelInfo(&
object[i].color,&pixel) != MagickFalse)
1253 object[i].merge=MagickTrue;
1259 artifact=GetImageArtifact(image,
"connected-components:remove-ids");
1260 if (artifact == (
const char *) NULL)
1261 artifact=GetImageArtifact(image,
"connected-components:remove");
1262 if (artifact != (
const char *) NULL)
1263 for (c=(
char *) artifact; *c !=
'\0'; )
1268 while ((isspace((
int) ((
unsigned char) *c)) != 0) || (*c ==
','))
1270 first=(ssize_t) strtol(c,&c,10);
1272 first+=(ssize_t) component_image->colors;
1274 while (isspace((
int) ((
unsigned char) *c)) != 0)
1278 last=(ssize_t) strtol(c+1,&c,10);
1280 last+=(ssize_t) component_image->colors;
1282 step=(ssize_t) (first > last ? -1 : 1);
1283 for ( ; first != (last+step); first+=step)
1284 object[first].merge=MagickTrue;
1286 artifact=GetImageArtifact(image,
"connected-components:perimeter-threshold");
1287 if (artifact != (
const char *) NULL)
1292 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1293 metrics[++n]=
"perimeter";
1294 PerimeterThreshold(component_image,
object,n,exception);
1295 for (i=0; i < (ssize_t) component_image->colors; i++)
1296 if (((
object[i].metric[n] < min_threshold) ||
1297 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1298 object[i].merge=MagickTrue;
1300 artifact=GetImageArtifact(image,
"connected-components:circularity-threshold");
1301 if (artifact != (
const char *) NULL)
1306 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1307 metrics[++n]=
"circularity";
1308 CircularityThreshold(component_image,
object,n,exception);
1309 for (i=0; i < (ssize_t) component_image->colors; i++)
1310 if (((
object[i].metric[n] < min_threshold) ||
1311 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1312 object[i].merge=MagickTrue;
1314 artifact=GetImageArtifact(image,
"connected-components:diameter-threshold");
1315 if (artifact != (
const char *) NULL)
1320 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1321 metrics[++n]=
"diameter";
1322 for (i=0; i < (ssize_t) component_image->colors; i++)
1324 object[i].metric[n]=ceil(sqrt(4.0*
object[i].area/MagickPI)-0.5);
1325 if (((
object[i].metric[n] < min_threshold) ||
1326 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1327 object[i].merge=MagickTrue;
1330 artifact=GetImageArtifact(image,
"connected-components:major-axis-threshold");
1331 if (artifact != (
const char *) NULL)
1336 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1337 metrics[++n]=
"major-axis";
1338 MajorAxisThreshold(component_image,
object,n,exception);
1339 for (i=0; i < (ssize_t) component_image->colors; i++)
1340 if (((
object[i].metric[n] < min_threshold) ||
1341 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1342 object[i].merge=MagickTrue;
1344 artifact=GetImageArtifact(image,
"connected-components:minor-axis-threshold");
1345 if (artifact != (
const char *) NULL)
1350 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1351 metrics[++n]=
"minor-axis";
1352 MinorAxisThreshold(component_image,
object,n,exception);
1353 for (i=0; i < (ssize_t) component_image->colors; i++)
1354 if (((
object[i].metric[n] < min_threshold) ||
1355 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1356 object[i].merge=MagickTrue;
1358 artifact=GetImageArtifact(image,
"connected-components:eccentricity-threshold");
1359 if (artifact != (
const char *) NULL)
1364 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1365 metrics[++n]=
"eccentricity";
1366 EccentricityThreshold(component_image,
object,n,exception);
1367 for (i=0; i < (ssize_t) component_image->colors; i++)
1368 if (((
object[i].metric[n] < min_threshold) ||
1369 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1370 object[i].merge=MagickTrue;
1372 artifact=GetImageArtifact(image,
"connected-components:angle-threshold");
1373 if (artifact != (
const char *) NULL)
1378 (void) sscanf(artifact,
"%lf%*[ -]%lf",&min_threshold,&max_threshold);
1379 metrics[++n]=
"angle";
1380 AngleThreshold(component_image,
object,n,exception);
1381 for (i=0; i < (ssize_t) component_image->colors; i++)
1382 if (((
object[i].metric[n] < min_threshold) ||
1383 (
object[i].metric[n] >= max_threshold)) && (i != background_id))
1384 object[i].merge=MagickTrue;
1389 component_view=AcquireAuthenticCacheView(component_image,exception);
1390 object_view=AcquireVirtualCacheView(component_image,exception);
1391 (void) SetCacheViewVirtualPixelMethod(object_view,TileVirtualPixelMethod);
1392 for (i=0; i < (ssize_t) component_image->colors; i++)
1403 if (status == MagickFalse)
1405 if ((
object[i].merge == MagickFalse) || (i == background_id))
1410 for (j=0; j < (ssize_t) component_image->colors; j++)
1412 bounding_box=
object[i].bounding_box;
1413 for (y=0; y < (ssize_t) bounding_box.height; y++)
1421 if (status == MagickFalse)
1423 p=GetCacheViewVirtualPixels(component_view,bounding_box.x,
1424 bounding_box.y+y,bounding_box.width,1,exception);
1425 if (p == (
const Quantum *) NULL)
1430 for (x=0; x < (ssize_t) bounding_box.width; x++)
1435 if (status == MagickFalse)
1437 j=(ssize_t) GetPixelIndex(component_image,p);
1439 for (k=0; k < (ssize_t) (connectivity > 4 ? 4 : 2); k++)
1447 if (status == MagickFalse)
1449 dx=connectivity > 4 ? connect8[k][1] : connect4[k][1];
1450 dy=connectivity > 4 ? connect8[k][0] : connect4[k][0];
1451 q=GetCacheViewVirtualPixels(object_view,bounding_box.x+x+dx,
1452 bounding_box.y+y+dy,1,1,exception);
1453 if (q == (
const Quantum *) NULL)
1458 j=(ssize_t) GetPixelIndex(component_image,q);
1462 p+=GetPixelChannels(component_image);
1469 for (j=1; j < (ssize_t) component_image->colors; j++)
1470 if (
object[j].census >
object[
id].census)
1473 for (y=0; y < (ssize_t) bounding_box.height; y++)
1481 if (status == MagickFalse)
1483 q=GetCacheViewAuthenticPixels(component_view,bounding_box.x,
1484 bounding_box.y+y,bounding_box.width,1,exception);
1485 if (q == (Quantum *) NULL)
1490 for (x=0; x < (ssize_t) bounding_box.width; x++)
1492 if ((ssize_t) GetPixelIndex(component_image,q) == i)
1493 SetPixelIndex(component_image,(Quantum)
id,q);
1494 q+=GetPixelChannels(component_image);
1496 if (SyncCacheViewAuthenticPixels(component_view,exception) == MagickFalse)
1500 object_view=DestroyCacheView(object_view);
1501 component_view=DestroyCacheView(component_view);
1502 artifact=GetImageArtifact(image,
"connected-components:mean-color");
1503 if (IsStringTrue(artifact) != MagickFalse)
1508 for (i=0; i < (ssize_t) component_image->colors; i++)
1509 component_image->colormap[i]=
object[i].color;
1511 (void) SyncImage(component_image,exception);
1512 artifact=GetImageArtifact(image,
"connected-components:verbose");
1513 if ((IsStringTrue(artifact) != MagickFalse) ||
1523 for (i=0; i < (ssize_t) component_image->colors; i++)
1525 object[i].bounding_box.width=0;
1526 object[i].bounding_box.height=0;
1527 object[i].bounding_box.x=(ssize_t) component_image->columns;
1528 object[i].bounding_box.y=(ssize_t) component_image->rows;
1529 object[i].centroid.x=0;
1530 object[i].centroid.y=0;
1531 object[i].census=
object[i].area == 0.0 ? 0.0 : 1.0;
1534 component_view=AcquireVirtualCacheView(component_image,exception);
1535 for (y=0; y < (ssize_t) component_image->rows; y++)
1543 if (status == MagickFalse)
1545 p=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,
1547 if (p == (
const Quantum *) NULL)
1552 for (x=0; x < (ssize_t) component_image->columns; x++)
1557 id=(size_t) GetPixelIndex(component_image,p);
1558 if (x <
object[
id].bounding_box.x)
1559 object[id].bounding_box.x=x;
1560 if (x > (ssize_t)
object[
id].bounding_box.width)
1561 object[id].bounding_box.width=(size_t) x;
1562 if (y <
object[
id].bounding_box.y)
1563 object[id].bounding_box.y=y;
1564 if (y > (ssize_t)
object[
id].bounding_box.height)
1565 object[id].bounding_box.height=(size_t) y;
1566 object[id].centroid.x+=x;
1567 object[id].centroid.y+=y;
1569 p+=GetPixelChannels(component_image);
1572 for (i=0; i < (ssize_t) component_image->colors; i++)
1574 object[i].bounding_box.width=(size_t) ((ssize_t)
1575 object[i].bounding_box.width-(
object[i].bounding_box.x-1));
1576 object[i].bounding_box.height=(size_t) ((ssize_t)
1577 object[i].bounding_box.height-(
object[i].bounding_box.y-1));
1578 object[i].centroid.x=
object[i].centroid.x/
object[i].area;
1579 object[i].centroid.y=
object[i].centroid.y/
object[i].area;
1581 component_view=DestroyCacheView(component_view);
1583 artifact=GetImageArtifact(image,
"connected-components:sort-order");
1584 if (artifact != (
const char *) NULL)
1585 if (LocaleCompare(artifact,
"decreasing") == 0)
1588 artifact=GetImageArtifact(image,
"connected-components:sort");
1589 if (artifact != (
const char *) NULL)
1591 if (LocaleCompare(artifact,
"area") == 0)
1593 if (LocaleCompare(artifact,
"width") == 0)
1595 if (LocaleCompare(artifact,
"height") == 0)
1597 if (LocaleCompare(artifact,
"x") == 0)
1599 if (LocaleCompare(artifact,
"y") == 0)
1602 for (i=0; i < (ssize_t) component_image->colors; i++)
1603 object[i].key=order*key;
1604 qsort((
void *)
object,component_image->colors,
sizeof(*
object),
1605 CCObjectInfoCompare);
1611 artifact=GetImageArtifact(image,
1612 "connected-components:exclude-header");
1613 if (IsStringTrue(artifact) == MagickFalse)
1615 (void) fprintf(stdout,
"Objects (");
1616 artifact=GetImageArtifact(image,
1617 "connected-components:exclude-ids");
1618 if (IsStringTrue(artifact) == MagickFalse)
1619 (void) fprintf(stdout,
"id: ");
1620 (void) fprintf(stdout,
"bounding-box centroid area mean-color");
1621 for (j=0; j <= n; j++)
1622 (
void) fprintf(stdout,
" %s",metrics[j]);
1623 (void) fprintf(stdout,
"):\n");
1625 for (i=0; i < (ssize_t) component_image->colors; i++)
1626 if (
object[i].census > 0.0)
1629 mean_color[MagickPathExtent];
1631 GetColorTuple(&
object[i].color,MagickFalse,mean_color);
1632 (void) fprintf(stdout,
" ");
1633 artifact=GetImageArtifact(image,
1634 "connected-components:exclude-ids");
1635 if (IsStringTrue(artifact) == MagickFalse)
1636 (void) fprintf(stdout,
"%.20g: ",(
double)
object[i].
id);
1637 (void) fprintf(stdout,
1638 "%.20gx%.20g%+.20g%+.20g %.1f,%.1f %.*g %s",(
double)
1639 object[i].bounding_box.width,(
double)
1640 object[i].bounding_box.height,(
double)
1641 object[i].bounding_box.x,(
double)
object[i].bounding_box.y,
1642 object[i].centroid.x,
object[i].centroid.y,
1643 GetMagickPrecision(),(
double)
object[i].area,mean_color);
1644 for (j=0; j <= n; j++)
1645 (
void) fprintf(stdout,
" %.*g",GetMagickPrecision(),
1646 object[i].metric[j]);
1647 (void) fprintf(stdout,
"\n");
1652 object=(
CCObjectInfo *) RelinquishMagickMemory(
object);
1655 return(component_image);
1684#define IntegralImageTag "Integral/Image"
1705 assert(image != (
const Image *) NULL);
1706 assert(image->signature == MagickCoreSignature);
1708 assert(exception->signature == MagickCoreSignature);
1709 if (IsEventLogging() != MagickFalse)
1710 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",image->filename);
1711 integral_image=CloneImage(image,0,0,MagickTrue,exception);
1712 if (integral_image == (
Image *) NULL)
1713 return((
Image *) NULL);
1714 if (SetImageStorageClass(integral_image,DirectClass,exception) == MagickFalse)
1716 integral_image=DestroyImage(integral_image);
1717 return((
Image *) NULL);
1724 image_view=AcquireVirtualCacheView(integral_image,exception);
1725 integral_view=AcquireAuthenticCacheView(integral_image,exception);
1726 for (y=0; y < (ssize_t) integral_image->rows; y++)
1740 if (status == MagickFalse)
1742 p=GetCacheViewVirtualPixels(integral_view,0,y-1,integral_image->columns,1,
1744 q=GetCacheViewAuthenticPixels(integral_view,0,y,integral_image->columns,1,
1746 if ((p == (
const Quantum *) NULL) || (p == (Quantum *) NULL))
1751 for (x=0; x < (ssize_t) integral_image->columns; x++)
1756 for (i=0; i < (ssize_t) GetPixelChannels(integral_image); i++)
1761 PixelTrait traits = GetPixelChannelTraits(integral_image,
1763 if (traits == UndefinedPixelTrait)
1765 if ((traits & CopyPixelTrait) != 0)
1769 sum+=(double) (q-GetPixelChannels(integral_image))[i];
1772 if ((x > 0) && (y > 0))
1773 sum-=(double) (p-GetPixelChannels(integral_image))[i];
1774 q[i]=ClampToQuantum(sum);
1776 p+=GetPixelChannels(integral_image);
1777 q+=GetPixelChannels(integral_image);
1779 sync=SyncCacheViewAuthenticPixels(integral_view,exception);
1780 if (sync == MagickFalse)
1782 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1788 proceed=SetImageProgress(integral_image,IntegralImageTag,progress,
1789 integral_image->rows);
1790 if (proceed == MagickFalse)
1794 integral_view=DestroyCacheView(integral_view);
1795 image_view=DestroyCacheView(image_view);
1796 if (status == MagickFalse)
1797 integral_image=DestroyImage(integral_image);
1798 return(integral_image);