blob: 23ba96279dc25e9df574904fe620cd350a8598b7 [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% %
20% Copyright 1999-2010 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% 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*/
43#include "magick/studio.h"
44#include "magick/property.h"
45#include "magick/animate.h"
46#include "magick/blob.h"
47#include "magick/blob-private.h"
48#include "magick/cache.h"
49#include "magick/cache-private.h"
50#include "magick/cache-view.h"
51#include "magick/client.h"
52#include "magick/color.h"
53#include "magick/color-private.h"
54#include "magick/colorspace.h"
55#include "magick/colorspace-private.h"
56#include "magick/composite.h"
57#include "magick/composite-private.h"
58#include "magick/compress.h"
59#include "magick/constitute.h"
60#include "magick/deprecate.h"
61#include "magick/display.h"
62#include "magick/draw.h"
63#include "magick/enhance.h"
64#include "magick/exception.h"
65#include "magick/exception-private.h"
66#include "magick/feature.h"
67#include "magick/gem.h"
68#include "magick/geometry.h"
69#include "magick/list.h"
70#include "magick/image-private.h"
71#include "magick/magic.h"
72#include "magick/magick.h"
73#include "magick/memory_.h"
74#include "magick/module.h"
75#include "magick/monitor.h"
76#include "magick/monitor-private.h"
77#include "magick/option.h"
78#include "magick/paint.h"
79#include "magick/pixel-private.h"
80#include "magick/profile.h"
81#include "magick/quantize.h"
82#include "magick/random_.h"
83#include "magick/segment.h"
84#include "magick/semaphore.h"
85#include "magick/signature-private.h"
86#include "magick/string_.h"
87#include "magick/thread-private.h"
88#include "magick/timer.h"
89#include "magick/utility.h"
90#include "magick/version.h"
91
92/*
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94% %
95% %
96% %
97% G e t I m a g e C h a n n e l F e a t u r e s %
98% %
99% %
100% %
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102%
cristy7396d882010-01-27 02:37:56 +0000103% GetImageChannelFeatures() returns features for each channel in the image in
104% 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%
cristy7e9726d2010-01-26 02:08:40 +0000111% channel_features=GetImageChannelFeatures(image,1,excepton);
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%
116% The format of the GetImageChannelFeatures method is:
117%
118% ChannelFeatures *GetImageChannelFeatures(const Image *image,
cristy549a37e2010-01-26 15:24:15 +0000119% const unsigned long 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
131static inline long MagickAbsoluteValue(const long x)
132{
133 if (x < 0)
134 return(-x);
135 return(x);
136}
137
cristy3e2860c2010-01-24 01:36:30 +0000138MagickExport ChannelFeatures *GetImageChannelFeatures(const Image *image,
cristy7e9726d2010-01-26 02:08:40 +0000139 const unsigned long 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 {
cristy549a37e2010-01-26 15:24:15 +0000143 DoublePixelPacket
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,
156 mean,
157 *sum,
cristye1897792010-01-29 02:05:50 +0000158 *sum_average,
cristycf5e6492010-01-28 02:45:28 +0000159 sum_squares,
160 variance;
cristy7396d882010-01-27 02:37:56 +0000161
cristy2070fa52010-01-24 03:17:57 +0000162 LongPixelPacket
cristy7396d882010-01-27 02:37:56 +0000163 gray,
164 *grays;
cristy2070fa52010-01-24 03:17:57 +0000165
cristy3e2860c2010-01-24 01:36:30 +0000166 long
cristy3a82f252010-01-26 20:31:51 +0000167 y,
168 z;
cristy3e2860c2010-01-24 01:36:30 +0000169
cristy2070fa52010-01-24 03:17:57 +0000170 MagickBooleanType
171 status;
172
173 register long
174 i;
175
cristy3e2860c2010-01-24 01:36:30 +0000176 size_t
177 length;
178
cristyf2bf2c72010-01-25 19:54:15 +0000179 unsigned long
cristy7396d882010-01-27 02:37:56 +0000180 number_grays;
cristyf2bf2c72010-01-25 19:54:15 +0000181
cristy3e2860c2010-01-24 01:36:30 +0000182 assert(image != (Image *) NULL);
183 assert(image->signature == MagickSignature);
184 if (image->debug != MagickFalse)
185 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy7e9726d2010-01-26 02:08:40 +0000186 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
187 return((ChannelFeatures *) NULL);
cristy3e2860c2010-01-24 01:36:30 +0000188 length=AllChannels+1UL;
189 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
190 sizeof(*channel_features));
191 if (channel_features == (ChannelFeatures *) NULL)
192 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
193 (void) ResetMagickMemory(channel_features,0,length*
194 sizeof(*channel_features));
cristy2070fa52010-01-24 03:17:57 +0000195 /*
cristy7396d882010-01-27 02:37:56 +0000196 Form grays.
cristy2070fa52010-01-24 03:17:57 +0000197 */
cristy7396d882010-01-27 02:37:56 +0000198 grays=(LongPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
199 if (grays == (LongPixelPacket *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000200 {
cristy2070fa52010-01-24 03:17:57 +0000201 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
202 channel_features);
cristye1897792010-01-29 02:05:50 +0000203 (void) ThrowMagickException(exception,GetMagickModule(),
204 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy2070fa52010-01-24 03:17:57 +0000205 return(channel_features);
206 }
207 for (i=0; i <= (long) MaxMap; i++)
208 {
cristy7396d882010-01-27 02:37:56 +0000209 grays[i].red=(~0UL);
210 grays[i].green=(~0UL);
211 grays[i].blue=(~0UL);
212 grays[i].opacity=(~0UL);
213 grays[i].index=(~0UL);
cristy2070fa52010-01-24 03:17:57 +0000214 }
215 status=MagickTrue;
216 image_view=AcquireCacheView(image);
217#if defined(MAGICKCORE_OPENMP_SUPPORT)
218 #pragma omp parallel for schedule(dynamic,4) shared(status)
219#endif
cristy3e2860c2010-01-24 01:36:30 +0000220 for (y=0; y < (long) image->rows; y++)
221 {
222 register const IndexPacket
223 *restrict indexes;
224
225 register const PixelPacket
226 *restrict p;
227
228 register long
229 x;
230
cristy2070fa52010-01-24 03:17:57 +0000231 if (status == MagickFalse)
232 continue;
233 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3e2860c2010-01-24 01:36:30 +0000234 if (p == (const PixelPacket *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000235 {
236 status=MagickFalse;
237 continue;
238 }
239 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy3e2860c2010-01-24 01:36:30 +0000240 for (x=0; x < (long) image->columns; x++)
241 {
cristy7396d882010-01-27 02:37:56 +0000242 grays[ScaleQuantumToMap(p->red)].red=ScaleQuantumToMap(p->red);
243 grays[ScaleQuantumToMap(p->green)].green=ScaleQuantumToMap(p->green);
244 grays[ScaleQuantumToMap(p->blue)].blue=ScaleQuantumToMap(p->blue);
cristy2070fa52010-01-24 03:17:57 +0000245 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000246 grays[ScaleQuantumToMap(p->opacity)].opacity=
cristy2070fa52010-01-24 03:17:57 +0000247 ScaleQuantumToMap(p->opacity);
248 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000249 grays[ScaleQuantumToMap(indexes[x])].index=
cristy2070fa52010-01-24 03:17:57 +0000250 ScaleQuantumToMap(indexes[x]);
cristy3e2860c2010-01-24 01:36:30 +0000251 p++;
252 }
253 }
cristy30c510a2010-01-24 03:43:00 +0000254 image_view=DestroyCacheView(image_view);
255 if (status == MagickFalse)
256 {
cristy7396d882010-01-27 02:37:56 +0000257 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristy30c510a2010-01-24 03:43:00 +0000258 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
259 channel_features);
260 return(channel_features);
261 }
cristy7396d882010-01-27 02:37:56 +0000262 (void) ResetMagickMemory(&gray,0,sizeof(gray));
cristy2070fa52010-01-24 03:17:57 +0000263 for (i=0; i <= (long) MaxMap; i++)
264 {
cristy7396d882010-01-27 02:37:56 +0000265 if (grays[i].red != ~0UL)
266 grays[gray.red++].red=grays[i].red;
267 if (grays[i].green != ~0UL)
268 grays[gray.green++].green=grays[i].green;
269 if (grays[i].blue != ~0UL)
270 grays[gray.blue++].blue=grays[i].blue;
cristy2070fa52010-01-24 03:17:57 +0000271 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000272 if (grays[i].opacity != ~0UL)
273 grays[gray.opacity++].opacity=grays[i].opacity;
cristy2070fa52010-01-24 03:17:57 +0000274 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000275 if (grays[i].index != ~0UL)
276 grays[gray.index++].index=grays[i].index;
cristy2070fa52010-01-24 03:17:57 +0000277 }
cristyf2bf2c72010-01-25 19:54:15 +0000278 /*
279 Allocate spatial dependence matrix.
280 */
cristy7396d882010-01-27 02:37:56 +0000281 number_grays=gray.red;
282 if (gray.green > number_grays)
283 number_grays=gray.green;
284 if (gray.blue > number_grays)
285 number_grays=gray.blue;
cristyf2bf2c72010-01-25 19:54:15 +0000286 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000287 if (gray.opacity > number_grays)
288 number_grays=gray.opacity;
cristyf2bf2c72010-01-25 19:54:15 +0000289 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000290 if (gray.index > number_grays)
291 number_grays=gray.index;
292 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
293 sizeof(*cooccurrence));
294 if (cooccurrence == (ChannelStatistics **) NULL)
cristyf2bf2c72010-01-25 19:54:15 +0000295 {
cristy7396d882010-01-27 02:37:56 +0000296 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000297 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
298 channel_features);
cristye1897792010-01-29 02:05:50 +0000299 (void) ThrowMagickException(exception,GetMagickModule(),
300 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000301 return(channel_features);
302 }
cristy7396d882010-01-27 02:37:56 +0000303 for (i=0; i < (long) number_grays; i++)
cristyf2bf2c72010-01-25 19:54:15 +0000304 {
cristy7396d882010-01-27 02:37:56 +0000305 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
306 sizeof(**cooccurrence));
307 if (cooccurrence[i] == (ChannelStatistics *) NULL)
cristyf2bf2c72010-01-25 19:54:15 +0000308 break;
cristy7396d882010-01-27 02:37:56 +0000309 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
310 sizeof(*cooccurrence));
cristyf2bf2c72010-01-25 19:54:15 +0000311 }
cristy7396d882010-01-27 02:37:56 +0000312 if (i < (long) number_grays)
cristyf2bf2c72010-01-25 19:54:15 +0000313 {
cristyf2bf2c72010-01-25 19:54:15 +0000314 for (i--; i >= 0; i--)
cristy7396d882010-01-27 02:37:56 +0000315 cooccurrence[i]=(ChannelStatistics *)
316 RelinquishMagickMemory(cooccurrence[i]);
317 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
318 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000319 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
320 channel_features);
cristye1897792010-01-29 02:05:50 +0000321 (void) ThrowMagickException(exception,GetMagickModule(),
322 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000323 return(channel_features);
324 }
325 /*
326 Initialize spatial dependence matrix.
327 */
328 status=MagickTrue;
329 image_view=AcquireCacheView(image);
330#if defined(MAGICKCORE_OPENMP_SUPPORT)
331 #pragma omp parallel for schedule(dynamic,4) shared(status)
332#endif
333 for (y=0; y < (long) image->rows; y++)
334 {
335 long
336 u,
337 v;
338
339 register const IndexPacket
340 *restrict indexes;
341
342 register const PixelPacket
343 *restrict p;
344
345 register long
346 x;
347
cristy7e9726d2010-01-26 02:08:40 +0000348 ssize_t
349 offset;
350
cristyf2bf2c72010-01-25 19:54:15 +0000351 if (status == MagickFalse)
352 continue;
cristy7e9726d2010-01-26 02:08:40 +0000353 p=GetCacheViewVirtualPixels(image_view,-(long) distance,y,image->columns+
354 2*distance,distance+1,exception);
cristyf2bf2c72010-01-25 19:54:15 +0000355 if (p == (const PixelPacket *) NULL)
356 {
357 status=MagickFalse;
358 continue;
359 }
360 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy7e9726d2010-01-26 02:08:40 +0000361 p+=distance;
362 indexes+=distance;
cristyf2bf2c72010-01-25 19:54:15 +0000363 for (x=0; x < (long) image->columns; x++)
364 {
365 for (i=0; i < 4; i++)
366 {
cristy7e9726d2010-01-26 02:08:40 +0000367 switch (i)
368 {
369 case 0:
cristy549a37e2010-01-26 15:24:15 +0000370 default:
cristy7e9726d2010-01-26 02:08:40 +0000371 {
372 /*
cristy7396d882010-01-27 02:37:56 +0000373 Horizontal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000374 */
375 offset=(ssize_t) distance;
376 break;
377 }
378 case 1:
379 {
380 /*
cristy7396d882010-01-27 02:37:56 +0000381 Vertical adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000382 */
cristy7396d882010-01-27 02:37:56 +0000383 offset=(ssize_t) (image->columns+2*distance);
cristy7e9726d2010-01-26 02:08:40 +0000384 break;
385 }
386 case 2:
387 {
388 /*
cristy7396d882010-01-27 02:37:56 +0000389 Right diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000390 */
cristy7396d882010-01-27 02:37:56 +0000391 offset=(ssize_t) (image->columns+2*distance)-distance;
cristy7e9726d2010-01-26 02:08:40 +0000392 break;
393 }
394 case 3:
395 {
396 /*
cristy7396d882010-01-27 02:37:56 +0000397 Left diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000398 */
399 offset=(ssize_t) (image->columns+2*distance)+distance;
400 break;
401 }
402 }
403 u=0;
404 v=0;
cristy7396d882010-01-27 02:37:56 +0000405 while (grays[u].red != ScaleQuantumToMap(p->red))
cristy7e9726d2010-01-26 02:08:40 +0000406 u++;
cristy7396d882010-01-27 02:37:56 +0000407 while (grays[v].red != ScaleQuantumToMap((p+offset)->red))
cristy7e9726d2010-01-26 02:08:40 +0000408 v++;
cristy7396d882010-01-27 02:37:56 +0000409 cooccurrence[u][v].direction[i].red++;
410 cooccurrence[v][u].direction[i].red++;
cristy7e9726d2010-01-26 02:08:40 +0000411 u=0;
412 v=0;
cristy7396d882010-01-27 02:37:56 +0000413 while (grays[u].green != ScaleQuantumToMap(p->green))
cristy7e9726d2010-01-26 02:08:40 +0000414 u++;
cristy7396d882010-01-27 02:37:56 +0000415 while (grays[v].green != ScaleQuantumToMap((p+offset)->green))
cristy7e9726d2010-01-26 02:08:40 +0000416 v++;
cristy7396d882010-01-27 02:37:56 +0000417 cooccurrence[u][v].direction[i].green++;
418 cooccurrence[v][u].direction[i].green++;
cristy7e9726d2010-01-26 02:08:40 +0000419 u=0;
420 v=0;
cristy7396d882010-01-27 02:37:56 +0000421 while (grays[u].blue != ScaleQuantumToMap(p->blue))
cristy7e9726d2010-01-26 02:08:40 +0000422 u++;
cristy7396d882010-01-27 02:37:56 +0000423 while (grays[v].blue != ScaleQuantumToMap((p+offset)->blue))
cristy7e9726d2010-01-26 02:08:40 +0000424 v++;
cristy7396d882010-01-27 02:37:56 +0000425 cooccurrence[u][v].direction[i].blue++;
426 cooccurrence[v][u].direction[i].blue++;
cristy7e9726d2010-01-26 02:08:40 +0000427 if (image->matte != MagickFalse)
428 {
429 u=0;
430 v=0;
cristy7396d882010-01-27 02:37:56 +0000431 while (grays[u].opacity != ScaleQuantumToMap(p->opacity))
cristy7e9726d2010-01-26 02:08:40 +0000432 u++;
cristy7396d882010-01-27 02:37:56 +0000433 while (grays[v].opacity != ScaleQuantumToMap((p+offset)->opacity))
cristy7e9726d2010-01-26 02:08:40 +0000434 v++;
cristy7396d882010-01-27 02:37:56 +0000435 cooccurrence[u][v].direction[i].opacity++;
436 cooccurrence[v][u].direction[i].opacity++;
cristy7e9726d2010-01-26 02:08:40 +0000437 }
438 if (image->colorspace == CMYKColorspace)
439 {
440 u=0;
441 v=0;
cristy7396d882010-01-27 02:37:56 +0000442 while (grays[u].index != ScaleQuantumToMap(indexes[x]))
cristy7e9726d2010-01-26 02:08:40 +0000443 u++;
cristy7396d882010-01-27 02:37:56 +0000444 while (grays[v].index != ScaleQuantumToMap(indexes[x+offset]))
cristy7e9726d2010-01-26 02:08:40 +0000445 v++;
cristy7396d882010-01-27 02:37:56 +0000446 cooccurrence[u][v].direction[i].index++;
447 cooccurrence[v][u].direction[i].index++;
cristy7e9726d2010-01-26 02:08:40 +0000448 }
cristyf2bf2c72010-01-25 19:54:15 +0000449 }
cristy7e9726d2010-01-26 02:08:40 +0000450 p++;
cristyf2bf2c72010-01-25 19:54:15 +0000451 }
452 }
453 image_view=DestroyCacheView(image_view);
454 if (status == MagickFalse)
455 {
cristy7396d882010-01-27 02:37:56 +0000456 for (i=0; i < (long) number_grays; i++)
457 cooccurrence[i]=(ChannelStatistics *)
458 RelinquishMagickMemory(cooccurrence[i]);
459 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
460 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000461 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
462 channel_features);
cristye1897792010-01-29 02:05:50 +0000463 (void) ThrowMagickException(exception,GetMagickModule(),
464 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristyf2bf2c72010-01-25 19:54:15 +0000465 return(channel_features);
466 }
467 /*
cristy7e9726d2010-01-26 02:08:40 +0000468 Normalize spatial dependence matrix.
469 */
470#if defined(MAGICKCORE_OPENMP_SUPPORT)
471 #pragma omp parallel for schedule(dynamic,4) shared(status)
472#endif
cristybd822072010-01-27 00:30:00 +0000473 for (i=0; i < 4; i++)
cristy7e9726d2010-01-26 02:08:40 +0000474 {
cristy549a37e2010-01-26 15:24:15 +0000475 double
476 normalize;
477
cristybd822072010-01-27 00:30:00 +0000478 switch (i)
cristy7e9726d2010-01-26 02:08:40 +0000479 {
cristybd822072010-01-27 00:30:00 +0000480 case 0:
481 default:
cristy7e9726d2010-01-26 02:08:40 +0000482 {
cristybd822072010-01-27 00:30:00 +0000483 /*
cristy7396d882010-01-27 02:37:56 +0000484 Horizontal adjacency.
cristybd822072010-01-27 00:30:00 +0000485 */
486 normalize=2.0*image->rows*(image->columns-distance);
487 break;
488 }
489 case 1:
490 {
491 /*
cristy7396d882010-01-27 02:37:56 +0000492 Vertical adjacency.
cristybd822072010-01-27 00:30:00 +0000493 */
cristy7396d882010-01-27 02:37:56 +0000494 normalize=2.0*(image->rows-distance)*image->columns;
cristybd822072010-01-27 00:30:00 +0000495 break;
496 }
497 case 2:
498 {
499 /*
cristy7396d882010-01-27 02:37:56 +0000500 Right diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000501 */
cristy7396d882010-01-27 02:37:56 +0000502 normalize=2.0*(image->rows-distance)*(image->columns-distance);
cristybd822072010-01-27 00:30:00 +0000503 break;
504 }
505 case 3:
506 {
507 /*
cristy7396d882010-01-27 02:37:56 +0000508 Left diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000509 */
510 normalize=2.0*(image->rows-distance)*(image->columns-distance);
511 break;
512 }
513 }
cristy7396d882010-01-27 02:37:56 +0000514 for (y=0; y < (long) number_grays; y++)
cristybd822072010-01-27 00:30:00 +0000515 {
516 register long
517 x;
518
cristy7396d882010-01-27 02:37:56 +0000519 for (x=0; x < (long) number_grays; x++)
cristybd822072010-01-27 00:30:00 +0000520 {
cristy7396d882010-01-27 02:37:56 +0000521 cooccurrence[x][y].direction[i].red/=normalize;
522 cooccurrence[x][y].direction[i].green/=normalize;
523 cooccurrence[x][y].direction[i].blue/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000524 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000525 cooccurrence[x][y].direction[i].opacity/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000526 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000527 cooccurrence[x][y].direction[i].index/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000528 }
529 }
530 }
531 /*
532 Compute texture features.
533 */
cristy7396d882010-01-27 02:37:56 +0000534 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
cristye1897792010-01-29 02:05:50 +0000535 sum_average=(ChannelStatistics *) AcquireQuantumMemory(2*number_grays,
536 sizeof(*sum_average));
537 if ((sum == (ChannelStatistics *) NULL) ||
538 (sum_average == (ChannelStatistics *) NULL))
cristy7396d882010-01-27 02:37:56 +0000539 {
cristye1897792010-01-29 02:05:50 +0000540 if (sum_average != (ChannelStatistics *) NULL)
541 sum_average=(ChannelStatistics *) RelinquishMagickMemory(sum_average);
542 if (sum != (ChannelStatistics *) NULL)
543 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
cristy7396d882010-01-27 02:37:56 +0000544 for (i=0; i < (long) number_grays; i++)
545 cooccurrence[i]=(ChannelStatistics *)
546 RelinquishMagickMemory(cooccurrence[i]);
547 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
548 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
549 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
550 channel_features);
cristye1897792010-01-29 02:05:50 +0000551 (void) ThrowMagickException(exception,GetMagickModule(),
552 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy7396d882010-01-27 02:37:56 +0000553 return(channel_features);
554 }
555 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
cristye1897792010-01-29 02:05:50 +0000556 (void) ResetMagickMemory(sum_average,0,2*number_grays*sizeof(*sum_average));
cristy7396d882010-01-27 02:37:56 +0000557 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
558 (void) ResetMagickMemory(&mean,0,sizeof(mean));
559 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
cristy3a82f252010-01-26 20:31:51 +0000560#if defined(MAGICKCORE_OPENMP_SUPPORT)
561 #pragma omp parallel for schedule(dynamic,4) shared(status)
562#endif
cristybd822072010-01-27 00:30:00 +0000563 for (i=0; i < 4; i++)
cristy549a37e2010-01-26 15:24:15 +0000564 {
565 register long
cristybd822072010-01-27 00:30:00 +0000566 y;
cristy549a37e2010-01-26 15:24:15 +0000567
cristy7396d882010-01-27 02:37:56 +0000568 for (y=0; y < (long) number_grays; y++)
cristy549a37e2010-01-26 15:24:15 +0000569 {
cristybd822072010-01-27 00:30:00 +0000570 register long
571 x;
572
cristy7396d882010-01-27 02:37:56 +0000573 for (x=0; x < (long) number_grays; x++)
cristy549a37e2010-01-26 15:24:15 +0000574 {
575 /*
cristy3a82f252010-01-26 20:31:51 +0000576 Angular second moment: measure of homogeneity of the image.
cristy549a37e2010-01-26 15:24:15 +0000577 */
578 channel_features[RedChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000579 cooccurrence[x][y].direction[i].red*
580 cooccurrence[x][y].direction[i].red;
cristy549a37e2010-01-26 15:24:15 +0000581 channel_features[GreenChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000582 cooccurrence[x][y].direction[i].green*
583 cooccurrence[x][y].direction[i].green;
cristy549a37e2010-01-26 15:24:15 +0000584 channel_features[BlueChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000585 cooccurrence[x][y].direction[i].blue*
586 cooccurrence[x][y].direction[i].blue;
cristy549a37e2010-01-26 15:24:15 +0000587 if (image->matte != MagickFalse)
588 channel_features[OpacityChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000589 cooccurrence[x][y].direction[i].opacity*
590 cooccurrence[x][y].direction[i].opacity;
cristy549a37e2010-01-26 15:24:15 +0000591 if (image->colorspace == CMYKColorspace)
cristy3a82f252010-01-26 20:31:51 +0000592 channel_features[BlackChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000593 cooccurrence[x][y].direction[i].index*
594 cooccurrence[x][y].direction[i].index;
595 /*
596 Correlation: measure of linear-dependencies in the image.
597 */
598 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000599 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
cristycdf8e1b2010-01-28 01:52:33 +0000600 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
601 if (image->matte != MagickFalse)
602 sum[y].direction[i].opacity+=cooccurrence[x][y].direction[i].opacity;
603 if (image->colorspace == CMYKColorspace)
604 sum[y].direction[i].index+=cooccurrence[x][y].direction[i].index;
605 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000606 correlation.direction[i].green+=x*y*
607 cooccurrence[x][y].direction[i].green;
cristy7396d882010-01-27 02:37:56 +0000608 correlation.direction[i].blue+=x*y*
609 cooccurrence[x][y].direction[i].blue;
cristy7396d882010-01-27 02:37:56 +0000610 if (image->matte != MagickFalse)
cristycdf8e1b2010-01-28 01:52:33 +0000611 correlation.direction[i].opacity+=x*y*
612 cooccurrence[x][y].direction[i].opacity;
cristy7396d882010-01-27 02:37:56 +0000613 if (image->colorspace == CMYKColorspace)
cristycdf8e1b2010-01-28 01:52:33 +0000614 correlation.direction[i].index+=x*y*
615 cooccurrence[x][y].direction[i].index;
cristycf5e6492010-01-28 02:45:28 +0000616 /*
617 Inverse Difference Moment.
618 */
cristy9fc6dbf2010-01-28 02:49:50 +0000619 channel_features[RedChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000620 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000621 channel_features[GreenChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000622 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000623 channel_features[BlueChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000624 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
625 if (image->matte != MagickFalse)
cristy9fc6dbf2010-01-28 02:49:50 +0000626 channel_features[OpacityChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000627 cooccurrence[x][y].direction[i].opacity/((y-x)*(y-x)+1);
628 if (image->colorspace == CMYKColorspace)
cristy9fc6dbf2010-01-28 02:49:50 +0000629 channel_features[IndexChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000630 cooccurrence[x][y].direction[i].index/((y-x)*(y-x)+1);
cristye1897792010-01-29 02:05:50 +0000631 /*
632 Sum average.
633 */
634 sum_average[y+x+2].direction[i].red+=
635 cooccurrence[x][y].direction[i].red;
636 sum_average[y+x+2].direction[i].green+=
637 cooccurrence[x][y].direction[i].green;
638 sum_average[y+x+2].direction[i].blue+=
639 cooccurrence[x][y].direction[i].blue;
640 if (image->matte != MagickFalse)
641 sum_average[y+x+2].direction[i].opacity+=
642 cooccurrence[x][y].direction[i].opacity;
643 if (image->colorspace == CMYKColorspace)
644 sum_average[y+x+2].direction[i].index+=
645 cooccurrence[x][y].direction[i].index;
646 /*
647 Entropy.
648 */
649 channel_features[RedChannel].entropy[i]-=
650 cooccurrence[x][y].direction[i].red*
651 log10(cooccurrence[x][y].direction[i].red+MagickEpsilon);
652 channel_features[GreenChannel].entropy[i]-=
653 cooccurrence[x][y].direction[i].green*
654 log10(cooccurrence[x][y].direction[i].green+MagickEpsilon);
655 channel_features[BlueChannel].entropy[i]-=
656 cooccurrence[x][y].direction[i].blue*
657 log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon);
658 if (image->matte != MagickFalse)
659 channel_features[OpacityChannel].entropy[i]-=
660 cooccurrence[x][y].direction[i].opacity*
661 log10(cooccurrence[x][y].direction[i].opacity+MagickEpsilon);
662 if (image->colorspace == CMYKColorspace)
663 channel_features[IndexChannel].entropy[i]-=
664 cooccurrence[x][y].direction[i].index*
665 log10(cooccurrence[x][y].direction[i].index+MagickEpsilon);
cristy7e9726d2010-01-26 02:08:40 +0000666 }
cristycdf8e1b2010-01-28 01:52:33 +0000667 mean.direction[i].red+=y*sum[y].direction[i].red;
668 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
669 mean.direction[i].green+=y*sum[y].direction[i].green;
670 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
671 mean.direction[i].blue+=y*sum[y].direction[i].blue;
672 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
673 if (image->matte != MagickFalse)
674 {
675 mean.direction[i].opacity+=y*sum[y].direction[i].opacity;
676 sum_squares.direction[i].opacity+=y*y*sum[y].direction[i].opacity;
677 }
678 if (image->colorspace == CMYKColorspace)
679 {
680 mean.direction[i].index+=y*sum[y].direction[i].index;
681 sum_squares.direction[i].index+=y*y*sum[y].direction[i].index;
682 }
cristy7e9726d2010-01-26 02:08:40 +0000683 }
cristycdf8e1b2010-01-28 01:52:33 +0000684 /*
685 Correlation: measure of linear-dependencies in the image.
686 */
687 channel_features[RedChannel].correlation[i]=
688 (correlation.direction[i].red-mean.direction[i].red*
689 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
690 (mean.direction[i].red*mean.direction[i].red))*sqrt(
691 sum_squares.direction[i].red-(mean.direction[i].red*
692 mean.direction[i].red)));
693 channel_features[GreenChannel].correlation[i]=
694 (correlation.direction[i].green-mean.direction[i].green*
695 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
696 (mean.direction[i].green*mean.direction[i].green))*sqrt(
697 sum_squares.direction[i].green-(mean.direction[i].green*
698 mean.direction[i].green)));
699 channel_features[BlueChannel].correlation[i]=
700 (correlation.direction[i].blue-mean.direction[i].blue*
701 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
702 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
703 sum_squares.direction[i].blue-(mean.direction[i].blue*
704 mean.direction[i].blue)));
705 if (image->matte != MagickFalse)
706 channel_features[OpacityChannel].correlation[i]=
707 (correlation.direction[i].opacity-mean.direction[i].opacity*
708 mean.direction[i].opacity)/(sqrt(sum_squares.direction[i].opacity-
709 (mean.direction[i].opacity*mean.direction[i].opacity))*sqrt(
710 sum_squares.direction[i].opacity-(mean.direction[i].opacity*
711 mean.direction[i].opacity)));
712 if (image->colorspace == CMYKColorspace)
713 channel_features[IndexChannel].correlation[i]=
714 (correlation.direction[i].index-mean.direction[i].index*
715 mean.direction[i].index)/(sqrt(sum_squares.direction[i].index-
716 (mean.direction[i].index*mean.direction[i].index))*sqrt(
717 sum_squares.direction[i].index-(mean.direction[i].index*
718 mean.direction[i].index)));
cristy7e9726d2010-01-26 02:08:40 +0000719 }
cristycf5e6492010-01-28 02:45:28 +0000720 /*
721 Compute more texture features.
722 */
cristye1897792010-01-29 02:05:50 +0000723#if defined(MAGICKCORE_OPENMP_SUPPORT)
724 #pragma omp parallel for schedule(dynamic,4) shared(status)
725#endif
726 for (i=0; i < 4; i++)
727 {
728 register long
729 x;
730
731 for (x=2; x < (long) (2*number_grays); x++)
732 {
733 /*
734 Sum average.
735 */
736 channel_features[RedChannel].sum_average[i]+=
737 x*sum_average[x].direction[i].red;
738 channel_features[GreenChannel].sum_average[i]+=
739 x*sum_average[x].direction[i].green;
740 channel_features[BlueChannel].sum_average[i]+=
741 x*sum_average[x].direction[i].blue;
742 if (image->matte != MagickFalse)
743 channel_features[OpacityChannel].sum_average[i]+=
744 x*sum_average[x].direction[i].opacity;
745 if (image->colorspace == CMYKColorspace)
746 channel_features[IndexChannel].sum_average[i]+=
747 x*sum_average[x].direction[i].index;
748 /*
749 Sum entropy.
750 */
751 channel_features[RedChannel].sum_entropy[i]-=
752 sum_average[x].direction[i].red*
753 log10(sum_average[x].direction[i].red+MagickEpsilon);
754 channel_features[GreenChannel].sum_entropy[i]-=
755 sum_average[x].direction[i].green*
756 log10(sum_average[x].direction[i].green+MagickEpsilon);
757 channel_features[BlueChannel].sum_entropy[i]-=
758 sum_average[x].direction[i].blue*
759 log10(sum_average[x].direction[i].blue+MagickEpsilon);
760 if (image->matte != MagickFalse)
761 channel_features[OpacityChannel].sum_entropy[i]-=
762 sum_average[x].direction[i].opacity*
763 log10(sum_average[x].direction[i].opacity+MagickEpsilon);
764 if (image->colorspace == CMYKColorspace)
765 channel_features[IndexChannel].sum_entropy[i]-=
766 sum_average[x].direction[i].index*
767 log10(sum_average[x].direction[i].index+MagickEpsilon);
768 /*
769 Sum variance.
770 */
771 channel_features[RedChannel].sum_variance[i]+=
772 (x-channel_features[RedChannel].sum_entropy[i])*
773 (x-channel_features[RedChannel].sum_entropy[i])*
774 sum_average[x].direction[i].red;
775 channel_features[GreenChannel].sum_variance[i]+=
776 (x-channel_features[GreenChannel].sum_entropy[i])*
777 (x-channel_features[GreenChannel].sum_entropy[i])*
778 sum_average[x].direction[i].green;
779 channel_features[BlueChannel].sum_variance[i]+=
780 (x-channel_features[BlueChannel].sum_entropy[i])*
781 (x-channel_features[BlueChannel].sum_entropy[i])*
782 sum_average[x].direction[i].blue;
783 if (image->matte != MagickFalse)
784 channel_features[OpacityChannel].sum_variance[i]+=
785 (x-channel_features[OpacityChannel].sum_entropy[i])*
786 (x-channel_features[OpacityChannel].sum_entropy[i])*
787 sum_average[x].direction[i].opacity;
788 if (image->colorspace == CMYKColorspace)
789 channel_features[IndexChannel].sum_variance[i]+=
790 (x-channel_features[IndexChannel].sum_entropy[i])*
791 (x-channel_features[IndexChannel].sum_entropy[i])*
792 sum_average[x].direction[i].index;
793 }
794 }
795 /*
796 Compute more texture features.
797 */
cristycf5e6492010-01-28 02:45:28 +0000798 (void) ResetMagickMemory(&variance,0,sizeof(variance));
cristye1897792010-01-29 02:05:50 +0000799 (void) ResetMagickMemory(sum_average,0,2*number_grays*sizeof(*sum_average));
cristycf5e6492010-01-28 02:45:28 +0000800#if defined(MAGICKCORE_OPENMP_SUPPORT)
801 #pragma omp parallel for schedule(dynamic,4) shared(status)
802#endif
803 for (i=0; i < 4; i++)
804 {
805 register long
806 y;
807
808 for (y=0; y < (long) number_grays; y++)
809 {
810 register long
811 x;
812
813 for (x=0; x < (long) number_grays; x++)
814 {
815 /*
816 Sum of Squares: Variance
817 */
818 variance.direction[i].red+=(y-mean.direction[i].red+1)*
819 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
820 variance.direction[i].green+=(y-mean.direction[i].green+1)*
821 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
822 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
823 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
824 if (image->matte != MagickFalse)
825 variance.direction[i].opacity+=(y-mean.direction[i].opacity+1)*
826 (y-mean.direction[i].opacity+1)*
827 cooccurrence[x][y].direction[i].opacity;
828 if (image->colorspace == CMYKColorspace)
829 variance.direction[i].index+=(y-mean.direction[i].index+1)*
830 (y-mean.direction[i].index+1)*cooccurrence[x][y].direction[i].index;
cristye1897792010-01-29 02:05:50 +0000831 /*
832 Sum average.
833 */
834 sum_average[MagickAbsoluteValue(y-x)].direction[i].red+=
835 cooccurrence[x][y].direction[i].red;
836 sum_average[MagickAbsoluteValue(y-x)].direction[i].green+=
837 cooccurrence[x][y].direction[i].green;
838 sum_average[MagickAbsoluteValue(y-x)].direction[i].blue+=
839 cooccurrence[x][y].direction[i].blue;
840 if (image->matte != MagickFalse)
841 sum_average[MagickAbsoluteValue(y-x)].direction[i].opacity+=
842 cooccurrence[x][y].direction[i].opacity;
843 if (image->colorspace == CMYKColorspace)
844 sum_average[MagickAbsoluteValue(y-x)].direction[i].index+=
845 cooccurrence[x][y].direction[i].index;
cristycf5e6492010-01-28 02:45:28 +0000846 }
847 }
848 channel_features[RedChannel].variance_sum_of_squares[i]=
849 variance.direction[i].red;
850 channel_features[GreenChannel].variance_sum_of_squares[i]=
851 variance.direction[i].green;
852 channel_features[BlueChannel].variance_sum_of_squares[i]=
853 variance.direction[i].blue;
854 if (image->matte != MagickFalse)
855 channel_features[RedChannel].variance_sum_of_squares[i]=
856 variance.direction[i].opacity;
857 if (image->colorspace == CMYKColorspace)
858 channel_features[RedChannel].variance_sum_of_squares[i]=
859 variance.direction[i].index;
860 }
861 /*
862 Compute more texture features.
863 */
cristye1897792010-01-29 02:05:50 +0000864 (void) ResetMagickMemory(&variance,0,sizeof(variance));
865 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
866#if defined(MAGICKCORE_OPENMP_SUPPORT)
867 #pragma omp parallel for schedule(dynamic,4) shared(status)
868#endif
869 for (i=0; i < 4; i++)
870 {
871 register long
872 x;
873
874 for (x=0; x < (long) number_grays; x++)
875 {
876 variance.direction[i].red+=sum_average[x].direction[i].red;
877 variance.direction[i].green+=sum_average[x].direction[i].green;
878 variance.direction[i].blue+=sum_average[x].direction[i].blue;
879 if (image->matte != MagickFalse)
880 variance.direction[i].opacity+=sum_average[x].direction[i].opacity;
881 if (image->colorspace == CMYKColorspace)
882 variance.direction[i].index+=sum_average[x].direction[i].index;
883 sum_squares.direction[i].red+=sum_average[x].direction[i].red*
884 sum_average[x].direction[i].red;
885 sum_squares.direction[i].green+=sum_average[x].direction[i].green*
886 sum_average[x].direction[i].green;
887 sum_squares.direction[i].blue+=sum_average[x].direction[i].blue*
888 sum_average[x].direction[i].blue;
889 if (image->matte != MagickFalse)
890 sum_squares.direction[i].opacity+=sum_average[x].direction[i].opacity*
891 sum_average[x].direction[i].opacity;
892 if (image->colorspace == CMYKColorspace)
893 sum_squares.direction[i].index+=sum_average[x].direction[i].index*
894 sum_average[x].direction[i].index;
895 }
896 channel_features[RedChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +0000897 (((double) number_grays*number_grays*sum_squares.direction[i].red)-
cristye1897792010-01-29 02:05:50 +0000898 (variance.direction[i].red*variance.direction[i].red))/
899 ((double) number_grays*number_grays*number_grays*number_grays);
900 channel_features[GreenChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +0000901 (((double) number_grays*number_grays*sum_squares.direction[i].green)-
cristye1897792010-01-29 02:05:50 +0000902 (variance.direction[i].green*variance.direction[i].green))/
903 ((double) number_grays*number_grays*number_grays*number_grays);
904 channel_features[BlueChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +0000905 (((double) number_grays*number_grays*sum_squares.direction[i].blue)-
cristye1897792010-01-29 02:05:50 +0000906 (variance.direction[i].blue*variance.direction[i].blue))/
907 ((double) number_grays*number_grays*number_grays*number_grays);
908 if (image->matte != MagickFalse)
909 channel_features[OpacityChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +0000910 (((double) number_grays*number_grays*sum_squares.direction[i].opacity)-
cristye1897792010-01-29 02:05:50 +0000911 (variance.direction[i].opacity*variance.direction[i].opacity))/
912 ((double) number_grays*number_grays*number_grays*number_grays);
913 if (image->colorspace == CMYKColorspace)
914 channel_features[IndexChannel].difference_variance[i]=
cristye0e19dc2010-01-29 02:13:08 +0000915 (((double) number_grays*number_grays*sum_squares.direction[i].index)-
cristye1897792010-01-29 02:05:50 +0000916 (variance.direction[i].index*variance.direction[i].index))/
917 ((double) number_grays*number_grays*number_grays*number_grays);
918 }
919 /*
920 Compute more texture features.
921 */
cristy3a82f252010-01-26 20:31:51 +0000922#if defined(MAGICKCORE_OPENMP_SUPPORT)
923 #pragma omp parallel for schedule(dynamic,4) shared(status)
924#endif
cristybd822072010-01-27 00:30:00 +0000925 for (i=0; i < 4; i++)
cristy3a82f252010-01-26 20:31:51 +0000926 {
cristy7396d882010-01-27 02:37:56 +0000927 for (z=0; z < (long) number_grays; z++)
cristy3a82f252010-01-26 20:31:51 +0000928 {
929 register long
cristybd822072010-01-27 00:30:00 +0000930 y;
cristy3a82f252010-01-26 20:31:51 +0000931
cristy7396d882010-01-27 02:37:56 +0000932 ChannelStatistics
cristybd822072010-01-27 00:30:00 +0000933 pixel;
934
935 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy7396d882010-01-27 02:37:56 +0000936 for (y=0; y < (long) number_grays; y++)
cristy3a82f252010-01-26 20:31:51 +0000937 {
cristybd822072010-01-27 00:30:00 +0000938 register long
939 x;
940
cristy7396d882010-01-27 02:37:56 +0000941 for (x=0; x < (long) number_grays; x++)
cristy3a82f252010-01-26 20:31:51 +0000942 {
943 /*
944 Contrast: amount of local variations present in an image.
945 */
946 if (((y-x) == z) || ((x-y) == z))
947 {
cristy7396d882010-01-27 02:37:56 +0000948 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
949 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
950 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristy3a82f252010-01-26 20:31:51 +0000951 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000952 pixel.direction[i].opacity+=
953 cooccurrence[x][y].direction[i].opacity;
cristy3a82f252010-01-26 20:31:51 +0000954 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000955 pixel.direction[i].index+=cooccurrence[x][y].direction[i].index;
cristy3a82f252010-01-26 20:31:51 +0000956 }
957 }
958 }
cristy7396d882010-01-27 02:37:56 +0000959 channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red;
960 channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green;
961 channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue;
cristy3a82f252010-01-26 20:31:51 +0000962 if (image->matte != MagickFalse)
963 channel_features[OpacityChannel].contrast[i]+=z*z*
cristy7396d882010-01-27 02:37:56 +0000964 pixel.direction[i].opacity;
cristy3a82f252010-01-26 20:31:51 +0000965 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000966 channel_features[BlackChannel].contrast[i]+=z*z*
967 pixel.direction[i].index;
cristy3a82f252010-01-26 20:31:51 +0000968 }
969 }
cristy7e9726d2010-01-26 02:08:40 +0000970 /*
cristyf2bf2c72010-01-25 19:54:15 +0000971 Relinquish resources.
972 */
cristye1897792010-01-29 02:05:50 +0000973 sum_average=(ChannelStatistics *) RelinquishMagickMemory(sum_average);
cristy7396d882010-01-27 02:37:56 +0000974 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
975 for (i=0; i < (long) number_grays; i++)
976 cooccurrence[i]=(ChannelStatistics *)
977 RelinquishMagickMemory(cooccurrence[i]);
978 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
979 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristy3e2860c2010-01-24 01:36:30 +0000980 return(channel_features);
981}