blob: f0b12578a5939668468fdb9299586abcdb5327f3 [file] [log] [blame]
cristy3e2860c2010-01-24 01:36:30 +00001/*
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% John Cristy %
17% July 1992 %
18% %
19% %
cristy7e41fe82010-12-04 23:12:08 +000020% Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
cristy3e2860c2010-01-24 01:36:30 +000021% 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% http://www.imagemagick.org/script/license.php %
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*/
cristy4c08aed2011-07-01 19:47:50 +000043#include "MagickCore/studio.h"
44#include "MagickCore/property.h"
45#include "MagickCore/animate.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/client.h"
52#include "MagickCore/color.h"
53#include "MagickCore/color-private.h"
54#include "MagickCore/colorspace.h"
55#include "MagickCore/colorspace-private.h"
56#include "MagickCore/composite.h"
57#include "MagickCore/composite-private.h"
58#include "MagickCore/compress.h"
59#include "MagickCore/constitute.h"
60#include "MagickCore/display.h"
61#include "MagickCore/draw.h"
62#include "MagickCore/enhance.h"
63#include "MagickCore/exception.h"
64#include "MagickCore/exception-private.h"
65#include "MagickCore/feature.h"
66#include "MagickCore/gem.h"
67#include "MagickCore/geometry.h"
68#include "MagickCore/list.h"
69#include "MagickCore/image-private.h"
70#include "MagickCore/magic.h"
71#include "MagickCore/magick.h"
72#include "MagickCore/memory_.h"
73#include "MagickCore/module.h"
74#include "MagickCore/monitor.h"
75#include "MagickCore/monitor-private.h"
76#include "MagickCore/option.h"
77#include "MagickCore/paint.h"
78#include "MagickCore/pixel-accessor.h"
79#include "MagickCore/profile.h"
80#include "MagickCore/quantize.h"
81#include "MagickCore/quantum-private.h"
82#include "MagickCore/random_.h"
83#include "MagickCore/segment.h"
84#include "MagickCore/semaphore.h"
85#include "MagickCore/signature-private.h"
86#include "MagickCore/string_.h"
87#include "MagickCore/thread-private.h"
88#include "MagickCore/timer.h"
89#include "MagickCore/utility.h"
90#include "MagickCore/version.h"
cristy3e2860c2010-01-24 01:36:30 +000091
92/*
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94% %
95% %
96% %
cristy490408a2011-07-07 14:42:05 +000097% G e t I m a g e F e a t u r e s %
cristy3e2860c2010-01-24 01:36:30 +000098% %
99% %
100% %
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102%
cristy490408a2011-07-07 14:42:05 +0000103% GetImageFeatures() returns features for each channel in the image in
cristy7396d882010-01-27 02:37:56 +0000104% each of four directions (horizontal, vertical, left and right diagonals)
105% for the specified distance. The features include the angular second
106% moment, contrast, correlation, sum of squares: variance, inverse difference
107% moment, sum average, sum varience, sum entropy, entropy, difference variance,% difference entropy, information measures of correlation 1, information
108% measures of correlation 2, and maximum correlation coefficient. You can
109% access the red channel contrast, for example, like this:
cristy3e2860c2010-01-24 01:36:30 +0000110%
cristy490408a2011-07-07 14:42:05 +0000111% channel_features=GetImageFeatures(image,1,exception);
cristy549a37e2010-01-26 15:24:15 +0000112% contrast=channel_features[RedChannel].contrast[0];
cristy3e2860c2010-01-24 01:36:30 +0000113%
114% Use MagickRelinquishMemory() to free the features buffer.
115%
cristy490408a2011-07-07 14:42:05 +0000116% The format of the GetImageFeatures method is:
cristy3e2860c2010-01-24 01:36:30 +0000117%
cristy490408a2011-07-07 14:42:05 +0000118% ChannelFeatures *GetImageFeatures(const Image *image,
cristybb503372010-05-27 20:51:26 +0000119% const size_t distance,ExceptionInfo *exception)
cristy3e2860c2010-01-24 01:36:30 +0000120%
121% A description of each parameter follows:
122%
123% o image: the image.
124%
cristy7e9726d2010-01-26 02:08:40 +0000125% o distance: the distance.
126%
cristy3e2860c2010-01-24 01:36:30 +0000127% o exception: return any errors or warnings in this structure.
128%
129*/
cristye1897792010-01-29 02:05:50 +0000130
cristybb503372010-05-27 20:51:26 +0000131static inline ssize_t MagickAbsoluteValue(const ssize_t x)
cristye1897792010-01-29 02:05:50 +0000132{
133 if (x < 0)
134 return(-x);
135 return(x);
136}
137
cristy490408a2011-07-07 14:42:05 +0000138MagickExport ChannelFeatures *GetImageFeatures(const Image *image,
cristybb503372010-05-27 20:51:26 +0000139 const size_t distance,ExceptionInfo *exception)
cristy3e2860c2010-01-24 01:36:30 +0000140{
cristy7396d882010-01-27 02:37:56 +0000141 typedef struct _ChannelStatistics
cristyf2bf2c72010-01-25 19:54:15 +0000142 {
cristy101ab702011-10-13 13:06:32 +0000143 PixelInfo
cristy7396d882010-01-27 02:37:56 +0000144 direction[4]; /* horizontal, vertical, left and right diagonals */
145 } ChannelStatistics;
cristyf2bf2c72010-01-25 19:54:15 +0000146
cristy2070fa52010-01-24 03:17:57 +0000147 CacheView
148 *image_view;
149
cristy3e2860c2010-01-24 01:36:30 +0000150 ChannelFeatures
151 *channel_features;
152
cristy7396d882010-01-27 02:37:56 +0000153 ChannelStatistics
154 **cooccurrence,
155 correlation,
cristyffa10d02010-01-30 17:53:27 +0000156 *density_x,
157 *density_xy,
158 *density_y,
159 entropy_x,
160 entropy_xy,
161 entropy_xy1,
162 entropy_xy2,
163 entropy_y,
cristy7396d882010-01-27 02:37:56 +0000164 mean,
cristyffa10d02010-01-30 17:53:27 +0000165 **Q,
cristy7396d882010-01-27 02:37:56 +0000166 *sum,
cristycf5e6492010-01-28 02:45:28 +0000167 sum_squares,
168 variance;
cristy7396d882010-01-27 02:37:56 +0000169
cristy101ab702011-10-13 13:06:32 +0000170 PixelPacket
cristy7396d882010-01-27 02:37:56 +0000171 gray,
172 *grays;
cristy2070fa52010-01-24 03:17:57 +0000173
cristy2070fa52010-01-24 03:17:57 +0000174 MagickBooleanType
175 status;
176
cristybb503372010-05-27 20:51:26 +0000177 register ssize_t
cristy2070fa52010-01-24 03:17:57 +0000178 i;
179
cristy3e2860c2010-01-24 01:36:30 +0000180 size_t
181 length;
182
cristy9d314ff2011-03-09 01:30:28 +0000183 ssize_t
184 y,
185 z;
186
cristyecd0ab52010-05-30 14:59:20 +0000187 unsigned int
cristy7396d882010-01-27 02:37:56 +0000188 number_grays;
cristyf2bf2c72010-01-25 19:54:15 +0000189
cristy3e2860c2010-01-24 01:36:30 +0000190 assert(image != (Image *) NULL);
191 assert(image->signature == MagickSignature);
192 if (image->debug != MagickFalse)
193 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy7e9726d2010-01-26 02:08:40 +0000194 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
195 return((ChannelFeatures *) NULL);
cristy9a9230e2011-04-26 14:56:14 +0000196 length=CompositeChannels+1UL;
cristy3e2860c2010-01-24 01:36:30 +0000197 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
198 sizeof(*channel_features));
199 if (channel_features == (ChannelFeatures *) NULL)
200 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
201 (void) ResetMagickMemory(channel_features,0,length*
202 sizeof(*channel_features));
cristy2070fa52010-01-24 03:17:57 +0000203 /*
cristy7396d882010-01-27 02:37:56 +0000204 Form grays.
cristy2070fa52010-01-24 03:17:57 +0000205 */
cristy101ab702011-10-13 13:06:32 +0000206 grays=(PixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
207 if (grays == (PixelPacket *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000208 {
cristy2070fa52010-01-24 03:17:57 +0000209 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
210 channel_features);
cristye1897792010-01-29 02:05:50 +0000211 (void) ThrowMagickException(exception,GetMagickModule(),
212 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy2070fa52010-01-24 03:17:57 +0000213 return(channel_features);
214 }
cristybb503372010-05-27 20:51:26 +0000215 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy2070fa52010-01-24 03:17:57 +0000216 {
cristyecd0ab52010-05-30 14:59:20 +0000217 grays[i].red=(~0U);
218 grays[i].green=(~0U);
219 grays[i].blue=(~0U);
cristy4c08aed2011-07-01 19:47:50 +0000220 grays[i].alpha=(~0U);
221 grays[i].black=(~0U);
cristy2070fa52010-01-24 03:17:57 +0000222 }
223 status=MagickTrue;
224 image_view=AcquireCacheView(image);
225#if defined(MAGICKCORE_OPENMP_SUPPORT)
226 #pragma omp parallel for schedule(dynamic,4) shared(status)
227#endif
cristybb503372010-05-27 20:51:26 +0000228 for (y=0; y < (ssize_t) image->rows; y++)
cristy3e2860c2010-01-24 01:36:30 +0000229 {
cristy4c08aed2011-07-01 19:47:50 +0000230 register const Quantum
cristy3e2860c2010-01-24 01:36:30 +0000231 *restrict p;
232
cristybb503372010-05-27 20:51:26 +0000233 register ssize_t
cristy3e2860c2010-01-24 01:36:30 +0000234 x;
235
cristy2070fa52010-01-24 03:17:57 +0000236 if (status == MagickFalse)
237 continue;
238 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000239 if (p == (const Quantum *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000240 {
241 status=MagickFalse;
242 continue;
243 }
cristybb503372010-05-27 20:51:26 +0000244 for (x=0; x < (ssize_t) image->columns; x++)
cristy3e2860c2010-01-24 01:36:30 +0000245 {
cristy4c08aed2011-07-01 19:47:50 +0000246 grays[ScaleQuantumToMap(GetPixelRed(image,p))].red=
247 ScaleQuantumToMap(GetPixelRed(image,p));
248 grays[ScaleQuantumToMap(GetPixelGreen(image,p))].green=
249 ScaleQuantumToMap(GetPixelGreen(image,p));
250 grays[ScaleQuantumToMap(GetPixelBlue(image,p))].blue=
251 ScaleQuantumToMap(GetPixelBlue(image,p));
cristy2070fa52010-01-24 03:17:57 +0000252 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000253 grays[ScaleQuantumToMap(GetPixelBlack(image,p))].black=
254 ScaleQuantumToMap(GetPixelBlack(image,p));
cristy53a727d2011-06-16 14:53:56 +0000255 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000256 grays[ScaleQuantumToMap(GetPixelAlpha(image,p))].alpha=
257 ScaleQuantumToMap(GetPixelAlpha(image,p));
cristyed231572011-07-14 02:18:59 +0000258 p+=GetPixelChannels(image);
cristy3e2860c2010-01-24 01:36:30 +0000259 }
260 }
cristy30c510a2010-01-24 03:43:00 +0000261 image_view=DestroyCacheView(image_view);
262 if (status == MagickFalse)
263 {
cristy101ab702011-10-13 13:06:32 +0000264 grays=(PixelPacket *) RelinquishMagickMemory(grays);
cristy30c510a2010-01-24 03:43:00 +0000265 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
266 channel_features);
267 return(channel_features);
268 }
cristy7396d882010-01-27 02:37:56 +0000269 (void) ResetMagickMemory(&gray,0,sizeof(gray));
cristybb503372010-05-27 20:51:26 +0000270 for (i=0; i <= (ssize_t) MaxMap; i++)
cristy2070fa52010-01-24 03:17:57 +0000271 {
cristyecd0ab52010-05-30 14:59:20 +0000272 if (grays[i].red != ~0U)
cristy4c08aed2011-07-01 19:47:50 +0000273 grays[gray.red++].red=grays[i].red;
cristyecd0ab52010-05-30 14:59:20 +0000274 if (grays[i].green != ~0U)
cristy4c08aed2011-07-01 19:47:50 +0000275 grays[gray.green++].green=grays[i].green;
cristyecd0ab52010-05-30 14:59:20 +0000276 if (grays[i].blue != ~0U)
cristy4c08aed2011-07-01 19:47:50 +0000277 grays[gray.blue++].blue=grays[i].blue;
cristy2070fa52010-01-24 03:17:57 +0000278 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000279 if (grays[i].black != ~0U)
280 grays[gray.black++].black=grays[i].black;
cristy53a727d2011-06-16 14:53:56 +0000281 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000282 if (grays[i].alpha != ~0U)
283 grays[gray.alpha++].alpha=grays[i].alpha;
cristy2070fa52010-01-24 03:17:57 +0000284 }
cristyf2bf2c72010-01-25 19:54:15 +0000285 /*
286 Allocate spatial dependence matrix.
287 */
cristy7396d882010-01-27 02:37:56 +0000288 number_grays=gray.red;
289 if (gray.green > number_grays)
290 number_grays=gray.green;
291 if (gray.blue > number_grays)
292 number_grays=gray.blue;
cristyf2bf2c72010-01-25 19:54:15 +0000293 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000294 if (gray.black > number_grays)
295 number_grays=gray.black;
cristy53a727d2011-06-16 14:53:56 +0000296 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000297 if (gray.alpha > number_grays)
298 number_grays=gray.alpha;
cristy7396d882010-01-27 02:37:56 +0000299 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
300 sizeof(*cooccurrence));
cristy77173e52010-02-02 02:51:35 +0000301 density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
cristyffa10d02010-01-30 17:53:27 +0000302 sizeof(*density_x));
303 density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
304 sizeof(*density_xy));
cristy77173e52010-02-02 02:51:35 +0000305 density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1),
cristyffa10d02010-01-30 17:53:27 +0000306 sizeof(*density_y));
307 Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q));
308 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
309 if ((cooccurrence == (ChannelStatistics **) NULL) ||
310 (density_x == (ChannelStatistics *) NULL) ||
311 (density_xy == (ChannelStatistics *) NULL) ||
312 (density_y == (ChannelStatistics *) NULL) ||
313 (Q == (ChannelStatistics **) NULL) ||
314 (sum == (ChannelStatistics *) NULL))
cristyf2bf2c72010-01-25 19:54:15 +0000315 {
cristyffa10d02010-01-30 17:53:27 +0000316 if (Q != (ChannelStatistics **) NULL)
317 {
cristybb503372010-05-27 20:51:26 +0000318 for (i=0; i < (ssize_t) number_grays; i++)
cristyffa10d02010-01-30 17:53:27 +0000319 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
320 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
321 }
322 if (sum != (ChannelStatistics *) NULL)
323 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
324 if (density_y != (ChannelStatistics *) NULL)
325 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
326 if (density_xy != (ChannelStatistics *) NULL)
327 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
328 if (density_x != (ChannelStatistics *) NULL)
329 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
330 if (cooccurrence != (ChannelStatistics **) NULL)
331 {
cristybb503372010-05-27 20:51:26 +0000332 for (i=0; i < (ssize_t) number_grays; i++)
cristyffa10d02010-01-30 17:53:27 +0000333 cooccurrence[i]=(ChannelStatistics *)
334 RelinquishMagickMemory(cooccurrence[i]);
335 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(
336 cooccurrence);
337 }
cristy101ab702011-10-13 13:06:32 +0000338 grays=(PixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000339 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
340 channel_features);
cristye1897792010-01-29 02:05:50 +0000341 (void) ThrowMagickException(exception,GetMagickModule(),
342 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000343 return(channel_features);
344 }
cristyffa10d02010-01-30 17:53:27 +0000345 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
cristy77173e52010-02-02 02:51:35 +0000346 (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x));
347 (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy));
348 (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y));
cristyffa10d02010-01-30 17:53:27 +0000349 (void) ResetMagickMemory(&mean,0,sizeof(mean));
350 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
351 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
352 (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy));
353 (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x));
354 (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy));
355 (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1));
356 (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2));
357 (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y));
358 (void) ResetMagickMemory(&variance,0,sizeof(variance));
cristybb503372010-05-27 20:51:26 +0000359 for (i=0; i < (ssize_t) number_grays; i++)
cristyf2bf2c72010-01-25 19:54:15 +0000360 {
cristy7396d882010-01-27 02:37:56 +0000361 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
362 sizeof(**cooccurrence));
cristyffa10d02010-01-30 17:53:27 +0000363 Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q));
364 if ((cooccurrence[i] == (ChannelStatistics *) NULL) ||
365 (Q[i] == (ChannelStatistics *) NULL))
cristyf2bf2c72010-01-25 19:54:15 +0000366 break;
cristy7396d882010-01-27 02:37:56 +0000367 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
cristy3749be42010-02-02 02:46:51 +0000368 sizeof(**cooccurrence));
369 (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q));
cristyf2bf2c72010-01-25 19:54:15 +0000370 }
cristybb503372010-05-27 20:51:26 +0000371 if (i < (ssize_t) number_grays)
cristyf2bf2c72010-01-25 19:54:15 +0000372 {
cristyf2bf2c72010-01-25 19:54:15 +0000373 for (i--; i >= 0; i--)
cristyffa10d02010-01-30 17:53:27 +0000374 {
375 if (Q[i] != (ChannelStatistics *) NULL)
376 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
377 if (cooccurrence[i] != (ChannelStatistics *) NULL)
378 cooccurrence[i]=(ChannelStatistics *)
379 RelinquishMagickMemory(cooccurrence[i]);
380 }
381 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
cristy7396d882010-01-27 02:37:56 +0000382 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
cristyffa10d02010-01-30 17:53:27 +0000383 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
384 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
385 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
386 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
cristy101ab702011-10-13 13:06:32 +0000387 grays=(PixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000388 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
389 channel_features);
cristye1897792010-01-29 02:05:50 +0000390 (void) ThrowMagickException(exception,GetMagickModule(),
391 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000392 return(channel_features);
393 }
394 /*
395 Initialize spatial dependence matrix.
396 */
397 status=MagickTrue;
398 image_view=AcquireCacheView(image);
cristybb503372010-05-27 20:51:26 +0000399 for (y=0; y < (ssize_t) image->rows; y++)
cristyf2bf2c72010-01-25 19:54:15 +0000400 {
cristy4c08aed2011-07-01 19:47:50 +0000401 register const Quantum
cristyf2bf2c72010-01-25 19:54:15 +0000402 *restrict p;
403
cristybb503372010-05-27 20:51:26 +0000404 register ssize_t
cristyf2bf2c72010-01-25 19:54:15 +0000405 x;
406
cristy7e9726d2010-01-26 02:08:40 +0000407 ssize_t
cristy53a727d2011-06-16 14:53:56 +0000408 i,
cristy9d314ff2011-03-09 01:30:28 +0000409 offset,
410 u,
411 v;
cristy7e9726d2010-01-26 02:08:40 +0000412
cristyf2bf2c72010-01-25 19:54:15 +0000413 if (status == MagickFalse)
414 continue;
cristybb503372010-05-27 20:51:26 +0000415 p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+
cristy53a727d2011-06-16 14:53:56 +0000416 2*distance,distance+1,exception);
cristy4c08aed2011-07-01 19:47:50 +0000417 if (p == (const Quantum *) NULL)
cristyf2bf2c72010-01-25 19:54:15 +0000418 {
419 status=MagickFalse;
420 continue;
421 }
cristyed231572011-07-14 02:18:59 +0000422 p+=distance*GetPixelChannels(image);;
cristybb503372010-05-27 20:51:26 +0000423 for (x=0; x < (ssize_t) image->columns; x++)
cristyf2bf2c72010-01-25 19:54:15 +0000424 {
425 for (i=0; i < 4; i++)
426 {
cristy7e9726d2010-01-26 02:08:40 +0000427 switch (i)
428 {
429 case 0:
cristy549a37e2010-01-26 15:24:15 +0000430 default:
cristy7e9726d2010-01-26 02:08:40 +0000431 {
432 /*
cristy7396d882010-01-27 02:37:56 +0000433 Horizontal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000434 */
435 offset=(ssize_t) distance;
436 break;
437 }
438 case 1:
439 {
440 /*
cristy7396d882010-01-27 02:37:56 +0000441 Vertical adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000442 */
cristy7396d882010-01-27 02:37:56 +0000443 offset=(ssize_t) (image->columns+2*distance);
cristy7e9726d2010-01-26 02:08:40 +0000444 break;
445 }
446 case 2:
447 {
448 /*
cristy7396d882010-01-27 02:37:56 +0000449 Right diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000450 */
cristyd99b0962010-05-29 23:14:26 +0000451 offset=(ssize_t) ((image->columns+2*distance)-distance);
cristy7e9726d2010-01-26 02:08:40 +0000452 break;
453 }
454 case 3:
455 {
456 /*
cristy7396d882010-01-27 02:37:56 +0000457 Left diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000458 */
cristyd99b0962010-05-29 23:14:26 +0000459 offset=(ssize_t) ((image->columns+2*distance)+distance);
cristy7e9726d2010-01-26 02:08:40 +0000460 break;
461 }
462 }
463 u=0;
464 v=0;
cristy4c08aed2011-07-01 19:47:50 +0000465 while (grays[u].red != ScaleQuantumToMap(GetPixelRed(image,p)))
cristy7e9726d2010-01-26 02:08:40 +0000466 u++;
cristyed231572011-07-14 02:18:59 +0000467 while (grays[v].red != ScaleQuantumToMap(GetPixelRed(image,p+offset*GetPixelChannels(image))))
cristy7e9726d2010-01-26 02:08:40 +0000468 v++;
cristy7396d882010-01-27 02:37:56 +0000469 cooccurrence[u][v].direction[i].red++;
470 cooccurrence[v][u].direction[i].red++;
cristy7e9726d2010-01-26 02:08:40 +0000471 u=0;
472 v=0;
cristy4c08aed2011-07-01 19:47:50 +0000473 while (grays[u].green != ScaleQuantumToMap(GetPixelGreen(image,p)))
cristy7e9726d2010-01-26 02:08:40 +0000474 u++;
cristyed231572011-07-14 02:18:59 +0000475 while (grays[v].green != ScaleQuantumToMap(GetPixelGreen(image,p+offset*GetPixelChannels(image))))
cristy7e9726d2010-01-26 02:08:40 +0000476 v++;
cristy7396d882010-01-27 02:37:56 +0000477 cooccurrence[u][v].direction[i].green++;
478 cooccurrence[v][u].direction[i].green++;
cristy7e9726d2010-01-26 02:08:40 +0000479 u=0;
480 v=0;
cristy4c08aed2011-07-01 19:47:50 +0000481 while (grays[u].blue != ScaleQuantumToMap(GetPixelBlue(image,p)))
cristy7e9726d2010-01-26 02:08:40 +0000482 u++;
cristyed231572011-07-14 02:18:59 +0000483 while (grays[v].blue != ScaleQuantumToMap(GetPixelBlue(image,p+offset*GetPixelChannels(image))))
cristy7e9726d2010-01-26 02:08:40 +0000484 v++;
cristy7396d882010-01-27 02:37:56 +0000485 cooccurrence[u][v].direction[i].blue++;
486 cooccurrence[v][u].direction[i].blue++;
cristy7e9726d2010-01-26 02:08:40 +0000487 if (image->colorspace == CMYKColorspace)
488 {
489 u=0;
490 v=0;
cristy4c08aed2011-07-01 19:47:50 +0000491 while (grays[u].black != ScaleQuantumToMap(GetPixelBlack(image,p)))
cristy7e9726d2010-01-26 02:08:40 +0000492 u++;
cristyed231572011-07-14 02:18:59 +0000493 while (grays[v].black != ScaleQuantumToMap(GetPixelBlack(image,p+offset*GetPixelChannels(image))))
cristy7e9726d2010-01-26 02:08:40 +0000494 v++;
cristy4c08aed2011-07-01 19:47:50 +0000495 cooccurrence[u][v].direction[i].black++;
496 cooccurrence[v][u].direction[i].black++;
cristy7e9726d2010-01-26 02:08:40 +0000497 }
cristy53a727d2011-06-16 14:53:56 +0000498 if (image->matte != MagickFalse)
499 {
500 u=0;
501 v=0;
cristy4c08aed2011-07-01 19:47:50 +0000502 while (grays[u].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p)))
cristy53a727d2011-06-16 14:53:56 +0000503 u++;
cristyed231572011-07-14 02:18:59 +0000504 while (grays[v].alpha != ScaleQuantumToMap(GetPixelAlpha(image,p+offset*GetPixelChannels(image))))
cristy53a727d2011-06-16 14:53:56 +0000505 v++;
cristy4c08aed2011-07-01 19:47:50 +0000506 cooccurrence[u][v].direction[i].alpha++;
507 cooccurrence[v][u].direction[i].alpha++;
cristy53a727d2011-06-16 14:53:56 +0000508 }
cristyf2bf2c72010-01-25 19:54:15 +0000509 }
cristyed231572011-07-14 02:18:59 +0000510 p+=GetPixelChannels(image);
cristyf2bf2c72010-01-25 19:54:15 +0000511 }
512 }
cristy101ab702011-10-13 13:06:32 +0000513 grays=(PixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000514 image_view=DestroyCacheView(image_view);
515 if (status == MagickFalse)
516 {
cristybb503372010-05-27 20:51:26 +0000517 for (i=0; i < (ssize_t) number_grays; i++)
cristy7396d882010-01-27 02:37:56 +0000518 cooccurrence[i]=(ChannelStatistics *)
519 RelinquishMagickMemory(cooccurrence[i]);
520 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
cristyf2bf2c72010-01-25 19:54:15 +0000521 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
522 channel_features);
cristye1897792010-01-29 02:05:50 +0000523 (void) ThrowMagickException(exception,GetMagickModule(),
524 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000525 return(channel_features);
526 }
527 /*
cristy7e9726d2010-01-26 02:08:40 +0000528 Normalize spatial dependence matrix.
529 */
cristybd822072010-01-27 00:30:00 +0000530 for (i=0; i < 4; i++)
cristy7e9726d2010-01-26 02:08:40 +0000531 {
cristy549a37e2010-01-26 15:24:15 +0000532 double
533 normalize;
534
cristy53a727d2011-06-16 14:53:56 +0000535 register ssize_t
536 y;
537
cristybd822072010-01-27 00:30:00 +0000538 switch (i)
cristy7e9726d2010-01-26 02:08:40 +0000539 {
cristybd822072010-01-27 00:30:00 +0000540 case 0:
541 default:
cristy7e9726d2010-01-26 02:08:40 +0000542 {
cristybd822072010-01-27 00:30:00 +0000543 /*
cristy7396d882010-01-27 02:37:56 +0000544 Horizontal adjacency.
cristybd822072010-01-27 00:30:00 +0000545 */
546 normalize=2.0*image->rows*(image->columns-distance);
547 break;
548 }
549 case 1:
550 {
551 /*
cristy7396d882010-01-27 02:37:56 +0000552 Vertical adjacency.
cristybd822072010-01-27 00:30:00 +0000553 */
cristy7396d882010-01-27 02:37:56 +0000554 normalize=2.0*(image->rows-distance)*image->columns;
cristybd822072010-01-27 00:30:00 +0000555 break;
556 }
557 case 2:
558 {
559 /*
cristy7396d882010-01-27 02:37:56 +0000560 Right diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000561 */
cristy7396d882010-01-27 02:37:56 +0000562 normalize=2.0*(image->rows-distance)*(image->columns-distance);
cristybd822072010-01-27 00:30:00 +0000563 break;
564 }
565 case 3:
566 {
567 /*
cristy7396d882010-01-27 02:37:56 +0000568 Left diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000569 */
570 normalize=2.0*(image->rows-distance)*(image->columns-distance);
571 break;
572 }
573 }
cristy53a727d2011-06-16 14:53:56 +0000574 normalize=1.0/(fabs((double) normalize) <= MagickEpsilon ? 1.0 : normalize);
cristybb503372010-05-27 20:51:26 +0000575 for (y=0; y < (ssize_t) number_grays; y++)
cristybd822072010-01-27 00:30:00 +0000576 {
cristybb503372010-05-27 20:51:26 +0000577 register ssize_t
cristybd822072010-01-27 00:30:00 +0000578 x;
579
cristybb503372010-05-27 20:51:26 +0000580 for (x=0; x < (ssize_t) number_grays; x++)
cristybd822072010-01-27 00:30:00 +0000581 {
cristy53a727d2011-06-16 14:53:56 +0000582 cooccurrence[x][y].direction[i].red*=normalize;
583 cooccurrence[x][y].direction[i].green*=normalize;
584 cooccurrence[x][y].direction[i].blue*=normalize;
cristy549a37e2010-01-26 15:24:15 +0000585 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000586 cooccurrence[x][y].direction[i].black*=normalize;
cristy53a727d2011-06-16 14:53:56 +0000587 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000588 cooccurrence[x][y].direction[i].alpha*=normalize;
cristy549a37e2010-01-26 15:24:15 +0000589 }
590 }
591 }
592 /*
593 Compute texture features.
594 */
cristy3a82f252010-01-26 20:31:51 +0000595#if defined(MAGICKCORE_OPENMP_SUPPORT)
596 #pragma omp parallel for schedule(dynamic,4) shared(status)
597#endif
cristybd822072010-01-27 00:30:00 +0000598 for (i=0; i < 4; i++)
cristy549a37e2010-01-26 15:24:15 +0000599 {
cristybb503372010-05-27 20:51:26 +0000600 register ssize_t
cristybd822072010-01-27 00:30:00 +0000601 y;
cristy549a37e2010-01-26 15:24:15 +0000602
cristybb503372010-05-27 20:51:26 +0000603 for (y=0; y < (ssize_t) number_grays; y++)
cristy549a37e2010-01-26 15:24:15 +0000604 {
cristybb503372010-05-27 20:51:26 +0000605 register ssize_t
cristybd822072010-01-27 00:30:00 +0000606 x;
607
cristybb503372010-05-27 20:51:26 +0000608 for (x=0; x < (ssize_t) number_grays; x++)
cristy549a37e2010-01-26 15:24:15 +0000609 {
610 /*
cristy3a82f252010-01-26 20:31:51 +0000611 Angular second moment: measure of homogeneity of the image.
cristy549a37e2010-01-26 15:24:15 +0000612 */
613 channel_features[RedChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000614 cooccurrence[x][y].direction[i].red*
615 cooccurrence[x][y].direction[i].red;
cristy549a37e2010-01-26 15:24:15 +0000616 channel_features[GreenChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000617 cooccurrence[x][y].direction[i].green*
618 cooccurrence[x][y].direction[i].green;
cristy549a37e2010-01-26 15:24:15 +0000619 channel_features[BlueChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000620 cooccurrence[x][y].direction[i].blue*
621 cooccurrence[x][y].direction[i].blue;
cristy549a37e2010-01-26 15:24:15 +0000622 if (image->colorspace == CMYKColorspace)
cristy3a82f252010-01-26 20:31:51 +0000623 channel_features[BlackChannel].angular_second_moment[i]+=
cristy4c08aed2011-07-01 19:47:50 +0000624 cooccurrence[x][y].direction[i].black*
625 cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000626 if (image->matte != MagickFalse)
627 channel_features[OpacityChannel].angular_second_moment[i]+=
cristy4c08aed2011-07-01 19:47:50 +0000628 cooccurrence[x][y].direction[i].alpha*
629 cooccurrence[x][y].direction[i].alpha;
cristy7396d882010-01-27 02:37:56 +0000630 /*
631 Correlation: measure of linear-dependencies in the image.
632 */
633 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000634 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
cristycdf8e1b2010-01-28 01:52:33 +0000635 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristycdf8e1b2010-01-28 01:52:33 +0000636 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000637 sum[y].direction[i].black+=cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000638 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000639 sum[y].direction[i].alpha+=cooccurrence[x][y].direction[i].alpha;
cristycdf8e1b2010-01-28 01:52:33 +0000640 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000641 correlation.direction[i].green+=x*y*
642 cooccurrence[x][y].direction[i].green;
cristy7396d882010-01-27 02:37:56 +0000643 correlation.direction[i].blue+=x*y*
644 cooccurrence[x][y].direction[i].blue;
cristy7396d882010-01-27 02:37:56 +0000645 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000646 correlation.direction[i].black+=x*y*
647 cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000648 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000649 correlation.direction[i].alpha+=x*y*
650 cooccurrence[x][y].direction[i].alpha;
cristycf5e6492010-01-28 02:45:28 +0000651 /*
652 Inverse Difference Moment.
653 */
cristy9fc6dbf2010-01-28 02:49:50 +0000654 channel_features[RedChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000655 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000656 channel_features[GreenChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000657 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000658 channel_features[BlueChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000659 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
cristycf5e6492010-01-28 02:45:28 +0000660 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000661 channel_features[BlackChannel].inverse_difference_moment[i]+=
662 cooccurrence[x][y].direction[i].black/((y-x)*(y-x)+1);
cristy53a727d2011-06-16 14:53:56 +0000663 if (image->matte != MagickFalse)
664 channel_features[OpacityChannel].inverse_difference_moment[i]+=
cristy4c08aed2011-07-01 19:47:50 +0000665 cooccurrence[x][y].direction[i].alpha/((y-x)*(y-x)+1);
cristye1897792010-01-29 02:05:50 +0000666 /*
667 Sum average.
668 */
cristyffa10d02010-01-30 17:53:27 +0000669 density_xy[y+x+2].direction[i].red+=
cristye1897792010-01-29 02:05:50 +0000670 cooccurrence[x][y].direction[i].red;
cristyffa10d02010-01-30 17:53:27 +0000671 density_xy[y+x+2].direction[i].green+=
cristye1897792010-01-29 02:05:50 +0000672 cooccurrence[x][y].direction[i].green;
cristyffa10d02010-01-30 17:53:27 +0000673 density_xy[y+x+2].direction[i].blue+=
cristye1897792010-01-29 02:05:50 +0000674 cooccurrence[x][y].direction[i].blue;
cristye1897792010-01-29 02:05:50 +0000675 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000676 density_xy[y+x+2].direction[i].black+=
677 cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000678 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000679 density_xy[y+x+2].direction[i].alpha+=
680 cooccurrence[x][y].direction[i].alpha;
cristye1897792010-01-29 02:05:50 +0000681 /*
682 Entropy.
683 */
684 channel_features[RedChannel].entropy[i]-=
685 cooccurrence[x][y].direction[i].red*
686 log10(cooccurrence[x][y].direction[i].red+MagickEpsilon);
687 channel_features[GreenChannel].entropy[i]-=
688 cooccurrence[x][y].direction[i].green*
689 log10(cooccurrence[x][y].direction[i].green+MagickEpsilon);
690 channel_features[BlueChannel].entropy[i]-=
691 cooccurrence[x][y].direction[i].blue*
692 log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon);
cristye1897792010-01-29 02:05:50 +0000693 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000694 channel_features[BlackChannel].entropy[i]-=
695 cooccurrence[x][y].direction[i].black*
696 log10(cooccurrence[x][y].direction[i].black+MagickEpsilon);
cristy53a727d2011-06-16 14:53:56 +0000697 if (image->matte != MagickFalse)
698 channel_features[OpacityChannel].entropy[i]-=
cristy4c08aed2011-07-01 19:47:50 +0000699 cooccurrence[x][y].direction[i].alpha*
700 log10(cooccurrence[x][y].direction[i].alpha+MagickEpsilon);
cristye0acabf2010-01-30 00:52:38 +0000701 /*
702 Information Measures of Correlation.
703 */
cristyffa10d02010-01-30 17:53:27 +0000704 density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red;
705 density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green;
706 density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristy53a727d2011-06-16 14:53:56 +0000707 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000708 density_x[x].direction[i].alpha+=
709 cooccurrence[x][y].direction[i].alpha;
710 if (image->colorspace == CMYKColorspace)
711 density_x[x].direction[i].black+=
712 cooccurrence[x][y].direction[i].black;
cristyffa10d02010-01-30 17:53:27 +0000713 density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
714 density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
715 density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristye0acabf2010-01-30 00:52:38 +0000716 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000717 density_y[y].direction[i].black+=
718 cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000719 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000720 density_y[y].direction[i].alpha+=
721 cooccurrence[x][y].direction[i].alpha;
cristy7e9726d2010-01-26 02:08:40 +0000722 }
cristycdf8e1b2010-01-28 01:52:33 +0000723 mean.direction[i].red+=y*sum[y].direction[i].red;
724 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
725 mean.direction[i].green+=y*sum[y].direction[i].green;
726 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
727 mean.direction[i].blue+=y*sum[y].direction[i].blue;
728 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
cristycdf8e1b2010-01-28 01:52:33 +0000729 if (image->colorspace == CMYKColorspace)
730 {
cristy4c08aed2011-07-01 19:47:50 +0000731 mean.direction[i].black+=y*sum[y].direction[i].black;
732 sum_squares.direction[i].black+=y*y*sum[y].direction[i].black;
cristycdf8e1b2010-01-28 01:52:33 +0000733 }
cristy53a727d2011-06-16 14:53:56 +0000734 if (image->matte != MagickFalse)
735 {
cristy4c08aed2011-07-01 19:47:50 +0000736 mean.direction[i].alpha+=y*sum[y].direction[i].alpha;
737 sum_squares.direction[i].alpha+=y*y*sum[y].direction[i].alpha;
cristy53a727d2011-06-16 14:53:56 +0000738 }
cristy7e9726d2010-01-26 02:08:40 +0000739 }
cristycdf8e1b2010-01-28 01:52:33 +0000740 /*
741 Correlation: measure of linear-dependencies in the image.
742 */
743 channel_features[RedChannel].correlation[i]=
744 (correlation.direction[i].red-mean.direction[i].red*
745 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
746 (mean.direction[i].red*mean.direction[i].red))*sqrt(
747 sum_squares.direction[i].red-(mean.direction[i].red*
748 mean.direction[i].red)));
749 channel_features[GreenChannel].correlation[i]=
750 (correlation.direction[i].green-mean.direction[i].green*
751 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
752 (mean.direction[i].green*mean.direction[i].green))*sqrt(
753 sum_squares.direction[i].green-(mean.direction[i].green*
754 mean.direction[i].green)));
755 channel_features[BlueChannel].correlation[i]=
756 (correlation.direction[i].blue-mean.direction[i].blue*
757 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
758 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
759 sum_squares.direction[i].blue-(mean.direction[i].blue*
760 mean.direction[i].blue)));
cristycdf8e1b2010-01-28 01:52:33 +0000761 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000762 channel_features[BlackChannel].correlation[i]=
763 (correlation.direction[i].black-mean.direction[i].black*
764 mean.direction[i].black)/(sqrt(sum_squares.direction[i].black-
765 (mean.direction[i].black*mean.direction[i].black))*sqrt(
766 sum_squares.direction[i].black-(mean.direction[i].black*
767 mean.direction[i].black)));
cristy53a727d2011-06-16 14:53:56 +0000768 if (image->matte != MagickFalse)
769 channel_features[OpacityChannel].correlation[i]=
cristy4c08aed2011-07-01 19:47:50 +0000770 (correlation.direction[i].alpha-mean.direction[i].alpha*
771 mean.direction[i].alpha)/(sqrt(sum_squares.direction[i].alpha-
772 (mean.direction[i].alpha*mean.direction[i].alpha))*sqrt(
773 sum_squares.direction[i].alpha-(mean.direction[i].alpha*
774 mean.direction[i].alpha)));
cristy7e9726d2010-01-26 02:08:40 +0000775 }
cristycf5e6492010-01-28 02:45:28 +0000776 /*
777 Compute more texture features.
778 */
cristye1897792010-01-29 02:05:50 +0000779#if defined(MAGICKCORE_OPENMP_SUPPORT)
780 #pragma omp parallel for schedule(dynamic,4) shared(status)
781#endif
782 for (i=0; i < 4; i++)
783 {
cristybb503372010-05-27 20:51:26 +0000784 register ssize_t
cristye1897792010-01-29 02:05:50 +0000785 x;
786
cristybb503372010-05-27 20:51:26 +0000787 for (x=2; x < (ssize_t) (2*number_grays); x++)
cristye1897792010-01-29 02:05:50 +0000788 {
789 /*
790 Sum average.
791 */
792 channel_features[RedChannel].sum_average[i]+=
cristyffa10d02010-01-30 17:53:27 +0000793 x*density_xy[x].direction[i].red;
cristye1897792010-01-29 02:05:50 +0000794 channel_features[GreenChannel].sum_average[i]+=
cristyffa10d02010-01-30 17:53:27 +0000795 x*density_xy[x].direction[i].green;
cristye1897792010-01-29 02:05:50 +0000796 channel_features[BlueChannel].sum_average[i]+=
cristyffa10d02010-01-30 17:53:27 +0000797 x*density_xy[x].direction[i].blue;
cristye1897792010-01-29 02:05:50 +0000798 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000799 channel_features[BlackChannel].sum_average[i]+=
800 x*density_xy[x].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000801 if (image->matte != MagickFalse)
802 channel_features[OpacityChannel].sum_average[i]+=
cristy4c08aed2011-07-01 19:47:50 +0000803 x*density_xy[x].direction[i].alpha;
cristye1897792010-01-29 02:05:50 +0000804 /*
805 Sum entropy.
806 */
807 channel_features[RedChannel].sum_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +0000808 density_xy[x].direction[i].red*
809 log10(density_xy[x].direction[i].red+MagickEpsilon);
cristye1897792010-01-29 02:05:50 +0000810 channel_features[GreenChannel].sum_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +0000811 density_xy[x].direction[i].green*
812 log10(density_xy[x].direction[i].green+MagickEpsilon);
cristye1897792010-01-29 02:05:50 +0000813 channel_features[BlueChannel].sum_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +0000814 density_xy[x].direction[i].blue*
815 log10(density_xy[x].direction[i].blue+MagickEpsilon);
cristye1897792010-01-29 02:05:50 +0000816 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000817 channel_features[BlackChannel].sum_entropy[i]-=
818 density_xy[x].direction[i].black*
819 log10(density_xy[x].direction[i].black+MagickEpsilon);
cristy53a727d2011-06-16 14:53:56 +0000820 if (image->matte != MagickFalse)
821 channel_features[OpacityChannel].sum_entropy[i]-=
cristy4c08aed2011-07-01 19:47:50 +0000822 density_xy[x].direction[i].alpha*
823 log10(density_xy[x].direction[i].alpha+MagickEpsilon);
cristye1897792010-01-29 02:05:50 +0000824 /*
825 Sum variance.
826 */
827 channel_features[RedChannel].sum_variance[i]+=
828 (x-channel_features[RedChannel].sum_entropy[i])*
829 (x-channel_features[RedChannel].sum_entropy[i])*
cristyffa10d02010-01-30 17:53:27 +0000830 density_xy[x].direction[i].red;
cristye1897792010-01-29 02:05:50 +0000831 channel_features[GreenChannel].sum_variance[i]+=
832 (x-channel_features[GreenChannel].sum_entropy[i])*
833 (x-channel_features[GreenChannel].sum_entropy[i])*
cristyffa10d02010-01-30 17:53:27 +0000834 density_xy[x].direction[i].green;
cristye1897792010-01-29 02:05:50 +0000835 channel_features[BlueChannel].sum_variance[i]+=
836 (x-channel_features[BlueChannel].sum_entropy[i])*
837 (x-channel_features[BlueChannel].sum_entropy[i])*
cristyffa10d02010-01-30 17:53:27 +0000838 density_xy[x].direction[i].blue;
cristye1897792010-01-29 02:05:50 +0000839 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000840 channel_features[BlackChannel].sum_variance[i]+=
841 (x-channel_features[BlackChannel].sum_entropy[i])*
842 (x-channel_features[BlackChannel].sum_entropy[i])*
843 density_xy[x].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000844 if (image->matte != MagickFalse)
845 channel_features[OpacityChannel].sum_variance[i]+=
846 (x-channel_features[OpacityChannel].sum_entropy[i])*
847 (x-channel_features[OpacityChannel].sum_entropy[i])*
cristy4c08aed2011-07-01 19:47:50 +0000848 density_xy[x].direction[i].alpha;
cristye1897792010-01-29 02:05:50 +0000849 }
850 }
851 /*
852 Compute more texture features.
853 */
cristycf5e6492010-01-28 02:45:28 +0000854#if defined(MAGICKCORE_OPENMP_SUPPORT)
855 #pragma omp parallel for schedule(dynamic,4) shared(status)
856#endif
857 for (i=0; i < 4; i++)
858 {
cristybb503372010-05-27 20:51:26 +0000859 register ssize_t
cristycf5e6492010-01-28 02:45:28 +0000860 y;
861
cristybb503372010-05-27 20:51:26 +0000862 for (y=0; y < (ssize_t) number_grays; y++)
cristycf5e6492010-01-28 02:45:28 +0000863 {
cristybb503372010-05-27 20:51:26 +0000864 register ssize_t
cristycf5e6492010-01-28 02:45:28 +0000865 x;
866
cristybb503372010-05-27 20:51:26 +0000867 for (x=0; x < (ssize_t) number_grays; x++)
cristycf5e6492010-01-28 02:45:28 +0000868 {
869 /*
870 Sum of Squares: Variance
871 */
872 variance.direction[i].red+=(y-mean.direction[i].red+1)*
873 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
874 variance.direction[i].green+=(y-mean.direction[i].green+1)*
875 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
876 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
877 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
cristy53a727d2011-06-16 14:53:56 +0000878 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000879 variance.direction[i].black+=(y-mean.direction[i].black+1)*
880 (y-mean.direction[i].black+1)*cooccurrence[x][y].direction[i].black;
cristycf5e6492010-01-28 02:45:28 +0000881 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000882 variance.direction[i].alpha+=(y-mean.direction[i].alpha+1)*
883 (y-mean.direction[i].alpha+1)*
884 cooccurrence[x][y].direction[i].alpha;
cristye1897792010-01-29 02:05:50 +0000885 /*
cristye0acabf2010-01-30 00:52:38 +0000886 Sum average / Difference Variance.
cristye1897792010-01-29 02:05:50 +0000887 */
cristyffa10d02010-01-30 17:53:27 +0000888 density_xy[MagickAbsoluteValue(y-x)].direction[i].red+=
cristye1897792010-01-29 02:05:50 +0000889 cooccurrence[x][y].direction[i].red;
cristyffa10d02010-01-30 17:53:27 +0000890 density_xy[MagickAbsoluteValue(y-x)].direction[i].green+=
cristye1897792010-01-29 02:05:50 +0000891 cooccurrence[x][y].direction[i].green;
cristyffa10d02010-01-30 17:53:27 +0000892 density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+=
cristye1897792010-01-29 02:05:50 +0000893 cooccurrence[x][y].direction[i].blue;
cristye1897792010-01-29 02:05:50 +0000894 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000895 density_xy[MagickAbsoluteValue(y-x)].direction[i].black+=
896 cooccurrence[x][y].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000897 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000898 density_xy[MagickAbsoluteValue(y-x)].direction[i].alpha+=
899 cooccurrence[x][y].direction[i].alpha;
cristye0acabf2010-01-30 00:52:38 +0000900 /*
901 Information Measures of Correlation.
902 */
cristyffa10d02010-01-30 17:53:27 +0000903 entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red*
cristye0acabf2010-01-30 00:52:38 +0000904 log10(cooccurrence[x][y].direction[i].red+MagickEpsilon);
cristyffa10d02010-01-30 17:53:27 +0000905 entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green*
cristye0acabf2010-01-30 00:52:38 +0000906 log10(cooccurrence[x][y].direction[i].green+MagickEpsilon);
cristyffa10d02010-01-30 17:53:27 +0000907 entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue*
cristye0acabf2010-01-30 00:52:38 +0000908 log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon);
cristy53a727d2011-06-16 14:53:56 +0000909 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000910 entropy_xy.direction[i].black-=cooccurrence[x][y].direction[i].black*
911 log10(cooccurrence[x][y].direction[i].black+MagickEpsilon);
cristye0acabf2010-01-30 00:52:38 +0000912 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000913 entropy_xy.direction[i].alpha-=
914 cooccurrence[x][y].direction[i].alpha*log10(
915 cooccurrence[x][y].direction[i].alpha+MagickEpsilon);
cristyffa10d02010-01-30 17:53:27 +0000916 entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red*
917 log10(density_x[x].direction[i].red*density_y[y].direction[i].red+
918 MagickEpsilon));
919 entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green*
920 log10(density_x[x].direction[i].green*density_y[y].direction[i].green+
921 MagickEpsilon));
922 entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue*
923 log10(density_x[x].direction[i].blue*density_y[y].direction[i].blue+
924 MagickEpsilon));
cristye0acabf2010-01-30 00:52:38 +0000925 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000926 entropy_xy1.direction[i].black-=(
927 cooccurrence[x][y].direction[i].black*log10(
928 density_x[x].direction[i].black*density_y[y].direction[i].black+
cristyffa10d02010-01-30 17:53:27 +0000929 MagickEpsilon));
cristy53a727d2011-06-16 14:53:56 +0000930 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000931 entropy_xy1.direction[i].alpha-=(
932 cooccurrence[x][y].direction[i].alpha*log10(
933 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha+
cristy53a727d2011-06-16 14:53:56 +0000934 MagickEpsilon));
cristyffa10d02010-01-30 17:53:27 +0000935 entropy_xy2.direction[i].red-=(density_x[x].direction[i].red*
936 density_y[y].direction[i].red*log10(density_x[x].direction[i].red*
937 density_y[y].direction[i].red+MagickEpsilon));
938 entropy_xy2.direction[i].green-=(density_x[x].direction[i].green*
939 density_y[y].direction[i].green*log10(density_x[x].direction[i].green*
940 density_y[y].direction[i].green+MagickEpsilon));
941 entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue*
942 density_y[y].direction[i].blue*log10(density_x[x].direction[i].blue*
943 density_y[y].direction[i].blue+MagickEpsilon));
cristyffa10d02010-01-30 17:53:27 +0000944 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000945 entropy_xy2.direction[i].black-=(density_x[x].direction[i].black*
946 density_y[y].direction[i].black*log10(
947 density_x[x].direction[i].black*density_y[y].direction[i].black+
cristyffa10d02010-01-30 17:53:27 +0000948 MagickEpsilon));
cristy53a727d2011-06-16 14:53:56 +0000949 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000950 entropy_xy2.direction[i].alpha-=(density_x[x].direction[i].alpha*
951 density_y[y].direction[i].alpha*log10(
952 density_x[x].direction[i].alpha*density_y[y].direction[i].alpha+
cristy53a727d2011-06-16 14:53:56 +0000953 MagickEpsilon));
cristycf5e6492010-01-28 02:45:28 +0000954 }
955 }
956 channel_features[RedChannel].variance_sum_of_squares[i]=
957 variance.direction[i].red;
958 channel_features[GreenChannel].variance_sum_of_squares[i]=
959 variance.direction[i].green;
960 channel_features[BlueChannel].variance_sum_of_squares[i]=
961 variance.direction[i].blue;
cristycf5e6492010-01-28 02:45:28 +0000962 if (image->colorspace == CMYKColorspace)
963 channel_features[RedChannel].variance_sum_of_squares[i]=
cristy4c08aed2011-07-01 19:47:50 +0000964 variance.direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000965 if (image->matte != MagickFalse)
966 channel_features[RedChannel].variance_sum_of_squares[i]=
cristy4c08aed2011-07-01 19:47:50 +0000967 variance.direction[i].alpha;
cristycf5e6492010-01-28 02:45:28 +0000968 }
969 /*
970 Compute more texture features.
971 */
cristye1897792010-01-29 02:05:50 +0000972 (void) ResetMagickMemory(&variance,0,sizeof(variance));
973 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
974#if defined(MAGICKCORE_OPENMP_SUPPORT)
975 #pragma omp parallel for schedule(dynamic,4) shared(status)
976#endif
977 for (i=0; i < 4; i++)
978 {
cristybb503372010-05-27 20:51:26 +0000979 register ssize_t
cristye1897792010-01-29 02:05:50 +0000980 x;
981
cristybb503372010-05-27 20:51:26 +0000982 for (x=0; x < (ssize_t) number_grays; x++)
cristye1897792010-01-29 02:05:50 +0000983 {
cristye0acabf2010-01-30 00:52:38 +0000984 /*
985 Difference variance.
986 */
cristyffa10d02010-01-30 17:53:27 +0000987 variance.direction[i].red+=density_xy[x].direction[i].red;
988 variance.direction[i].green+=density_xy[x].direction[i].green;
989 variance.direction[i].blue+=density_xy[x].direction[i].blue;
cristye1897792010-01-29 02:05:50 +0000990 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +0000991 variance.direction[i].black+=density_xy[x].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +0000992 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +0000993 variance.direction[i].alpha+=density_xy[x].direction[i].alpha;
cristyffa10d02010-01-30 17:53:27 +0000994 sum_squares.direction[i].red+=density_xy[x].direction[i].red*
995 density_xy[x].direction[i].red;
996 sum_squares.direction[i].green+=density_xy[x].direction[i].green*
997 density_xy[x].direction[i].green;
998 sum_squares.direction[i].blue+=density_xy[x].direction[i].blue*
999 density_xy[x].direction[i].blue;
cristye1897792010-01-29 02:05:50 +00001000 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001001 sum_squares.direction[i].black+=density_xy[x].direction[i].black*
1002 density_xy[x].direction[i].black;
cristy53a727d2011-06-16 14:53:56 +00001003 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001004 sum_squares.direction[i].alpha+=density_xy[x].direction[i].alpha*
1005 density_xy[x].direction[i].alpha;
cristyf6214de2010-01-29 02:47:41 +00001006 /*
1007 Difference entropy.
1008 */
1009 channel_features[RedChannel].difference_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +00001010 density_xy[x].direction[i].red*
1011 log10(density_xy[x].direction[i].red+MagickEpsilon);
cristyf6214de2010-01-29 02:47:41 +00001012 channel_features[GreenChannel].difference_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +00001013 density_xy[x].direction[i].green*
1014 log10(density_xy[x].direction[i].green+MagickEpsilon);
cristyf6214de2010-01-29 02:47:41 +00001015 channel_features[BlueChannel].difference_entropy[i]-=
cristyffa10d02010-01-30 17:53:27 +00001016 density_xy[x].direction[i].blue*
1017 log10(density_xy[x].direction[i].blue+MagickEpsilon);
cristyf6214de2010-01-29 02:47:41 +00001018 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001019 channel_features[BlackChannel].difference_entropy[i]-=
1020 density_xy[x].direction[i].black*
1021 log10(density_xy[x].direction[i].black+MagickEpsilon);
cristy53a727d2011-06-16 14:53:56 +00001022 if (image->matte != MagickFalse)
1023 channel_features[OpacityChannel].difference_entropy[i]-=
cristy4c08aed2011-07-01 19:47:50 +00001024 density_xy[x].direction[i].alpha*
1025 log10(density_xy[x].direction[i].alpha+MagickEpsilon);
cristye0acabf2010-01-30 00:52:38 +00001026 /*
1027 Information Measures of Correlation.
1028 */
cristyffa10d02010-01-30 17:53:27 +00001029 entropy_x.direction[i].red-=(density_x[x].direction[i].red*
1030 log10(density_x[x].direction[i].red+MagickEpsilon));
1031 entropy_x.direction[i].green-=(density_x[x].direction[i].green*
1032 log10(density_x[x].direction[i].green+MagickEpsilon));
1033 entropy_x.direction[i].blue-=(density_x[x].direction[i].blue*
1034 log10(density_x[x].direction[i].blue+MagickEpsilon));
cristye0acabf2010-01-30 00:52:38 +00001035 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001036 entropy_x.direction[i].black-=(density_x[x].direction[i].black*
1037 log10(density_x[x].direction[i].black+MagickEpsilon));
cristye0acabf2010-01-30 00:52:38 +00001038 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001039 entropy_x.direction[i].alpha-=(density_x[x].direction[i].alpha*
1040 log10(density_x[x].direction[i].alpha+MagickEpsilon));
cristy53a727d2011-06-16 14:53:56 +00001041 entropy_y.direction[i].red-=(density_y[x].direction[i].red*
1042 log10(density_y[x].direction[i].red+MagickEpsilon));
1043 entropy_y.direction[i].green-=(density_y[x].direction[i].green*
1044 log10(density_y[x].direction[i].green+MagickEpsilon));
1045 entropy_y.direction[i].blue-=(density_y[x].direction[i].blue*
1046 log10(density_y[x].direction[i].blue+MagickEpsilon));
cristye0acabf2010-01-30 00:52:38 +00001047 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001048 entropy_y.direction[i].black-=(density_y[x].direction[i].black*
1049 log10(density_y[x].direction[i].black+MagickEpsilon));
cristy53a727d2011-06-16 14:53:56 +00001050 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001051 entropy_y.direction[i].alpha-=(density_y[x].direction[i].alpha*
1052 log10(density_y[x].direction[i].alpha+MagickEpsilon));
cristye1897792010-01-29 02:05:50 +00001053 }
cristyf6214de2010-01-29 02:47:41 +00001054 /*
1055 Difference variance.
1056 */
cristye1897792010-01-29 02:05:50 +00001057 channel_features[RedChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +00001058 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
cristye1897792010-01-29 02:05:50 +00001059 (variance.direction[i].red*variance.direction[i].red))/
1060 ((double) number_grays*number_grays*number_grays*number_grays);
1061 channel_features[GreenChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +00001062 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
cristye1897792010-01-29 02:05:50 +00001063 (variance.direction[i].green*variance.direction[i].green))/
1064 ((double) number_grays*number_grays*number_grays*number_grays);
1065 channel_features[BlueChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +00001066 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
cristye1897792010-01-29 02:05:50 +00001067 (variance.direction[i].blue*variance.direction[i].blue))/
1068 ((double) number_grays*number_grays*number_grays*number_grays);
cristy4c08aed2011-07-01 19:47:50 +00001069 if (image->colorspace == CMYKColorspace)
1070 channel_features[BlackChannel].difference_variance[i]=
1071 (((double) number_grays*number_grays*sum_squares.direction[i].black)-
1072 (variance.direction[i].black*variance.direction[i].black))/
1073 ((double) number_grays*number_grays*number_grays*number_grays);
cristye1897792010-01-29 02:05:50 +00001074 if (image->matte != MagickFalse)
1075 channel_features[OpacityChannel].difference_variance[i]=
cristy4c08aed2011-07-01 19:47:50 +00001076 (((double) number_grays*number_grays*sum_squares.direction[i].alpha)-
1077 (variance.direction[i].alpha*variance.direction[i].alpha))/
cristye1897792010-01-29 02:05:50 +00001078 ((double) number_grays*number_grays*number_grays*number_grays);
cristye0acabf2010-01-30 00:52:38 +00001079 /*
1080 Information Measures of Correlation.
1081 */
1082 channel_features[RedChannel].measure_of_correlation_1[i]=
cristyffa10d02010-01-30 17:53:27 +00001083 (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/
1084 (entropy_x.direction[i].red > entropy_y.direction[i].red ?
1085 entropy_x.direction[i].red : entropy_y.direction[i].red);
cristye0acabf2010-01-30 00:52:38 +00001086 channel_features[GreenChannel].measure_of_correlation_1[i]=
cristyffa10d02010-01-30 17:53:27 +00001087 (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/
1088 (entropy_x.direction[i].green > entropy_y.direction[i].green ?
1089 entropy_x.direction[i].green : entropy_y.direction[i].green);
cristye0acabf2010-01-30 00:52:38 +00001090 channel_features[BlueChannel].measure_of_correlation_1[i]=
cristyffa10d02010-01-30 17:53:27 +00001091 (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/
1092 (entropy_x.direction[i].blue > entropy_y.direction[i].blue ?
1093 entropy_x.direction[i].blue : entropy_y.direction[i].blue);
cristye0acabf2010-01-30 00:52:38 +00001094 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001095 channel_features[BlackChannel].measure_of_correlation_1[i]=
1096 (entropy_xy.direction[i].black-entropy_xy1.direction[i].black)/
1097 (entropy_x.direction[i].black > entropy_y.direction[i].black ?
1098 entropy_x.direction[i].black : entropy_y.direction[i].black);
cristy53a727d2011-06-16 14:53:56 +00001099 if (image->matte != MagickFalse)
1100 channel_features[OpacityChannel].measure_of_correlation_1[i]=
cristy4c08aed2011-07-01 19:47:50 +00001101 (entropy_xy.direction[i].alpha-entropy_xy1.direction[i].alpha)/
1102 (entropy_x.direction[i].alpha > entropy_y.direction[i].alpha ?
1103 entropy_x.direction[i].alpha : entropy_y.direction[i].alpha);
cristye0acabf2010-01-30 00:52:38 +00001104 channel_features[RedChannel].measure_of_correlation_2[i]=
cristyffa10d02010-01-30 17:53:27 +00001105 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].red-
1106 entropy_xy.direction[i].red)))));
cristye0acabf2010-01-30 00:52:38 +00001107 channel_features[GreenChannel].measure_of_correlation_2[i]=
cristyffa10d02010-01-30 17:53:27 +00001108 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].green-
1109 entropy_xy.direction[i].green)))));
cristye0acabf2010-01-30 00:52:38 +00001110 channel_features[BlueChannel].measure_of_correlation_2[i]=
cristyffa10d02010-01-30 17:53:27 +00001111 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].blue-
1112 entropy_xy.direction[i].blue)))));
cristye0acabf2010-01-30 00:52:38 +00001113 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001114 channel_features[BlackChannel].measure_of_correlation_2[i]=
1115 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].black-
1116 entropy_xy.direction[i].black)))));
cristy53a727d2011-06-16 14:53:56 +00001117 if (image->matte != MagickFalse)
1118 channel_features[OpacityChannel].measure_of_correlation_2[i]=
cristy4c08aed2011-07-01 19:47:50 +00001119 (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].alpha-
1120 entropy_xy.direction[i].alpha)))));
cristye1897792010-01-29 02:05:50 +00001121 }
1122 /*
1123 Compute more texture features.
1124 */
cristy3a82f252010-01-26 20:31:51 +00001125#if defined(MAGICKCORE_OPENMP_SUPPORT)
1126 #pragma omp parallel for schedule(dynamic,4) shared(status)
1127#endif
cristybd822072010-01-27 00:30:00 +00001128 for (i=0; i < 4; i++)
cristy3a82f252010-01-26 20:31:51 +00001129 {
cristybb503372010-05-27 20:51:26 +00001130 for (z=0; z < (ssize_t) number_grays; z++)
cristy3a82f252010-01-26 20:31:51 +00001131 {
cristybb503372010-05-27 20:51:26 +00001132 register ssize_t
cristybd822072010-01-27 00:30:00 +00001133 y;
cristy3a82f252010-01-26 20:31:51 +00001134
cristy7396d882010-01-27 02:37:56 +00001135 ChannelStatistics
cristybd822072010-01-27 00:30:00 +00001136 pixel;
1137
1138 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristybb503372010-05-27 20:51:26 +00001139 for (y=0; y < (ssize_t) number_grays; y++)
cristy3a82f252010-01-26 20:31:51 +00001140 {
cristybb503372010-05-27 20:51:26 +00001141 register ssize_t
cristybd822072010-01-27 00:30:00 +00001142 x;
1143
cristybb503372010-05-27 20:51:26 +00001144 for (x=0; x < (ssize_t) number_grays; x++)
cristy3a82f252010-01-26 20:31:51 +00001145 {
1146 /*
1147 Contrast: amount of local variations present in an image.
1148 */
1149 if (((y-x) == z) || ((x-y) == z))
1150 {
cristy7396d882010-01-27 02:37:56 +00001151 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
1152 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
1153 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristy53a727d2011-06-16 14:53:56 +00001154 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001155 pixel.direction[i].black+=cooccurrence[x][y].direction[i].black;
cristy3a82f252010-01-26 20:31:51 +00001156 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001157 pixel.direction[i].alpha+=
1158 cooccurrence[x][y].direction[i].alpha;
cristy3a82f252010-01-26 20:31:51 +00001159 }
cristyffa10d02010-01-30 17:53:27 +00001160 /*
1161 Maximum Correlation Coefficient.
1162 */
1163 Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red*
1164 cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/
1165 density_y[x].direction[i].red;
1166 Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green*
1167 cooccurrence[y][x].direction[i].green/
1168 density_x[z].direction[i].green/density_y[x].direction[i].red;
1169 Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue*
1170 cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/
1171 density_y[x].direction[i].blue;
cristy53a727d2011-06-16 14:53:56 +00001172 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001173 Q[z][y].direction[i].black+=cooccurrence[z][x].direction[i].black*
1174 cooccurrence[y][x].direction[i].black/
1175 density_x[z].direction[i].black/density_y[x].direction[i].black;
cristyffa10d02010-01-30 17:53:27 +00001176 if (image->matte != MagickFalse)
cristy4c08aed2011-07-01 19:47:50 +00001177 Q[z][y].direction[i].alpha+=
1178 cooccurrence[z][x].direction[i].alpha*
1179 cooccurrence[y][x].direction[i].alpha/
1180 density_x[z].direction[i].alpha/
1181 density_y[x].direction[i].alpha;
cristy3a82f252010-01-26 20:31:51 +00001182 }
1183 }
cristy7396d882010-01-27 02:37:56 +00001184 channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red;
1185 channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green;
1186 channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue;
cristy3a82f252010-01-26 20:31:51 +00001187 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +00001188 channel_features[BlackChannel].contrast[i]+=z*z*
cristy4c08aed2011-07-01 19:47:50 +00001189 pixel.direction[i].black;
cristy53a727d2011-06-16 14:53:56 +00001190 if (image->matte != MagickFalse)
1191 channel_features[OpacityChannel].contrast[i]+=z*z*
cristy4c08aed2011-07-01 19:47:50 +00001192 pixel.direction[i].alpha;
cristy3a82f252010-01-26 20:31:51 +00001193 }
cristye0acabf2010-01-30 00:52:38 +00001194 /*
1195 Maximum Correlation Coefficient.
cristyffa10d02010-01-30 17:53:27 +00001196 Future: return second largest eigenvalue of Q.
cristye0acabf2010-01-30 00:52:38 +00001197 */
1198 channel_features[RedChannel].maximum_correlation_coefficient[i]=
1199 sqrt((double) -1.0);
1200 channel_features[GreenChannel].maximum_correlation_coefficient[i]=
1201 sqrt((double) -1.0);
1202 channel_features[BlueChannel].maximum_correlation_coefficient[i]=
1203 sqrt((double) -1.0);
cristye0acabf2010-01-30 00:52:38 +00001204 if (image->colorspace == CMYKColorspace)
cristy4c08aed2011-07-01 19:47:50 +00001205 channel_features[BlackChannel].maximum_correlation_coefficient[i]=
cristye0acabf2010-01-30 00:52:38 +00001206 sqrt((double) -1.0);
cristy53a727d2011-06-16 14:53:56 +00001207 if (image->matte != MagickFalse)
1208 channel_features[OpacityChannel].maximum_correlation_coefficient[i]=
1209 sqrt((double) -1.0);
cristy3a82f252010-01-26 20:31:51 +00001210 }
cristy7e9726d2010-01-26 02:08:40 +00001211 /*
cristyf2bf2c72010-01-25 19:54:15 +00001212 Relinquish resources.
1213 */
cristy7396d882010-01-27 02:37:56 +00001214 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
cristybb503372010-05-27 20:51:26 +00001215 for (i=0; i < (ssize_t) number_grays; i++)
cristyffa10d02010-01-30 17:53:27 +00001216 Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]);
1217 Q=(ChannelStatistics **) RelinquishMagickMemory(Q);
1218 density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y);
1219 density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy);
1220 density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x);
cristybb503372010-05-27 20:51:26 +00001221 for (i=0; i < (ssize_t) number_grays; i++)
cristy7396d882010-01-27 02:37:56 +00001222 cooccurrence[i]=(ChannelStatistics *)
1223 RelinquishMagickMemory(cooccurrence[i]);
1224 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
cristy3e2860c2010-01-24 01:36:30 +00001225 return(channel_features);
1226}