MagickCore 7.1.2-31
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
feature.c
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% FFFFF EEEEE AAA TTTTT U U RRRR EEEEE %
7% F E A A T U U R R E %
8% FFF EEE AAAAA T U U RRRR EEE %
9% F E A A T U U R R E %
10% F EEEEE A A T UUU R R EEEEE %
11% %
12% %
13% MagickCore Image Feature Methods %
14% %
15% Software Design %
16% Cristy %
17% July 1992 %
18% %
19% %
20% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% https://imagemagick.org/license/ %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37%
38*/
39
40/*
41 Include declarations.
42*/
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"
99
100/*
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102% %
103% %
104% %
105% C a n n y E d g e I m a g e %
106% %
107% %
108% %
109%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110%
111% CannyEdgeImage() uses a multi-stage algorithm to detect a wide range of
112% edges in images.
113%
114% The format of the CannyEdgeImage method is:
115%
116% Image *CannyEdgeImage(const Image *image,const double radius,
117% const double sigma,const double lower_percent,
118% const double upper_percent,ExceptionInfo *exception)
119%
120% A description of each parameter follows:
121%
122% o image: the image.
123%
124% o radius: the radius of the gaussian smoothing filter.
125%
126% o sigma: the sigma of the gaussian smoothing filter.
127%
128% o lower_percent: percentage of edge pixels in the lower threshold.
129%
130% o upper_percent: percentage of edge pixels in the upper threshold.
131%
132% o exception: return any errors or warnings in this structure.
133%
134*/
135
136typedef struct _CannyInfo
137{
138 double
139 magnitude,
140 intensity;
141
142 int
143 orientation;
144
145 ssize_t
146 x,
147 y;
148} CannyInfo;
149
150static inline MagickBooleanType IsAuthenticPixel(const Image *image,
151 const ssize_t x,const ssize_t y)
152{
153 if ((x < 0) || (x >= (ssize_t) image->columns))
154 return(MagickFalse);
155 if ((y < 0) || (y >= (ssize_t) image->rows))
156 return(MagickFalse);
157 return(MagickTrue);
158}
159
160static MagickBooleanType TraceEdges(Image *edge_image,CacheView *edge_view,
161 MatrixInfo *canny_cache,const ssize_t x,const ssize_t y,
162 const double lower_threshold,ExceptionInfo *exception)
163{
164 CannyInfo
165 edge,
166 pixel;
167
168 MagickBooleanType
169 status;
170
171 Quantum
172 *q;
173
174 ssize_t
175 i;
176
177 q=GetCacheViewAuthenticPixels(edge_view,x,y,1,1,exception);
178 if (q == (Quantum *) NULL)
179 return(MagickFalse);
180 *q=QuantumRange;
181 status=SyncCacheViewAuthenticPixels(edge_view,exception);
182 if (status == MagickFalse)
183 return(MagickFalse);
184 if (GetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
185 return(MagickFalse);
186 edge.x=x;
187 edge.y=y;
188 if (SetMatrixElement(canny_cache,0,0,&edge) == MagickFalse)
189 return(MagickFalse);
190 for (i=1; i != 0; )
191 {
192 ssize_t
193 v;
194
195 i--;
196 status=GetMatrixElement(canny_cache,i,0,&edge);
197 if (status == MagickFalse)
198 return(MagickFalse);
199 for (v=(-1); v <= 1; v++)
200 {
201 ssize_t
202 u;
203
204 for (u=(-1); u <= 1; u++)
205 {
206 if ((u == 0) && (v == 0))
207 continue;
208 if (IsAuthenticPixel(edge_image,edge.x+u,edge.y+v) == MagickFalse)
209 continue;
210 /*
211 Not an edge if gradient value is below the lower threshold.
212 */
213 q=GetCacheViewAuthenticPixels(edge_view,edge.x+u,edge.y+v,1,1,
214 exception);
215 if (q == (Quantum *) NULL)
216 return(MagickFalse);
217 status=GetMatrixElement(canny_cache,edge.x+u,edge.y+v,&pixel);
218 if (status == MagickFalse)
219 return(MagickFalse);
220 if ((GetPixelIntensity(edge_image,q) == 0.0) &&
221 (pixel.intensity >= lower_threshold))
222 {
223 *q=QuantumRange;
224 status=SyncCacheViewAuthenticPixels(edge_view,exception);
225 if (status == MagickFalse)
226 return(MagickFalse);
227 edge.x+=u;
228 edge.y+=v;
229 status=SetMatrixElement(canny_cache,i,0,&edge);
230 if (status == MagickFalse)
231 return(MagickFalse);
232 i++;
233 }
234 }
235 }
236 }
237 return(MagickTrue);
238}
239
240MagickExport Image *CannyEdgeImage(const Image *image,const double radius,
241 const double sigma,const double lower_percent,const double upper_percent,
242 ExceptionInfo *exception)
243{
244#define CannyEdgeImageTag "CannyEdge/Image"
245
246 CacheView
247 *edge_view;
248
249 CannyInfo
250 element;
251
252 char
253 geometry[MagickPathExtent];
254
255 double
256 lower_threshold,
257 max,
258 min,
259 upper_threshold;
260
261 Image
262 *edge_image;
263
264 KernelInfo
265 *kernel_info;
266
267 MagickBooleanType
268 status;
269
270 MagickOffsetType
271 progress;
272
273 MatrixInfo
274 *canny_cache;
275
276 ssize_t
277 y;
278
279 assert(image != (const Image *) NULL);
280 assert(image->signature == MagickCoreSignature);
281 assert(exception != (ExceptionInfo *) NULL);
282 assert(exception->signature == MagickCoreSignature);
283 if (IsEventLogging() != MagickFalse)
284 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
285 /*
286 Filter out noise.
287 */
288 (void) FormatLocaleString(geometry,MagickPathExtent,
289 "blur:%.17gx%.17g;blur:%.17gx%.17g+90",radius,sigma,radius,sigma);
290 kernel_info=AcquireKernelInfo(geometry,exception);
291 if (kernel_info == (KernelInfo *) NULL)
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)
298 {
299 edge_image=DestroyImage(edge_image);
300 return((Image *) NULL);
301 }
302 (void) SetImageAlphaChannel(edge_image,OffAlphaChannel,exception);
303 /*
304 Find the intensity gradient of the image.
305 */
306 canny_cache=AcquireMatrixInfo(edge_image->columns,edge_image->rows,
307 sizeof(CannyInfo),exception);
308 if (canny_cache == (MatrixInfo *) NULL)
309 {
310 edge_image=DestroyImage(edge_image);
311 return((Image *) NULL);
312 }
313 status=MagickTrue;
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)
318#endif
319 for (y=0; y < (ssize_t) edge_image->rows; y++)
320 {
321 const Quantum
322 *magick_restrict p;
323
324 ssize_t
325 x;
326
327 if (status == MagickFalse)
328 continue;
329 p=GetCacheViewVirtualPixels(edge_view,0,y,edge_image->columns+1,2,
330 exception);
331 if (p == (const Quantum *) NULL)
332 {
333 status=MagickFalse;
334 continue;
335 }
336 for (x=0; x < (ssize_t) edge_image->columns; x++)
337 {
338 CannyInfo
339 pixel;
340
341 double
342 dx,
343 dy;
344
345 const Quantum
346 *magick_restrict kernel_pixels;
347
348 ssize_t
349 v;
350
351 static double
352 Gx[2][2] =
353 {
354 { -1.0, +1.0 },
355 { -1.0, +1.0 }
356 },
357 Gy[2][2] =
358 {
359 { +1.0, +1.0 },
360 { -1.0, -1.0 }
361 };
362
363 (void) memset(&pixel,0,sizeof(pixel));
364 dx=0.0;
365 dy=0.0;
366 kernel_pixels=p;
367 for (v=0; v < 2; v++)
368 {
369 ssize_t
370 u;
371
372 for (u=0; u < 2; u++)
373 {
374 double
375 intensity;
376
377 intensity=GetPixelIntensity(edge_image,kernel_pixels+u);
378 dx+=0.5*Gx[v][u]*intensity;
379 dy+=0.5*Gy[v][u]*intensity;
380 }
381 kernel_pixels+=edge_image->columns+1;
382 }
383 pixel.magnitude=hypot(dx,dy);
384 pixel.orientation=0;
385 if (fabs(dx) > MagickEpsilon)
386 {
387 double
388 slope;
389
390 slope=dy/dx;
391 if (slope < 0.0)
392 {
393 if (slope < -2.41421356237)
394 pixel.orientation=0;
395 else
396 if (slope < -0.414213562373)
397 pixel.orientation=1;
398 else
399 pixel.orientation=2;
400 }
401 else
402 {
403 if (slope > 2.41421356237)
404 pixel.orientation=0;
405 else
406 if (slope > 0.414213562373)
407 pixel.orientation=3;
408 else
409 pixel.orientation=2;
410 }
411 }
412 if (SetMatrixElement(canny_cache,x,y,&pixel) == MagickFalse)
413 continue;
414 p+=(ptrdiff_t) GetPixelChannels(edge_image);
415 }
416 }
417 edge_view=DestroyCacheView(edge_view);
418 /*
419 Non-maxima suppression, remove pixels that are not considered to be part
420 of an edge.
421 */
422 progress=0;
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)
430#endif
431 for (y=0; y < (ssize_t) edge_image->rows; y++)
432 {
433 Quantum
434 *magick_restrict q;
435
436 ssize_t
437 x;
438
439 if (status == MagickFalse)
440 continue;
441 q=GetCacheViewAuthenticPixels(edge_view,0,y,edge_image->columns,1,
442 exception);
443 if (q == (Quantum *) NULL)
444 {
445 status=MagickFalse;
446 continue;
447 }
448 for (x=0; x < (ssize_t) edge_image->columns; x++)
449 {
450 CannyInfo
451 alpha_pixel,
452 beta_pixel,
453 pixel;
454
455 (void) GetMatrixElement(canny_cache,x,y,&pixel);
456 switch (pixel.orientation)
457 {
458 case 0:
459 default:
460 {
461 /*
462 0 degrees, north and south.
463 */
464 (void) GetMatrixElement(canny_cache,x,y-1,&alpha_pixel);
465 (void) GetMatrixElement(canny_cache,x,y+1,&beta_pixel);
466 break;
467 }
468 case 1:
469 {
470 /*
471 45 degrees, northwest and southeast.
472 */
473 (void) GetMatrixElement(canny_cache,x-1,y-1,&alpha_pixel);
474 (void) GetMatrixElement(canny_cache,x+1,y+1,&beta_pixel);
475 break;
476 }
477 case 2:
478 {
479 /*
480 90 degrees, east and west.
481 */
482 (void) GetMatrixElement(canny_cache,x-1,y,&alpha_pixel);
483 (void) GetMatrixElement(canny_cache,x+1,y,&beta_pixel);
484 break;
485 }
486 case 3:
487 {
488 /*
489 135 degrees, northeast and southwest.
490 */
491 (void) GetMatrixElement(canny_cache,x+1,y-1,&beta_pixel);
492 (void) GetMatrixElement(canny_cache,x-1,y+1,&alpha_pixel);
493 break;
494 }
495 }
496 pixel.intensity=pixel.magnitude;
497 if ((pixel.magnitude < alpha_pixel.magnitude) ||
498 (pixel.magnitude < beta_pixel.magnitude))
499 pixel.intensity=0;
500 (void) SetMatrixElement(canny_cache,x,y,&pixel);
501#if defined(MAGICKCORE_OPENMP_SUPPORT)
502 #pragma omp critical (MagickCore_CannyEdgeImage)
503#endif
504 {
505 if (pixel.intensity < min)
506 min=pixel.intensity;
507 if (pixel.intensity > max)
508 max=pixel.intensity;
509 }
510 *q=(Quantum) 0;
511 q+=(ptrdiff_t) GetPixelChannels(edge_image);
512 }
513 if (SyncCacheViewAuthenticPixels(edge_view,exception) == MagickFalse)
514 status=MagickFalse;
515 }
516 edge_view=DestroyCacheView(edge_view);
517 /*
518 Estimate hysteresis threshold.
519 */
520 lower_threshold=lower_percent*(max-min)+min;
521 upper_threshold=upper_percent*(max-min)+min;
522 /*
523 Hysteresis threshold.
524 */
525 edge_view=AcquireAuthenticCacheView(edge_image,exception);
526 for (y=0; y < (ssize_t) edge_image->rows; y++)
527 {
528 ssize_t
529 x;
530
531 if (status == MagickFalse)
532 continue;
533 for (x=0; x < (ssize_t) edge_image->columns; x++)
534 {
535 CannyInfo
536 pixel;
537
538 const Quantum
539 *magick_restrict p;
540
541 /*
542 Edge if pixel gradient higher than upper threshold.
543 */
544 p=GetCacheViewVirtualPixels(edge_view,x,y,1,1,exception);
545 if (p == (const Quantum *) NULL)
546 continue;
547 status=GetMatrixElement(canny_cache,x,y,&pixel);
548 if (status == MagickFalse)
549 continue;
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,
553 exception);
554 }
555 if (image->progress_monitor != (MagickProgressMonitor) NULL)
556 {
557 MagickBooleanType
558 proceed;
559
560#if defined(MAGICKCORE_OPENMP_SUPPORT)
561 #pragma omp atomic
562#endif
563 progress++;
564 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
565 if (proceed == MagickFalse)
566 status=MagickFalse;
567 }
568 }
569 edge_view=DestroyCacheView(edge_view);
570 /*
571 Free resources.
572 */
573 canny_cache=DestroyMatrixInfo(canny_cache);
574 return(edge_image);
575}
576
577/*
578%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
579% %
580% %
581% %
582% G e t I m a g e F e a t u r e s %
583% %
584% %
585% %
586%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
587%
588% GetImageFeatures() returns features for each channel in the image in
589% each of four directions (horizontal, vertical, left and right diagonals)
590% for the specified distance. The features include the angular second
591% moment, contrast, correlation, sum of squares: variance, inverse difference
592% moment, sum average, sum variance, sum entropy, entropy, difference variance,
593% difference entropy, information measures of correlation 1, information
594% measures of correlation 2, and maximum correlation coefficient. You can
595% access the red channel contrast, for example, like this:
596%
597% channel_features=GetImageFeatures(image,1,exception);
598% contrast=channel_features[RedPixelChannel].contrast[0];
599%
600% Use MagickRelinquishMemory() to free the features buffer.
601%
602% The format of the GetImageFeatures method is:
603%
604% ChannelFeatures *GetImageFeatures(const Image *image,
605% const size_t distance,ExceptionInfo *exception)
606%
607% A description of each parameter follows:
608%
609% o image: the image.
610%
611% o distance: the distance.
612%
613% o exception: return any errors or warnings in this structure.
614%
615*/
616MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
617 const size_t distance,ExceptionInfo *exception)
618{
619 typedef struct _ChannelStatistics
620 {
621 PixelInfo
622 direction[4]; /* horizontal, vertical, left and right diagonals */
623 } ChannelStatistics;
624
625 CacheView
626 *image_view;
627
628 ChannelFeatures
629 *channel_features;
630
631 ChannelStatistics
632 **cooccurrence,
633 correlation,
634 *density_x,
635 *density_xy,
636 *density_y,
637 entropy_x,
638 entropy_xy,
639 entropy_xy1,
640 entropy_xy2,
641 entropy_y,
642 mean,
643 **Q,
644 *sum,
645 sum_squares,
646 variance;
647
648 PixelPacket
649 gray,
650 *grays;
651
652 MagickBooleanType
653 status;
654
655 ssize_t
656 i,
657 r;
658
659 size_t
660 length;
661
662 unsigned int
663 number_grays;
664
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)))
670 return((ChannelFeatures *) NULL);
671 length=MaxPixelChannels+1UL;
672 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
673 sizeof(*channel_features));
674 if (channel_features == (ChannelFeatures *) NULL)
675 {
676 (void) ThrowMagickException(exception,GetMagickModule(),
677 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
678 return(channel_features);
679 }
680 (void) memset(channel_features,0,length*
681 sizeof(*channel_features));
682 /*
683 Form grays.
684 */
685 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
686 if (grays == (PixelPacket *) NULL)
687 {
688 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
689 channel_features);
690 (void) ThrowMagickException(exception,GetMagickModule(),
691 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
692 return(channel_features);
693 }
694 for (i=0; i <= (ssize_t) MaxMap; i++)
695 {
696 grays[i].red=(~0U);
697 grays[i].green=(~0U);
698 grays[i].blue=(~0U);
699 grays[i].alpha=(~0U);
700 grays[i].black=(~0U);
701 }
702 status=MagickTrue;
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)
707#endif
708 for (r=0; r < (ssize_t) image->rows; r++)
709 {
710 const Quantum
711 *magick_restrict p;
712
713 ssize_t
714 x;
715
716 if (status == MagickFalse)
717 continue;
718 p=GetCacheViewVirtualPixels(image_view,0,r,image->columns,1,exception);
719 if (p == (const Quantum *) NULL)
720 {
721 status=MagickFalse;
722 continue;
723 }
724 for (x=0; x < (ssize_t) image->columns; x++)
725 {
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);
739 }
740 }
741 image_view=DestroyCacheView(image_view);
742 if (status == MagickFalse)
743 {
744 grays=(PixelPacket *) RelinquishMagickMemory(grays);
745 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
746 channel_features);
747 return(channel_features);
748 }
749 (void) memset(&gray,0,sizeof(gray));
750 for (i=0; i <= (ssize_t) MaxMap; i++)
751 {
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;
764 }
765 /*
766 Allocate spatial dependence matrix.
767 */
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;
779 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
780 sizeof(*cooccurrence));
781 density_x=(ChannelStatistics *) AcquireQuantumMemory(number_grays+1,
782 2*sizeof(*density_x));
783 density_xy=(ChannelStatistics *) AcquireQuantumMemory(number_grays+1,
784 2*sizeof(*density_xy));
785 density_y=(ChannelStatistics *) AcquireQuantumMemory(number_grays+1,
786 2*sizeof(*density_y));
787 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
788 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
789 if ((cooccurrence == (ChannelStatistics **) NULL) ||
790 (density_x == (ChannelStatistics *) NULL) ||
791 (density_xy == (ChannelStatistics *) NULL) ||
792 (density_y == (ChannelStatistics *) NULL) ||
793 (Q == (ChannelStatistics **) NULL) ||
794 (sum == (ChannelStatistics *) NULL))
795 {
796 if (Q != (ChannelStatistics **) NULL)
797 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
798 if (sum != (ChannelStatistics *) NULL)
799 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
800 if (density_y != (ChannelStatistics *) NULL)
801 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
802 if (density_xy != (ChannelStatistics *) NULL)
803 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
804 if (density_x != (ChannelStatistics *) NULL)
805 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
806 if (cooccurrence != (ChannelStatistics **) NULL)
807 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
808 cooccurrence);
809 grays=(PixelPacket *) RelinquishMagickMemory(grays);
810 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
811 channel_features);
812 (void) ThrowMagickException(exception,GetMagickModule(),
813 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
814 return(channel_features);
815 }
816 (void) memset(&correlation,0,sizeof(correlation));
817 (void) memset(density_x,0,2*(number_grays+1)*sizeof(*density_x));
818 (void) memset(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
819 (void) memset(density_y,0,2*(number_grays+1)*sizeof(*density_y));
820 (void) memset(&mean,0,sizeof(mean));
821 (void) memset(sum,0,number_grays*sizeof(*sum));
822 (void) memset(&sum_squares,0,sizeof(sum_squares));
823 (void) memset(density_xy,0,2*number_grays*sizeof(*density_xy));
824 (void) memset(&entropy_x,0,sizeof(entropy_x));
825 (void) memset(&entropy_xy,0,sizeof(entropy_xy));
826 (void) memset(&entropy_xy1,0,sizeof(entropy_xy1));
827 (void) memset(&entropy_xy2,0,sizeof(entropy_xy2));
828 (void) memset(&entropy_y,0,sizeof(entropy_y));
829 (void) memset(&variance,0,sizeof(variance));
830 for (i=0; i < (ssize_t) number_grays; i++)
831 {
832 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
833 sizeof(**cooccurrence));
834 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
835 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
836 (Q[i] == (ChannelStatistics *) NULL))
837 break;
838 (void) memset(cooccurrence[i],0,number_grays*
839 sizeof(**cooccurrence));
840 (void) memset(Q[i],0,number_grays*sizeof(**Q));
841 }
842 if (i < (ssize_t) number_grays)
843 {
844 for (i--; i >= 0; i--)
845 {
846 if (Q[i] != (ChannelStatistics *) NULL)
847 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
848 if (cooccurrence[i] != (ChannelStatistics *) NULL)
849 cooccurrence[i]=(ChannelStatistics *)
850 RelinquishMagickMemory(cooccurrence[i]);
851 }
852 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
853 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
854 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
855 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
856 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
857 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
858 grays=(PixelPacket *) RelinquishMagickMemory(grays);
859 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
860 channel_features);
861 (void) ThrowMagickException(exception,GetMagickModule(),
862 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
863 return(channel_features);
864 }
865 /*
866 Initialize spatial dependence matrix.
867 */
868 status=MagickTrue;
869 image_view=AcquireVirtualCacheView(image,exception);
870 for (r=0; r < (ssize_t) image->rows; r++)
871 {
872 const Quantum
873 *magick_restrict p;
874
875 ssize_t
876 x;
877
878 ssize_t
879 offset,
880 u,
881 v;
882
883 if (status == MagickFalse)
884 continue;
885 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,r,image->columns+
886 2*distance,distance+2,exception);
887 if (p == (const Quantum *) NULL)
888 {
889 status=MagickFalse;
890 continue;
891 }
892 p+=(ptrdiff_t) distance*GetPixelChannels(image);;
893 for (x=0; x < (ssize_t) image->columns; x++)
894 {
895 for (i=0; i < 4; i++)
896 {
897 switch (i)
898 {
899 case 0:
900 default:
901 {
902 /*
903 Horizontal adjacency.
904 */
905 offset=(ssize_t) distance;
906 break;
907 }
908 case 1:
909 {
910 /*
911 Vertical adjacency.
912 */
913 offset=(ssize_t) (image->columns+2*distance);
914 break;
915 }
916 case 2:
917 {
918 /*
919 Right diagonal adjacency.
920 */
921 offset=(ssize_t) ((image->columns+2*distance)-distance);
922 break;
923 }
924 case 3:
925 {
926 /*
927 Left diagonal adjacency.
928 */
929 offset=(ssize_t) ((image->columns+2*distance)+distance);
930 break;
931 }
932 }
933 u=0;
934 v=0;
935 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
936 u++;
937 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*(ssize_t) GetPixelChannels(image))))
938 v++;
939 cooccurrence[u][v].direction[i].red++;
940 cooccurrence[v][u].direction[i].red++;
941 u=0;
942 v=0;
943 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
944 u++;
945 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*(ssize_t) GetPixelChannels(image))))
946 v++;
947 cooccurrence[u][v].direction[i].green++;
948 cooccurrence[v][u].direction[i].green++;
949 u=0;
950 v=0;
951 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
952 u++;
953 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*(ssize_t) GetPixelChannels(image))))
954 v++;
955 cooccurrence[u][v].direction[i].blue++;
956 cooccurrence[v][u].direction[i].blue++;
957 if (image->colorspace == CMYKColorspace)
958 {
959 u=0;
960 v=0;
961 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
962 u++;
963 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*(ssize_t) GetPixelChannels(image))))
964 v++;
965 cooccurrence[u][v].direction[i].black++;
966 cooccurrence[v][u].direction[i].black++;
967 }
968 if (image->alpha_trait != UndefinedPixelTrait)
969 {
970 u=0;
971 v=0;
972 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
973 u++;
974 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*(ssize_t) GetPixelChannels(image))))
975 v++;
976 cooccurrence[u][v].direction[i].alpha++;
977 cooccurrence[v][u].direction[i].alpha++;
978 }
979 }
980 p+=(ptrdiff_t) GetPixelChannels(image);
981 }
982 }
983 grays=(PixelPacket *) RelinquishMagickMemory(grays);
984 image_view=DestroyCacheView(image_view);
985 if (status == MagickFalse)
986 {
987 for (i=0; i < (ssize_t) number_grays; i++)
988 cooccurrence[i]=(ChannelStatistics *)
989 RelinquishMagickMemory(cooccurrence[i]);
990 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
991 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
992 channel_features);
993 (void) ThrowMagickException(exception,GetMagickModule(),
994 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
995 return(channel_features);
996 }
997 /*
998 Normalize spatial dependence matrix.
999 */
1000 for (i=0; i < 4; i++)
1001 {
1002 double
1003 normalize;
1004
1005 ssize_t
1006 y;
1007
1008 switch (i)
1009 {
1010 case 0:
1011 default:
1012 {
1013 /*
1014 Horizontal adjacency.
1015 */
1016 normalize=2.0*image->rows*(image->columns-distance);
1017 break;
1018 }
1019 case 1:
1020 {
1021 /*
1022 Vertical adjacency.
1023 */
1024 normalize=2.0*(image->rows-distance)*image->columns;
1025 break;
1026 }
1027 case 2:
1028 {
1029 /*
1030 Right diagonal adjacency.
1031 */
1032 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1033 break;
1034 }
1035 case 3:
1036 {
1037 /*
1038 Left diagonal adjacency.
1039 */
1040 normalize=2.0*(image->rows-distance)*(image->columns-distance);
1041 break;
1042 }
1043 }
1044 normalize=MagickSafeReciprocal(normalize);
1045 for (y=0; y < (ssize_t) number_grays; y++)
1046 {
1047 ssize_t
1048 x;
1049
1050 for (x=0; x < (ssize_t) number_grays; x++)
1051 {
1052 cooccurrence[x][y].direction[i].red*=normalize;
1053 cooccurrence[x][y].direction[i].green*=normalize;
1054 cooccurrence[x][y].direction[i].blue*=normalize;
1055 if (image->colorspace == CMYKColorspace)
1056 cooccurrence[x][y].direction[i].black*=normalize;
1057 if (image->alpha_trait != UndefinedPixelTrait)
1058 cooccurrence[x][y].direction[i].alpha*=normalize;
1059 }
1060 }
1061 }
1062 /*
1063 Compute texture features.
1064 */
1065#if defined(MAGICKCORE_OPENMP_SUPPORT)
1066 #pragma omp parallel for schedule(static) shared(status) \
1067 magick_number_threads(image,image,number_grays,1)
1068#endif
1069 for (i=0; i < 4; i++)
1070 {
1071 ssize_t
1072 y;
1073
1074 for (y=0; y < (ssize_t) number_grays; y++)
1075 {
1076 ssize_t
1077 x;
1078
1079 for (x=0; x < (ssize_t) number_grays; x++)
1080 {
1081 /*
1082 Angular second moment: measure of homogeneity of the image.
1083 */
1084 channel_features[RedPixelChannel].angular_second_moment[i]+=
1085 cooccurrence[x][y].direction[i].red*
1086 cooccurrence[x][y].direction[i].red;
1087 channel_features[GreenPixelChannel].angular_second_moment[i]+=
1088 cooccurrence[x][y].direction[i].green*
1089 cooccurrence[x][y].direction[i].green;
1090 channel_features[BluePixelChannel].angular_second_moment[i]+=
1091 cooccurrence[x][y].direction[i].blue*
1092 cooccurrence[x][y].direction[i].blue;
1093 if (image->colorspace == CMYKColorspace)
1094 channel_features[BlackPixelChannel].angular_second_moment[i]+=
1095 cooccurrence[x][y].direction[i].black*
1096 cooccurrence[x][y].direction[i].black;
1097 if (image->alpha_trait != UndefinedPixelTrait)
1098 channel_features[AlphaPixelChannel].angular_second_moment[i]+=
1099 cooccurrence[x][y].direction[i].alpha*
1100 cooccurrence[x][y].direction[i].alpha;
1101 /*
1102 Correlation: measure of linear-dependencies in the image.
1103 */
1104 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1105 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1106 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1107 if (image->colorspace == CMYKColorspace)
1108 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
1109 if (image->alpha_trait != UndefinedPixelTrait)
1110 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
1111 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
1112 correlation.direction[i].green+=x*y*
1113 cooccurrence[x][y].direction[i].green;
1114 correlation.direction[i].blue+=x*y*
1115 cooccurrence[x][y].direction[i].blue;
1116 if (image->colorspace == CMYKColorspace)
1117 correlation.direction[i].black+=x*y*
1118 cooccurrence[x][y].direction[i].black;
1119 if (image->alpha_trait != UndefinedPixelTrait)
1120 correlation.direction[i].alpha+=x*y*
1121 cooccurrence[x][y].direction[i].alpha;
1122 /*
1123 Inverse Difference Moment.
1124 */
1125 channel_features[RedPixelChannel].inverse_difference_moment[i]+=
1126 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
1127 channel_features[GreenPixelChannel].inverse_difference_moment[i]+=
1128 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
1129 channel_features[BluePixelChannel].inverse_difference_moment[i]+=
1130 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
1131 if (image->colorspace == CMYKColorspace)
1132 channel_features[BlackPixelChannel].inverse_difference_moment[i]+=
1133 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
1134 if (image->alpha_trait != UndefinedPixelTrait)
1135 channel_features[AlphaPixelChannel].inverse_difference_moment[i]+=
1136 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
1137 /*
1138 Sum average.
1139 */
1140 density_xy[y+x+2].direction[i].red+=
1141 cooccurrence[x][y].direction[i].red;
1142 density_xy[y+x+2].direction[i].green+=
1143 cooccurrence[x][y].direction[i].green;
1144 density_xy[y+x+2].direction[i].blue+=
1145 cooccurrence[x][y].direction[i].blue;
1146 if (image->colorspace == CMYKColorspace)
1147 density_xy[y+x+2].direction[i].black+=
1148 cooccurrence[x][y].direction[i].black;
1149 if (image->alpha_trait != UndefinedPixelTrait)
1150 density_xy[y+x+2].direction[i].alpha+=
1151 cooccurrence[x][y].direction[i].alpha;
1152 /*
1153 Entropy.
1154 */
1155 channel_features[RedPixelChannel].entropy[i]-=
1156 cooccurrence[x][y].direction[i].red*
1157 log2(cooccurrence[x][y].direction[i].red);
1158 channel_features[GreenPixelChannel].entropy[i]-=
1159 cooccurrence[x][y].direction[i].green*
1160 log2(cooccurrence[x][y].direction[i].green);
1161 channel_features[BluePixelChannel].entropy[i]-=
1162 cooccurrence[x][y].direction[i].blue*
1163 log2(cooccurrence[x][y].direction[i].blue);
1164 if (image->colorspace == CMYKColorspace)
1165 channel_features[BlackPixelChannel].entropy[i]-=
1166 cooccurrence[x][y].direction[i].black*
1167 log2(cooccurrence[x][y].direction[i].black);
1168 if (image->alpha_trait != UndefinedPixelTrait)
1169 channel_features[AlphaPixelChannel].entropy[i]-=
1170 cooccurrence[x][y].direction[i].alpha*
1171 log2(cooccurrence[x][y].direction[i].alpha);
1172 /*
1173 Information Measures of Correlation.
1174 */
1175 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
1176 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
1177 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1178 if (image->alpha_trait != UndefinedPixelTrait)
1179 density_x[x].direction[i].alpha+=
1180 cooccurrence[x][y].direction[i].alpha;
1181 if (image->colorspace == CMYKColorspace)
1182 density_x[x].direction[i].black+=
1183 cooccurrence[x][y].direction[i].black;
1184 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
1185 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
1186 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1187 if (image->colorspace == CMYKColorspace)
1188 density_y[y].direction[i].black+=
1189 cooccurrence[x][y].direction[i].black;
1190 if (image->alpha_trait != UndefinedPixelTrait)
1191 density_y[y].direction[i].alpha+=
1192 cooccurrence[x][y].direction[i].alpha;
1193 }
1194 mean.direction[i].red+=y*sum[y].direction[i].red;
1195 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
1196 mean.direction[i].green+=y*sum[y].direction[i].green;
1197 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
1198 mean.direction[i].blue+=y*sum[y].direction[i].blue;
1199 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
1200 if (image->colorspace == CMYKColorspace)
1201 {
1202 mean.direction[i].black+=y*sum[y].direction[i].black;
1203 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
1204 }
1205 if (image->alpha_trait != UndefinedPixelTrait)
1206 {
1207 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
1208 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
1209 }
1210 }
1211 /*
1212 Correlation: measure of linear-dependencies in the image.
1213 */
1214 channel_features[RedPixelChannel].correlation[i]=
1215 (correlation.direction[i].red-mean.direction[i].red*
1216 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
1217 (mean.direction[i].red*mean.direction[i].red))*sqrt(
1218 sum_squares.direction[i].red-(mean.direction[i].red*
1219 mean.direction[i].red)));
1220 channel_features[GreenPixelChannel].correlation[i]=
1221 (correlation.direction[i].green-mean.direction[i].green*
1222 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
1223 (mean.direction[i].green*mean.direction[i].green))*sqrt(
1224 sum_squares.direction[i].green-(mean.direction[i].green*
1225 mean.direction[i].green)));
1226 channel_features[BluePixelChannel].correlation[i]=
1227 (correlation.direction[i].blue-mean.direction[i].blue*
1228 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
1229 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
1230 sum_squares.direction[i].blue-(mean.direction[i].blue*
1231 mean.direction[i].blue)));
1232 if (image->colorspace == CMYKColorspace)
1233 channel_features[BlackPixelChannel].correlation[i]=
1234 (correlation.direction[i].black-mean.direction[i].black*
1235 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
1236 (mean.direction[i].black*mean.direction[i].black))*sqrt(
1237 sum_squares.direction[i].black-(mean.direction[i].black*
1238 mean.direction[i].black)));
1239 if (image->alpha_trait != UndefinedPixelTrait)
1240 channel_features[AlphaPixelChannel].correlation[i]=
1241 (correlation.direction[i].alpha-mean.direction[i].alpha*
1242 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
1243 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
1244 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
1245 mean.direction[i].alpha)));
1246 }
1247 /*
1248 Compute more texture features.
1249 */
1250#if defined(MAGICKCORE_OPENMP_SUPPORT)
1251 #pragma omp parallel for schedule(static) shared(status) \
1252 magick_number_threads(image,image,number_grays,1)
1253#endif
1254 for (i=0; i < 4; i++)
1255 {
1256 ssize_t
1257 x;
1258
1259 for (x=2; x < (ssize_t) (2*number_grays); x++)
1260 {
1261 /*
1262 Sum average.
1263 */
1264 channel_features[RedPixelChannel].sum_average[i]+=
1265 x*density_xy[x].direction[i].red;
1266 channel_features[GreenPixelChannel].sum_average[i]+=
1267 x*density_xy[x].direction[i].green;
1268 channel_features[BluePixelChannel].sum_average[i]+=
1269 x*density_xy[x].direction[i].blue;
1270 if (image->colorspace == CMYKColorspace)
1271 channel_features[BlackPixelChannel].sum_average[i]+=
1272 x*density_xy[x].direction[i].black;
1273 if (image->alpha_trait != UndefinedPixelTrait)
1274 channel_features[AlphaPixelChannel].sum_average[i]+=
1275 x*density_xy[x].direction[i].alpha;
1276 /*
1277 Sum entropy.
1278 */
1279 channel_features[RedPixelChannel].sum_entropy[i]-=
1280 density_xy[x].direction[i].red*
1281 log2(density_xy[x].direction[i].red);
1282 channel_features[GreenPixelChannel].sum_entropy[i]-=
1283 density_xy[x].direction[i].green*
1284 log2(density_xy[x].direction[i].green);
1285 channel_features[BluePixelChannel].sum_entropy[i]-=
1286 density_xy[x].direction[i].blue*
1287 log2(density_xy[x].direction[i].blue);
1288 if (image->colorspace == CMYKColorspace)
1289 channel_features[BlackPixelChannel].sum_entropy[i]-=
1290 density_xy[x].direction[i].black*
1291 log2(density_xy[x].direction[i].black);
1292 if (image->alpha_trait != UndefinedPixelTrait)
1293 channel_features[AlphaPixelChannel].sum_entropy[i]-=
1294 density_xy[x].direction[i].alpha*
1295 log2(density_xy[x].direction[i].alpha);
1296 /*
1297 Sum variance.
1298 */
1299 channel_features[RedPixelChannel].sum_variance[i]+=
1300 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1301 (x-channel_features[RedPixelChannel].sum_entropy[i])*
1302 density_xy[x].direction[i].red;
1303 channel_features[GreenPixelChannel].sum_variance[i]+=
1304 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1305 (x-channel_features[GreenPixelChannel].sum_entropy[i])*
1306 density_xy[x].direction[i].green;
1307 channel_features[BluePixelChannel].sum_variance[i]+=
1308 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1309 (x-channel_features[BluePixelChannel].sum_entropy[i])*
1310 density_xy[x].direction[i].blue;
1311 if (image->colorspace == CMYKColorspace)
1312 channel_features[BlackPixelChannel].sum_variance[i]+=
1313 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1314 (x-channel_features[BlackPixelChannel].sum_entropy[i])*
1315 density_xy[x].direction[i].black;
1316 if (image->alpha_trait != UndefinedPixelTrait)
1317 channel_features[AlphaPixelChannel].sum_variance[i]+=
1318 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1319 (x-channel_features[AlphaPixelChannel].sum_entropy[i])*
1320 density_xy[x].direction[i].alpha;
1321 }
1322 }
1323 /*
1324 Compute more texture features.
1325 */
1326#if defined(MAGICKCORE_OPENMP_SUPPORT)
1327 #pragma omp parallel for schedule(static) shared(status) \
1328 magick_number_threads(image,image,number_grays,1)
1329#endif
1330 for (i=0; i < 4; i++)
1331 {
1332 ssize_t
1333 y;
1334
1335 for (y=0; y < (ssize_t) number_grays; y++)
1336 {
1337 ssize_t
1338 x;
1339
1340 for (x=0; x < (ssize_t) number_grays; x++)
1341 {
1342 /*
1343 Sum of Squares: Variance
1344 */
1345 variance.direction[i].red+=(y-mean.direction[i].red+1)*
1346 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
1347 variance.direction[i].green+=(y-mean.direction[i].green+1)*
1348 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
1349 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
1350 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
1351 if (image->colorspace == CMYKColorspace)
1352 variance.direction[i].black+=(y-mean.direction[i].black+1)*
1353 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
1354 if (image->alpha_trait != UndefinedPixelTrait)
1355 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
1356 (y-mean.direction[i].alpha+1)*
1357 cooccurrence[x][y].direction[i].alpha;
1358 /*
1359 Sum average / Difference Variance.
1360 */
1361 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
1362 cooccurrence[x][y].direction[i].red;
1363 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
1364 cooccurrence[x][y].direction[i].green;
1365 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
1366 cooccurrence[x][y].direction[i].blue;
1367 if (image->colorspace == CMYKColorspace)
1368 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
1369 cooccurrence[x][y].direction[i].black;
1370 if (image->alpha_trait != UndefinedPixelTrait)
1371 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
1372 cooccurrence[x][y].direction[i].alpha;
1373 /*
1374 Information Measures of Correlation.
1375 */
1376 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
1377 log2(cooccurrence[x][y].direction[i].red);
1378 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
1379 log2(cooccurrence[x][y].direction[i].green);
1380 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
1381 log2(cooccurrence[x][y].direction[i].blue);
1382 if (image->colorspace == CMYKColorspace)
1383 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
1384 log2(cooccurrence[x][y].direction[i].black);
1385 if (image->alpha_trait != UndefinedPixelTrait)
1386 entropy_xy.direction[i].alpha-=
1387 cooccurrence[x][y].direction[i].alpha*log2(
1388 cooccurrence[x][y].direction[i].alpha);
1389 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
1390 log2(density_x[x].direction[i].red*density_y[y].direction[i].red));
1391 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
1392 log2(density_x[x].direction[i].green*
1393 density_y[y].direction[i].green));
1394 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
1395 log2(density_x[x].direction[i].blue*density_y[y].direction[i].blue));
1396 if (image->colorspace == CMYKColorspace)
1397 entropy_xy1.direction[i].black-=(
1398 cooccurrence[x][y].direction[i].black*log2(
1399 density_x[x].direction[i].black*density_y[y].direction[i].black));
1400 if (image->alpha_trait != UndefinedPixelTrait)
1401 entropy_xy1.direction[i].alpha-=(
1402 cooccurrence[x][y].direction[i].alpha*log2(
1403 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1404 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
1405 density_y[y].direction[i].red*log2(density_x[x].direction[i].red*
1406 density_y[y].direction[i].red));
1407 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
1408 density_y[y].direction[i].green*log2(density_x[x].direction[i].green*
1409 density_y[y].direction[i].green));
1410 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
1411 density_y[y].direction[i].blue*log2(density_x[x].direction[i].blue*
1412 density_y[y].direction[i].blue));
1413 if (image->colorspace == CMYKColorspace)
1414 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
1415 density_y[y].direction[i].black*log2(
1416 density_x[x].direction[i].black*density_y[y].direction[i].black));
1417 if (image->alpha_trait != UndefinedPixelTrait)
1418 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
1419 density_y[y].direction[i].alpha*log2(
1420 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha));
1421 }
1422 }
1423 channel_features[RedPixelChannel].variance_sum_of_squares[i]=
1424 variance.direction[i].red;
1425 channel_features[GreenPixelChannel].variance_sum_of_squares[i]=
1426 variance.direction[i].green;
1427 channel_features[BluePixelChannel].variance_sum_of_squares[i]=
1428 variance.direction[i].blue;
1429 if (image->colorspace == CMYKColorspace)
1430 channel_features[BlackPixelChannel].variance_sum_of_squares[i]=
1431 variance.direction[i].black;
1432 if (image->alpha_trait != UndefinedPixelTrait)
1433 channel_features[AlphaPixelChannel].variance_sum_of_squares[i]=
1434 variance.direction[i].alpha;
1435 }
1436 /*
1437 Compute more texture features.
1438 */
1439 (void) memset(&variance,0,sizeof(variance));
1440 (void) memset(&sum_squares,0,sizeof(sum_squares));
1441#if defined(MAGICKCORE_OPENMP_SUPPORT)
1442 #pragma omp parallel for schedule(static) shared(status) \
1443 magick_number_threads(image,image,number_grays,1)
1444#endif
1445 for (i=0; i < 4; i++)
1446 {
1447 ssize_t
1448 x;
1449
1450 for (x=0; x < (ssize_t) number_grays; x++)
1451 {
1452 /*
1453 Difference variance.
1454 */
1455 variance.direction[i].red+=density_xy[x].direction[i].red;
1456 variance.direction[i].green+=density_xy[x].direction[i].green;
1457 variance.direction[i].blue+=density_xy[x].direction[i].blue;
1458 if (image->colorspace == CMYKColorspace)
1459 variance.direction[i].black+=density_xy[x].direction[i].black;
1460 if (image->alpha_trait != UndefinedPixelTrait)
1461 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
1462 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
1463 density_xy[x].direction[i].red;
1464 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
1465 density_xy[x].direction[i].green;
1466 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
1467 density_xy[x].direction[i].blue;
1468 if (image->colorspace == CMYKColorspace)
1469 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1470 density_xy[x].direction[i].black;
1471 if (image->alpha_trait != UndefinedPixelTrait)
1472 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1473 density_xy[x].direction[i].alpha;
1474 /*
1475 Difference entropy.
1476 */
1477 channel_features[RedPixelChannel].difference_entropy[i]-=
1478 density_xy[x].direction[i].red*
1479 log2(density_xy[x].direction[i].red);
1480 channel_features[GreenPixelChannel].difference_entropy[i]-=
1481 density_xy[x].direction[i].green*
1482 log2(density_xy[x].direction[i].green);
1483 channel_features[BluePixelChannel].difference_entropy[i]-=
1484 density_xy[x].direction[i].blue*
1485 log2(density_xy[x].direction[i].blue);
1486 if (image->colorspace == CMYKColorspace)
1487 channel_features[BlackPixelChannel].difference_entropy[i]-=
1488 density_xy[x].direction[i].black*
1489 log2(density_xy[x].direction[i].black);
1490 if (image->alpha_trait != UndefinedPixelTrait)
1491 channel_features[AlphaPixelChannel].difference_entropy[i]-=
1492 density_xy[x].direction[i].alpha*
1493 log2(density_xy[x].direction[i].alpha);
1494 /*
1495 Information Measures of Correlation.
1496 */
1497 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1498 log2(density_x[x].direction[i].red));
1499 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1500 log2(density_x[x].direction[i].green));
1501 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1502 log2(density_x[x].direction[i].blue));
1503 if (image->colorspace == CMYKColorspace)
1504 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1505 log2(density_x[x].direction[i].black));
1506 if (image->alpha_trait != UndefinedPixelTrait)
1507 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1508 log2(density_x[x].direction[i].alpha));
1509 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1510 log2(density_y[x].direction[i].red));
1511 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1512 log2(density_y[x].direction[i].green));
1513 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1514 log2(density_y[x].direction[i].blue));
1515 if (image->colorspace == CMYKColorspace)
1516 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1517 log2(density_y[x].direction[i].black));
1518 if (image->alpha_trait != UndefinedPixelTrait)
1519 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1520 log2(density_y[x].direction[i].alpha));
1521 }
1522 /*
1523 Difference variance.
1524 */
1525 channel_features[RedPixelChannel].difference_variance[i]=
1526 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
1527 (variance.direction[i].red*variance.direction[i].red))/
1528 ((double) number_grays*number_grays*number_grays*number_grays);
1529 channel_features[GreenPixelChannel].difference_variance[i]=
1530 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
1531 (variance.direction[i].green*variance.direction[i].green))/
1532 ((double) number_grays*number_grays*number_grays*number_grays);
1533 channel_features[BluePixelChannel].difference_variance[i]=
1534 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
1535 (variance.direction[i].blue*variance.direction[i].blue))/
1536 ((double) number_grays*number_grays*number_grays*number_grays);
1537 if (image->colorspace == CMYKColorspace)
1538 channel_features[BlackPixelChannel].difference_variance[i]=
1539 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1540 (variance.direction[i].black*variance.direction[i].black))/
1541 ((double) number_grays*number_grays*number_grays*number_grays);
1542 if (image->alpha_trait != UndefinedPixelTrait)
1543 channel_features[AlphaPixelChannel].difference_variance[i]=
1544 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1545 (variance.direction[i].alpha*variance.direction[i].alpha))/
1546 ((double) number_grays*number_grays*number_grays*number_grays);
1547 /*
1548 Information Measures of Correlation.
1549 */
1550 channel_features[RedPixelChannel].measure_of_correlation_1[i]=
1551 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1552 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1553 entropy_x.direction[i].red : entropy_y.direction[i].red);
1554 channel_features[GreenPixelChannel].measure_of_correlation_1[i]=
1555 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1556 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1557 entropy_x.direction[i].green : entropy_y.direction[i].green);
1558 channel_features[BluePixelChannel].measure_of_correlation_1[i]=
1559 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1560 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1561 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
1562 if (image->colorspace == CMYKColorspace)
1563 channel_features[BlackPixelChannel].measure_of_correlation_1[i]=
1564 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1565 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1566 entropy_x.direction[i].black : entropy_y.direction[i].black);
1567 if (image->alpha_trait != UndefinedPixelTrait)
1568 channel_features[AlphaPixelChannel].measure_of_correlation_1[i]=
1569 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1570 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1571 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
1572 channel_features[RedPixelChannel].measure_of_correlation_2[i]=
1573 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].red-
1574 entropy_xy.direction[i].red)))));
1575 channel_features[GreenPixelChannel].measure_of_correlation_2[i]=
1576 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].green-
1577 entropy_xy.direction[i].green)))));
1578 channel_features[BluePixelChannel].measure_of_correlation_2[i]=
1579 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].blue-
1580 entropy_xy.direction[i].blue)))));
1581 if (image->colorspace == CMYKColorspace)
1582 channel_features[BlackPixelChannel].measure_of_correlation_2[i]=
1583 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].black-
1584 entropy_xy.direction[i].black)))));
1585 if (image->alpha_trait != UndefinedPixelTrait)
1586 channel_features[AlphaPixelChannel].measure_of_correlation_2[i]=
1587 (sqrt(fabs(1.0-exp(-2.0*(double) (entropy_xy2.direction[i].alpha-
1588 entropy_xy.direction[i].alpha)))));
1589 }
1590 /*
1591 Compute more texture features.
1592 */
1593#if defined(MAGICKCORE_OPENMP_SUPPORT)
1594 #pragma omp parallel for schedule(static) shared(status) \
1595 magick_number_threads(image,image,number_grays,1)
1596#endif
1597 for (i=0; i < 4; i++)
1598 {
1599 ssize_t
1600 z;
1601
1602 for (z=0; z < (ssize_t) number_grays; z++)
1603 {
1604 ssize_t
1605 y;
1606
1607 ChannelStatistics
1608 pixel;
1609
1610 (void) memset(&pixel,0,sizeof(pixel));
1611 for (y=0; y < (ssize_t) number_grays; y++)
1612 {
1613 ssize_t
1614 x;
1615
1616 for (x=0; x < (ssize_t) number_grays; x++)
1617 {
1618 /*
1619 Contrast: amount of local variations present in an image.
1620 */
1621 if (((y-x) == z) || ((x-y) == z))
1622 {
1623 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1624 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1625 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
1626 if (image->colorspace == CMYKColorspace)
1627 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
1628 if (image->alpha_trait != UndefinedPixelTrait)
1629 pixel.direction[i].alpha+=
1630 cooccurrence[x][y].direction[i].alpha;
1631 }
1632 /*
1633 Maximum Correlation Coefficient.
1634 */
1635 if ((fabs(density_x[z].direction[i].red) > MagickEpsilon) &&
1636 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1637 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1638 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1639 density_y[x].direction[i].red;
1640 if ((fabs(density_x[z].direction[i].green) > MagickEpsilon) &&
1641 (fabs(density_y[x].direction[i].red) > MagickEpsilon))
1642 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1643 cooccurrence[y][x].direction[i].green/
1644 density_x[z].direction[i].green/density_y[x].direction[i].red;
1645 if ((fabs(density_x[z].direction[i].blue) > MagickEpsilon) &&
1646 (fabs(density_y[x].direction[i].blue) > MagickEpsilon))
1647 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1648 cooccurrence[y][x].direction[i].blue/
1649 density_x[z].direction[i].blue/density_y[x].direction[i].blue;
1650 if (image->colorspace == CMYKColorspace)
1651 if ((fabs(density_x[z].direction[i].black) > MagickEpsilon) &&
1652 (fabs(density_y[x].direction[i].black) > MagickEpsilon))
1653 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1654 cooccurrence[y][x].direction[i].black/
1655 density_x[z].direction[i].black/density_y[x].direction[i].black;
1656 if (image->alpha_trait != UndefinedPixelTrait)
1657 if ((fabs(density_x[z].direction[i].alpha) > MagickEpsilon) &&
1658 (fabs(density_y[x].direction[i].alpha) > MagickEpsilon))
1659 Q[z][y].direction[i].alpha+=
1660 cooccurrence[z][x].direction[i].alpha*
1661 cooccurrence[y][x].direction[i].alpha/
1662 density_x[z].direction[i].alpha/
1663 density_y[x].direction[i].alpha;
1664 }
1665 }
1666 channel_features[RedPixelChannel].contrast[i]+=z*z*
1667 pixel.direction[i].red;
1668 channel_features[GreenPixelChannel].contrast[i]+=z*z*
1669 pixel.direction[i].green;
1670 channel_features[BluePixelChannel].contrast[i]+=z*z*
1671 pixel.direction[i].blue;
1672 if (image->colorspace == CMYKColorspace)
1673 channel_features[BlackPixelChannel].contrast[i]+=z*z*
1674 pixel.direction[i].black;
1675 if (image->alpha_trait != UndefinedPixelTrait)
1676 channel_features[AlphaPixelChannel].contrast[i]+=z*z*
1677 pixel.direction[i].alpha;
1678 }
1679 /*
1680 Maximum Correlation Coefficient.
1681 Future: return second largest eigenvalue of Q.
1682 */
1683 channel_features[RedPixelChannel].maximum_correlation_coefficient[i]=
1684 sqrt(-1.0);
1685 channel_features[GreenPixelChannel].maximum_correlation_coefficient[i]=
1686 sqrt(-1.0);
1687 channel_features[BluePixelChannel].maximum_correlation_coefficient[i]=
1688 sqrt(-1.0);
1689 if (image->colorspace == CMYKColorspace)
1690 channel_features[BlackPixelChannel].maximum_correlation_coefficient[i]=
1691 sqrt(-1.0);
1692 if (image->alpha_trait != UndefinedPixelTrait)
1693 channel_features[AlphaPixelChannel].maximum_correlation_coefficient[i]=
1694 sqrt(-1.0);
1695 }
1696 /*
1697 Relinquish resources.
1698 */
1699 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
1700 for (i=0; i < (ssize_t) number_grays; i++)
1701 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1702 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1703 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1704 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1705 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
1706 for (i=0; i < (ssize_t) number_grays; i++)
1707 cooccurrence[i]=(ChannelStatistics *)
1708 RelinquishMagickMemory(cooccurrence[i]);
1709 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
1710 return(channel_features);
1711}
1712
1713/*
1714%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1715% %
1716% %
1717% %
1718% H o u g h L i n e I m a g e %
1719% %
1720% %
1721% %
1722%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1723%
1724% HoughLineImage() can be used in conjunction with any binary edge extracted
1725% image (we recommend Canny) to identify lines in the image. The algorithm
1726% accumulates counts for every white pixel for every possible orientation (for
1727% angles from 0 to 179 in 1 degree increments) and distance from the center of
1728% the image to the corner (in 1 px increments) and stores the counts in an
1729% accumulator matrix of angle vs distance. The size of the accumulator is
1730% 180x(diagonal/2). Next it searches this space for peaks in counts and
1731% converts the locations of the peaks to slope and intercept in the normal
1732% x,y input image space. Use the slope/intercepts to find the endpoints
1733% clipped to the bounds of the image. The lines are then drawn. The counts
1734% are a measure of the length of the lines.
1735%
1736% The format of the HoughLineImage method is:
1737%
1738% Image *HoughLineImage(const Image *image,const size_t width,
1739% const size_t height,const size_t threshold,ExceptionInfo *exception)
1740%
1741% A description of each parameter follows:
1742%
1743% o image: the image.
1744%
1745% o width, height: find line pairs as local maxima in this neighborhood.
1746%
1747% o threshold: the line count threshold.
1748%
1749% o exception: return any errors or warnings in this structure.
1750%
1751*/
1752
1753static inline double MagickRound(double x)
1754{
1755 /*
1756 Round the fraction to nearest integer.
1757 */
1758 if ((x-floor(x)) < (ceil(x)-x))
1759 return(floor(x));
1760 return(ceil(x));
1761}
1762
1763static Image *RenderHoughLines(const ImageInfo *image_info,const size_t columns,
1764 const size_t rows,ExceptionInfo *exception)
1765{
1766#define BoundingBox "viewbox"
1767
1768 DrawInfo
1769 *draw_info;
1770
1771 Image
1772 *image;
1773
1774 MagickBooleanType
1775 status;
1776
1777 /*
1778 Open image.
1779 */
1780 image=AcquireImage(image_info,exception);
1781 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
1782 if (status == MagickFalse)
1783 {
1784 image=DestroyImageList(image);
1785 return((Image *) NULL);
1786 }
1787 image->columns=columns;
1788 image->rows=rows;
1789 draw_info=CloneDrawInfo(image_info,(DrawInfo *) NULL);
1790 draw_info->affine.sx=image->resolution.x == 0.0 ? 1.0 : image->resolution.x/
1791 DefaultResolution;
1792 draw_info->affine.sy=image->resolution.y == 0.0 ? 1.0 : image->resolution.y/
1793 DefaultResolution;
1794 image->columns=CastDoubleToSizeT(draw_info->affine.sx*image->columns);
1795 image->rows=CastDoubleToSizeT(draw_info->affine.sy*image->rows);
1796 status=SetImageExtent(image,image->columns,image->rows,exception);
1797 if (status == MagickFalse)
1798 return(DestroyImageList(image));
1799 if (SetImageBackgroundColor(image,exception) == MagickFalse)
1800 {
1801 draw_info=DestroyDrawInfo(draw_info);
1802 image=DestroyImageList(image);
1803 return((Image *) NULL);
1804 }
1805 /*
1806 Render drawing.
1807 */
1808 if (GetBlobStreamData(image) == (unsigned char *) NULL)
1809 draw_info->primitive=FileToString(image->filename,~0UL,exception);
1810 else
1811 {
1812 draw_info->primitive=(char *) AcquireQuantumMemory(1,(size_t)
1813 GetBlobSize(image)+1);
1814 if (draw_info->primitive != (char *) NULL)
1815 {
1816 (void) memcpy(draw_info->primitive,GetBlobStreamData(image),
1817 (size_t) GetBlobSize(image));
1818 draw_info->primitive[GetBlobSize(image)]='\0';
1819 }
1820 }
1821 (void) DrawImage(image,draw_info,exception);
1822 draw_info=DestroyDrawInfo(draw_info);
1823 if (CloseBlob(image) == MagickFalse)
1824 image=DestroyImageList(image);
1825 return(GetFirstImageInList(image));
1826}
1827
1828MagickExport Image *HoughLineImage(const Image *image,const size_t width,
1829 const size_t height,const size_t threshold,ExceptionInfo *exception)
1830{
1831#define HoughLineImageTag "HoughLine/Image"
1832
1833 CacheView
1834 *image_view;
1835
1836 char
1837 message[MagickPathExtent],
1838 path[MagickPathExtent];
1839
1840 const char
1841 *artifact;
1842
1843 double
1844 hough_height;
1845
1846 Image
1847 *lines_image = NULL;
1848
1849 ImageInfo
1850 *image_info;
1851
1852 int
1853 file;
1854
1855 MagickBooleanType
1856 status;
1857
1858 MagickOffsetType
1859 progress;
1860
1861 MatrixInfo
1862 *accumulator;
1863
1864 PointInfo
1865 center;
1866
1867 ssize_t
1868 y;
1869
1870 size_t
1871 accumulator_height,
1872 accumulator_width,
1873 line_count;
1874
1875 /*
1876 Create the accumulator.
1877 */
1878 assert(image != (const Image *) NULL);
1879 assert(image->signature == MagickCoreSignature);
1880 assert(exception != (ExceptionInfo *) NULL);
1881 assert(exception->signature == MagickCoreSignature);
1882 if (IsEventLogging() != MagickFalse)
1883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
1884 accumulator_width=180;
1885 hough_height=((sqrt(2.0)*(double) (image->rows > image->columns ?
1886 image->rows : image->columns))/2.0);
1887 accumulator_height=(size_t) (2.0*hough_height);
1888 accumulator=AcquireMatrixInfo(accumulator_width,accumulator_height,
1889 sizeof(double),exception);
1890 if (accumulator == (MatrixInfo *) NULL)
1891 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1892 if (NullMatrix(accumulator) == MagickFalse)
1893 {
1894 accumulator=DestroyMatrixInfo(accumulator);
1895 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
1896 }
1897 /*
1898 Populate the accumulator.
1899 */
1900 status=MagickTrue;
1901 progress=0;
1902 center.x=(double) image->columns/2.0;
1903 center.y=(double) image->rows/2.0;
1904 image_view=AcquireVirtualCacheView(image,exception);
1905 for (y=0; y < (ssize_t) image->rows; y++)
1906 {
1907 const Quantum
1908 *magick_restrict p;
1909
1910 ssize_t
1911 x;
1912
1913 if (status == MagickFalse)
1914 continue;
1915 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
1916 if (p == (Quantum *) NULL)
1917 {
1918 status=MagickFalse;
1919 continue;
1920 }
1921 for (x=0; x < (ssize_t) image->columns; x++)
1922 {
1923 if (GetPixelIntensity(image,p) > ((double) QuantumRange/2.0))
1924 {
1925 ssize_t
1926 i;
1927
1928 for (i=0; i < 180; i++)
1929 {
1930 double
1931 count,
1932 radius;
1933
1934 radius=(((double) x-center.x)*cos(DegreesToRadians((double) i)))+
1935 (((double) y-center.y)*sin(DegreesToRadians((double) i)));
1936 (void) GetMatrixElement(accumulator,i,(ssize_t)
1937 MagickRound(radius+hough_height),&count);
1938 count++;
1939 (void) SetMatrixElement(accumulator,i,(ssize_t)
1940 MagickRound(radius+hough_height),&count);
1941 }
1942 }
1943 p+=(ptrdiff_t) GetPixelChannels(image);
1944 }
1945 if (image->progress_monitor != (MagickProgressMonitor) NULL)
1946 {
1947 MagickBooleanType
1948 proceed;
1949
1950#if defined(MAGICKCORE_OPENMP_SUPPORT)
1951 #pragma omp atomic
1952#endif
1953 progress++;
1954 proceed=SetImageProgress(image,CannyEdgeImageTag,progress,image->rows);
1955 if (proceed == MagickFalse)
1956 status=MagickFalse;
1957 }
1958 }
1959 image_view=DestroyCacheView(image_view);
1960 if (status == MagickFalse)
1961 {
1962 accumulator=DestroyMatrixInfo(accumulator);
1963 return((Image *) NULL);
1964 }
1965 /*
1966 Generate line segments from accumulator.
1967 */
1968 file=AcquireUniqueFileResource(path);
1969 if (file == -1)
1970 {
1971 accumulator=DestroyMatrixInfo(accumulator);
1972 return((Image *) NULL);
1973 }
1974 (void) FormatLocaleString(message,MagickPathExtent,
1975 "# Hough line transform: %.17gx%.17g%+.20g\n",(double) width,
1976 (double) height,(double) threshold);
1977 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1978 status=MagickFalse;
1979 (void) FormatLocaleString(message,MagickPathExtent,
1980 "viewbox 0 0 %.17g %.17g\n",(double) image->columns,(double) image->rows);
1981 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1982 status=MagickFalse;
1983 (void) FormatLocaleString(message,MagickPathExtent,
1984 "# x1,y1 x2,y2 # count angle distance\n");
1985 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
1986 status=MagickFalse;
1987 line_count=image->columns > image->rows ? image->columns/4 : image->rows/4;
1988 if (threshold != 0)
1989 line_count=threshold;
1990 for (y=0; y < (ssize_t) accumulator_height; y++)
1991 {
1992 ssize_t
1993 x;
1994
1995 for (x=0; x < (ssize_t) accumulator_width; x++)
1996 {
1997 double
1998 count;
1999
2000 (void) GetMatrixElement(accumulator,x,y,&count);
2001 if (count >= (double) line_count)
2002 {
2003 double
2004 maxima;
2005
2006 SegmentInfo
2007 line;
2008
2009 ssize_t
2010 v;
2011
2012 /*
2013 Is point a local maxima?
2014 */
2015 maxima=count;
2016 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2017 {
2018 ssize_t
2019 u;
2020
2021 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2022 {
2023 if ((u != 0) || (v !=0))
2024 {
2025 (void) GetMatrixElement(accumulator,x+u,y+v,&count);
2026 if (count > maxima)
2027 {
2028 maxima=count;
2029 break;
2030 }
2031 }
2032 }
2033 if (u < (ssize_t) (width/2))
2034 break;
2035 }
2036 (void) GetMatrixElement(accumulator,x,y,&count);
2037 if (maxima > count)
2038 continue;
2039 if ((x >= 45) && (x <= 135))
2040 {
2041 /*
2042 y = (r-x cos(t))/sin(t)
2043 */
2044 line.x1=0.0;
2045 line.y1=((double) (y-(accumulator_height/2.0))-((line.x1-
2046 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
2047 sin(DegreesToRadians((double) x))+(image->rows/2.0);
2048 line.x2=(double) image->columns;
2049 line.y2=((double) (y-(accumulator_height/2.0))-((line.x2-
2050 (image->columns/2.0))*cos(DegreesToRadians((double) x))))/
2051 sin(DegreesToRadians((double) x))+(image->rows/2.0);
2052 }
2053 else
2054 {
2055 /*
2056 x = (r-y cos(t))/sin(t)
2057 */
2058 line.y1=0.0;
2059 line.x1=((double) (y-(accumulator_height/2.0))-((line.y1-
2060 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
2061 cos(DegreesToRadians((double) x))+(image->columns/2.0);
2062 line.y2=(double) image->rows;
2063 line.x2=((double) (y-(accumulator_height/2.0))-((line.y2-
2064 (image->rows/2.0))*sin(DegreesToRadians((double) x))))/
2065 cos(DegreesToRadians((double) x))+(image->columns/2.0);
2066 }
2067 (void) FormatLocaleString(message,MagickPathExtent,
2068 "line %g,%g %g,%g # %g %g %g\n",line.x1,line.y1,line.x2,line.y2,
2069 maxima,(double) x,(double) y);
2070 if (write(file,message,strlen(message)) != (ssize_t) strlen(message))
2071 status=MagickFalse;
2072 }
2073 }
2074 }
2075 (void) close_utf8(file);
2076 /*
2077 Render lines to image canvas.
2078 */
2079 image_info=AcquireImageInfo();
2080 image_info->background_color=image->background_color;
2081 (void) FormatLocaleString(image_info->filename,MagickPathExtent,"%s",path);
2082 artifact=GetImageArtifact(image,"background");
2083 if (artifact != (const char *) NULL)
2084 (void) SetImageOption(image_info,"background",artifact);
2085 artifact=GetImageArtifact(image,"fill");
2086 if (artifact != (const char *) NULL)
2087 (void) SetImageOption(image_info,"fill",artifact);
2088 artifact=GetImageArtifact(image,"stroke");
2089 if (artifact != (const char *) NULL)
2090 (void) SetImageOption(image_info,"stroke",artifact);
2091 artifact=GetImageArtifact(image,"strokewidth");
2092 if (artifact != (const char *) NULL)
2093 (void) SetImageOption(image_info,"strokewidth",artifact);
2094 lines_image=RenderHoughLines(image_info,image->columns,image->rows,exception);
2095 artifact=GetImageArtifact(image,"hough-lines:accumulator");
2096 if ((lines_image != (Image *) NULL) &&
2097 (IsStringTrue(artifact) != MagickFalse))
2098 {
2099 Image
2100 *accumulator_image;
2101
2102 accumulator_image=MatrixToImage(accumulator,exception);
2103 if (accumulator_image != (Image *) NULL)
2104 AppendImageToList(&lines_image,accumulator_image);
2105 }
2106 /*
2107 Free resources.
2108 */
2109 accumulator=DestroyMatrixInfo(accumulator);
2110 image_info=DestroyImageInfo(image_info);
2111 (void) RelinquishUniqueFileResource(path);
2112 return(GetFirstImageInList(lines_image));
2113}
2114
2115/*
2116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2117% %
2118% %
2119% %
2120% M e a n S h i f t I m a g e %
2121% %
2122% %
2123% %
2124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2125%
2126% MeanShiftImage() delineate arbitrarily shaped clusters in the image. For
2127% each pixel, it visits all the pixels in the neighborhood specified by
2128% the window centered at the pixel and excludes those that are outside the
2129% radius=(window-1)/2 surrounding the pixel. From those pixels, it finds those
2130% that are within the specified color distance from the current mean, and
2131% computes a new x,y centroid from those coordinates and a new mean. This new
2132% x,y centroid is used as the center for a new window. This process iterates
2133% until it converges and the final mean is replaces the (original window
2134% center) pixel value. It repeats this process for the next pixel, etc.,
2135% until it processes all pixels in the image. Results are typically better with
2136% colorspaces other than sRGB. We recommend YIQ, YUV or YCbCr.
2137%
2138% The format of the MeanShiftImage method is:
2139%
2140% Image *MeanShiftImage(const Image *image,const size_t width,
2141% const size_t height,const double color_distance,
2142% ExceptionInfo *exception)
2143%
2144% A description of each parameter follows:
2145%
2146% o image: the image.
2147%
2148% o width, height: find pixels in this neighborhood.
2149%
2150% o color_distance: the color distance.
2151%
2152% o exception: return any errors or warnings in this structure.
2153%
2154*/
2155MagickExport Image *MeanShiftImage(const Image *image,const size_t width,
2156 const size_t height,const double color_distance,ExceptionInfo *exception)
2157{
2158#define MaxMeanShiftIterations 100
2159#define MeanShiftImageTag "MeanShift/Image"
2160
2161 CacheView
2162 *image_view,
2163 *mean_view,
2164 *pixel_view;
2165
2166 Image
2167 *mean_image;
2168
2169 MagickBooleanType
2170 status;
2171
2172 MagickOffsetType
2173 progress;
2174
2175 ssize_t
2176 y;
2177
2178 assert(image != (const Image *) NULL);
2179 assert(image->signature == MagickCoreSignature);
2180 assert(exception != (ExceptionInfo *) NULL);
2181 assert(exception->signature == MagickCoreSignature);
2182 if (IsEventLogging() != MagickFalse)
2183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
2184 mean_image=CloneImage(image,0,0,MagickTrue,exception);
2185 if (mean_image == (Image *) NULL)
2186 return((Image *) NULL);
2187 if (SetImageStorageClass(mean_image,DirectClass,exception) == MagickFalse)
2188 {
2189 mean_image=DestroyImage(mean_image);
2190 return((Image *) NULL);
2191 }
2192 status=MagickTrue;
2193 progress=0;
2194 image_view=AcquireVirtualCacheView(image,exception);
2195 pixel_view=AcquireVirtualCacheView(image,exception);
2196 mean_view=AcquireAuthenticCacheView(mean_image,exception);
2197#if defined(MAGICKCORE_OPENMP_SUPPORT)
2198 #pragma omp parallel for schedule(static) shared(status,progress) \
2199 magick_number_threads(mean_image,mean_image,mean_image->rows,1)
2200#endif
2201 for (y=0; y < (ssize_t) mean_image->rows; y++)
2202 {
2203 const Quantum
2204 *magick_restrict p;
2205
2206 Quantum
2207 *magick_restrict q;
2208
2209 ssize_t
2210 x;
2211
2212 if (status == MagickFalse)
2213 continue;
2214 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
2215 q=GetCacheViewAuthenticPixels(mean_view,0,y,mean_image->columns,1,
2216 exception);
2217 if ((p == (const Quantum *) NULL) || (q == (Quantum *) NULL))
2218 {
2219 status=MagickFalse;
2220 continue;
2221 }
2222 for (x=0; x < (ssize_t) mean_image->columns; x++)
2223 {
2224 PixelInfo
2225 mean_pixel,
2226 previous_pixel;
2227
2228 PointInfo
2229 mean_location,
2230 previous_location;
2231
2232 ssize_t
2233 i;
2234
2235 GetPixelInfo(image,&mean_pixel);
2236 GetPixelInfoPixel(image,p,&mean_pixel);
2237 mean_location.x=(double) x;
2238 mean_location.y=(double) y;
2239 for (i=0; i < MaxMeanShiftIterations; i++)
2240 {
2241 double
2242 distance,
2243 gamma = 1.0;
2244
2245 PixelInfo
2246 sum_pixel;
2247
2248 PointInfo
2249 sum_location;
2250
2251 ssize_t
2252 count,
2253 v;
2254
2255 sum_location.x=0.0;
2256 sum_location.y=0.0;
2257 GetPixelInfo(image,&sum_pixel);
2258 previous_location=mean_location;
2259 previous_pixel=mean_pixel;
2260 count=0;
2261 for (v=(-((ssize_t) height/2)); v <= (((ssize_t) height/2)); v++)
2262 {
2263 ssize_t
2264 u;
2265
2266 for (u=(-((ssize_t) width/2)); u <= (((ssize_t) width/2)); u++)
2267 {
2268 if ((v*v+u*u) <= (ssize_t) ((width/2)*(height/2)))
2269 {
2270 PixelInfo
2271 pixel;
2272
2273 status=GetOneCacheViewVirtualPixelInfo(pixel_view,(ssize_t)
2274 MagickRound(mean_location.x+u),(ssize_t) MagickRound(
2275 mean_location.y+v),&pixel,exception);
2276 distance=(mean_pixel.red-pixel.red)*(mean_pixel.red-pixel.red)+
2277 (mean_pixel.green-pixel.green)*(mean_pixel.green-pixel.green)+
2278 (mean_pixel.blue-pixel.blue)*(mean_pixel.blue-pixel.blue);
2279 if (distance <= (color_distance*color_distance))
2280 {
2281 sum_location.x+=mean_location.x+u;
2282 sum_location.y+=mean_location.y+v;
2283 sum_pixel.red+=pixel.red;
2284 sum_pixel.green+=pixel.green;
2285 sum_pixel.blue+=pixel.blue;
2286 sum_pixel.alpha+=pixel.alpha;
2287 count++;
2288 }
2289 }
2290 }
2291 }
2292 if (count != 0)
2293 gamma=MagickSafeReciprocal((double) count);
2294 mean_location.x=gamma*sum_location.x;
2295 mean_location.y=gamma*sum_location.y;
2296 mean_pixel.red=gamma*sum_pixel.red;
2297 mean_pixel.green=gamma*sum_pixel.green;
2298 mean_pixel.blue=gamma*sum_pixel.blue;
2299 mean_pixel.alpha=gamma*sum_pixel.alpha;
2300 distance=(mean_location.x-previous_location.x)*
2301 (mean_location.x-previous_location.x)+
2302 (mean_location.y-previous_location.y)*
2303 (mean_location.y-previous_location.y)+
2304 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)*
2305 255.0*QuantumScale*(mean_pixel.red-previous_pixel.red)+
2306 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)*
2307 255.0*QuantumScale*(mean_pixel.green-previous_pixel.green)+
2308 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue)*
2309 255.0*QuantumScale*(mean_pixel.blue-previous_pixel.blue);
2310 if (distance <= 3.0)
2311 break;
2312 }
2313 SetPixelRed(mean_image,ClampToQuantum(mean_pixel.red),q);
2314 SetPixelGreen(mean_image,ClampToQuantum(mean_pixel.green),q);
2315 SetPixelBlue(mean_image,ClampToQuantum(mean_pixel.blue),q);
2316 SetPixelAlpha(mean_image,ClampToQuantum(mean_pixel.alpha),q);
2317 p+=(ptrdiff_t) GetPixelChannels(image);
2318 q+=(ptrdiff_t) GetPixelChannels(mean_image);
2319 }
2320 if (SyncCacheViewAuthenticPixels(mean_view,exception) == MagickFalse)
2321 status=MagickFalse;
2322 if (image->progress_monitor != (MagickProgressMonitor) NULL)
2323 {
2324 MagickBooleanType
2325 proceed;
2326
2327#if defined(MAGICKCORE_OPENMP_SUPPORT)
2328 #pragma omp atomic
2329#endif
2330 progress++;
2331 proceed=SetImageProgress(image,MeanShiftImageTag,progress,image->rows);
2332 if (proceed == MagickFalse)
2333 status=MagickFalse;
2334 }
2335 }
2336 mean_view=DestroyCacheView(mean_view);
2337 pixel_view=DestroyCacheView(pixel_view);
2338 image_view=DestroyCacheView(image_view);
2339 return(mean_image);
2340}