blob: 412b07b86665ff2e06502d0be6183e63a4093a75 [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*/
130MagickExport ChannelFeatures *GetImageChannelFeatures(const Image *image,
cristy7e9726d2010-01-26 02:08:40 +0000131 const unsigned long distance,ExceptionInfo *exception)
cristy3e2860c2010-01-24 01:36:30 +0000132{
cristy7396d882010-01-27 02:37:56 +0000133 typedef struct _ChannelStatistics
cristyf2bf2c72010-01-25 19:54:15 +0000134 {
cristy549a37e2010-01-26 15:24:15 +0000135 DoublePixelPacket
cristy7396d882010-01-27 02:37:56 +0000136 direction[4]; /* horizontal, vertical, left and right diagonals */
137 } ChannelStatistics;
cristyf2bf2c72010-01-25 19:54:15 +0000138
cristy2070fa52010-01-24 03:17:57 +0000139 CacheView
140 *image_view;
141
cristy3e2860c2010-01-24 01:36:30 +0000142 ChannelFeatures
143 *channel_features;
144
cristy7396d882010-01-27 02:37:56 +0000145 ChannelStatistics
146 **cooccurrence,
147 correlation,
148 mean,
149 *sum,
cristycf5e6492010-01-28 02:45:28 +0000150 sum_squares,
151 variance;
cristy7396d882010-01-27 02:37:56 +0000152
cristy2070fa52010-01-24 03:17:57 +0000153 LongPixelPacket
cristy7396d882010-01-27 02:37:56 +0000154 gray,
155 *grays;
cristy2070fa52010-01-24 03:17:57 +0000156
cristy3e2860c2010-01-24 01:36:30 +0000157 long
cristy3a82f252010-01-26 20:31:51 +0000158 y,
159 z;
cristy3e2860c2010-01-24 01:36:30 +0000160
cristy2070fa52010-01-24 03:17:57 +0000161 MagickBooleanType
162 status;
163
164 register long
165 i;
166
cristy3e2860c2010-01-24 01:36:30 +0000167 size_t
168 length;
169
cristyf2bf2c72010-01-25 19:54:15 +0000170 unsigned long
cristy7396d882010-01-27 02:37:56 +0000171 number_grays;
cristyf2bf2c72010-01-25 19:54:15 +0000172
cristy3e2860c2010-01-24 01:36:30 +0000173 assert(image != (Image *) NULL);
174 assert(image->signature == MagickSignature);
175 if (image->debug != MagickFalse)
176 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
cristy7e9726d2010-01-26 02:08:40 +0000177 if ((image->columns < (distance+1)) || (image->rows < (distance+1)))
178 return((ChannelFeatures *) NULL);
cristy3e2860c2010-01-24 01:36:30 +0000179 length=AllChannels+1UL;
180 channel_features=(ChannelFeatures *) AcquireQuantumMemory(length,
181 sizeof(*channel_features));
182 if (channel_features == (ChannelFeatures *) NULL)
183 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
184 (void) ResetMagickMemory(channel_features,0,length*
185 sizeof(*channel_features));
cristy2070fa52010-01-24 03:17:57 +0000186 /*
cristy7396d882010-01-27 02:37:56 +0000187 Form grays.
cristy2070fa52010-01-24 03:17:57 +0000188 */
cristy7396d882010-01-27 02:37:56 +0000189 grays=(LongPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays));
190 if (grays == (LongPixelPacket *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000191 {
192 (void) ThrowMagickException(exception,GetMagickModule(),
193 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
194 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
195 channel_features);
196 return(channel_features);
197 }
198 for (i=0; i <= (long) MaxMap; i++)
199 {
cristy7396d882010-01-27 02:37:56 +0000200 grays[i].red=(~0UL);
201 grays[i].green=(~0UL);
202 grays[i].blue=(~0UL);
203 grays[i].opacity=(~0UL);
204 grays[i].index=(~0UL);
cristy2070fa52010-01-24 03:17:57 +0000205 }
206 status=MagickTrue;
207 image_view=AcquireCacheView(image);
208#if defined(MAGICKCORE_OPENMP_SUPPORT)
209 #pragma omp parallel for schedule(dynamic,4) shared(status)
210#endif
cristy3e2860c2010-01-24 01:36:30 +0000211 for (y=0; y < (long) image->rows; y++)
212 {
213 register const IndexPacket
214 *restrict indexes;
215
216 register const PixelPacket
217 *restrict p;
218
219 register long
220 x;
221
cristy2070fa52010-01-24 03:17:57 +0000222 if (status == MagickFalse)
223 continue;
224 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy3e2860c2010-01-24 01:36:30 +0000225 if (p == (const PixelPacket *) NULL)
cristy2070fa52010-01-24 03:17:57 +0000226 {
227 status=MagickFalse;
228 continue;
229 }
230 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy3e2860c2010-01-24 01:36:30 +0000231 for (x=0; x < (long) image->columns; x++)
232 {
cristy7396d882010-01-27 02:37:56 +0000233 grays[ScaleQuantumToMap(p->red)].red=ScaleQuantumToMap(p->red);
234 grays[ScaleQuantumToMap(p->green)].green=ScaleQuantumToMap(p->green);
235 grays[ScaleQuantumToMap(p->blue)].blue=ScaleQuantumToMap(p->blue);
cristy2070fa52010-01-24 03:17:57 +0000236 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000237 grays[ScaleQuantumToMap(p->opacity)].opacity=
cristy2070fa52010-01-24 03:17:57 +0000238 ScaleQuantumToMap(p->opacity);
239 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000240 grays[ScaleQuantumToMap(indexes[x])].index=
cristy2070fa52010-01-24 03:17:57 +0000241 ScaleQuantumToMap(indexes[x]);
cristy3e2860c2010-01-24 01:36:30 +0000242 p++;
243 }
244 }
cristy30c510a2010-01-24 03:43:00 +0000245 image_view=DestroyCacheView(image_view);
246 if (status == MagickFalse)
247 {
cristy7396d882010-01-27 02:37:56 +0000248 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristy30c510a2010-01-24 03:43:00 +0000249 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
250 channel_features);
251 return(channel_features);
252 }
cristy7396d882010-01-27 02:37:56 +0000253 (void) ResetMagickMemory(&gray,0,sizeof(gray));
cristy2070fa52010-01-24 03:17:57 +0000254 for (i=0; i <= (long) MaxMap; i++)
255 {
cristy7396d882010-01-27 02:37:56 +0000256 if (grays[i].red != ~0UL)
257 grays[gray.red++].red=grays[i].red;
258 if (grays[i].green != ~0UL)
259 grays[gray.green++].green=grays[i].green;
260 if (grays[i].blue != ~0UL)
261 grays[gray.blue++].blue=grays[i].blue;
cristy2070fa52010-01-24 03:17:57 +0000262 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000263 if (grays[i].opacity != ~0UL)
264 grays[gray.opacity++].opacity=grays[i].opacity;
cristy2070fa52010-01-24 03:17:57 +0000265 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000266 if (grays[i].index != ~0UL)
267 grays[gray.index++].index=grays[i].index;
cristy2070fa52010-01-24 03:17:57 +0000268 }
cristyf2bf2c72010-01-25 19:54:15 +0000269 /*
270 Allocate spatial dependence matrix.
271 */
cristy7396d882010-01-27 02:37:56 +0000272 number_grays=gray.red;
273 if (gray.green > number_grays)
274 number_grays=gray.green;
275 if (gray.blue > number_grays)
276 number_grays=gray.blue;
cristyf2bf2c72010-01-25 19:54:15 +0000277 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000278 if (gray.opacity > number_grays)
279 number_grays=gray.opacity;
cristyf2bf2c72010-01-25 19:54:15 +0000280 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000281 if (gray.index > number_grays)
282 number_grays=gray.index;
283 cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays,
284 sizeof(*cooccurrence));
285 if (cooccurrence == (ChannelStatistics **) NULL)
cristyf2bf2c72010-01-25 19:54:15 +0000286 {
287 (void) ThrowMagickException(exception,GetMagickModule(),
288 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy7396d882010-01-27 02:37:56 +0000289 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000290 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
291 channel_features);
292 return(channel_features);
293 }
cristy7396d882010-01-27 02:37:56 +0000294 for (i=0; i < (long) number_grays; i++)
cristyf2bf2c72010-01-25 19:54:15 +0000295 {
cristy7396d882010-01-27 02:37:56 +0000296 cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,
297 sizeof(**cooccurrence));
298 if (cooccurrence[i] == (ChannelStatistics *) NULL)
cristyf2bf2c72010-01-25 19:54:15 +0000299 break;
cristy7396d882010-01-27 02:37:56 +0000300 (void) ResetMagickMemory(cooccurrence[i],0,number_grays*
301 sizeof(*cooccurrence));
cristyf2bf2c72010-01-25 19:54:15 +0000302 }
cristy7396d882010-01-27 02:37:56 +0000303 if (i < (long) number_grays)
cristyf2bf2c72010-01-25 19:54:15 +0000304 {
305 (void) ThrowMagickException(exception,GetMagickModule(),
306 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
307 for (i--; i >= 0; i--)
cristy7396d882010-01-27 02:37:56 +0000308 cooccurrence[i]=(ChannelStatistics *)
309 RelinquishMagickMemory(cooccurrence[i]);
310 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
311 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000312 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
313 channel_features);
314 return(channel_features);
315 }
316 /*
317 Initialize spatial dependence matrix.
318 */
319 status=MagickTrue;
320 image_view=AcquireCacheView(image);
321#if defined(MAGICKCORE_OPENMP_SUPPORT)
322 #pragma omp parallel for schedule(dynamic,4) shared(status)
323#endif
324 for (y=0; y < (long) image->rows; y++)
325 {
326 long
327 u,
328 v;
329
330 register const IndexPacket
331 *restrict indexes;
332
333 register const PixelPacket
334 *restrict p;
335
336 register long
337 x;
338
cristy7e9726d2010-01-26 02:08:40 +0000339 ssize_t
340 offset;
341
cristyf2bf2c72010-01-25 19:54:15 +0000342 if (status == MagickFalse)
343 continue;
cristy7e9726d2010-01-26 02:08:40 +0000344 p=GetCacheViewVirtualPixels(image_view,-(long) distance,y,image->columns+
345 2*distance,distance+1,exception);
cristyf2bf2c72010-01-25 19:54:15 +0000346 if (p == (const PixelPacket *) NULL)
347 {
348 status=MagickFalse;
349 continue;
350 }
351 indexes=GetCacheViewVirtualIndexQueue(image_view);
cristy7e9726d2010-01-26 02:08:40 +0000352 p+=distance;
353 indexes+=distance;
cristyf2bf2c72010-01-25 19:54:15 +0000354 for (x=0; x < (long) image->columns; x++)
355 {
356 for (i=0; i < 4; i++)
357 {
cristy7e9726d2010-01-26 02:08:40 +0000358 switch (i)
359 {
360 case 0:
cristy549a37e2010-01-26 15:24:15 +0000361 default:
cristy7e9726d2010-01-26 02:08:40 +0000362 {
363 /*
cristy7396d882010-01-27 02:37:56 +0000364 Horizontal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000365 */
366 offset=(ssize_t) distance;
367 break;
368 }
369 case 1:
370 {
371 /*
cristy7396d882010-01-27 02:37:56 +0000372 Vertical adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000373 */
cristy7396d882010-01-27 02:37:56 +0000374 offset=(ssize_t) (image->columns+2*distance);
cristy7e9726d2010-01-26 02:08:40 +0000375 break;
376 }
377 case 2:
378 {
379 /*
cristy7396d882010-01-27 02:37:56 +0000380 Right diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000381 */
cristy7396d882010-01-27 02:37:56 +0000382 offset=(ssize_t) (image->columns+2*distance)-distance;
cristy7e9726d2010-01-26 02:08:40 +0000383 break;
384 }
385 case 3:
386 {
387 /*
cristy7396d882010-01-27 02:37:56 +0000388 Left diagonal adjacency.
cristy7e9726d2010-01-26 02:08:40 +0000389 */
390 offset=(ssize_t) (image->columns+2*distance)+distance;
391 break;
392 }
393 }
394 u=0;
395 v=0;
cristy7396d882010-01-27 02:37:56 +0000396 while (grays[u].red != ScaleQuantumToMap(p->red))
cristy7e9726d2010-01-26 02:08:40 +0000397 u++;
cristy7396d882010-01-27 02:37:56 +0000398 while (grays[v].red != ScaleQuantumToMap((p+offset)->red))
cristy7e9726d2010-01-26 02:08:40 +0000399 v++;
cristy7396d882010-01-27 02:37:56 +0000400 cooccurrence[u][v].direction[i].red++;
401 cooccurrence[v][u].direction[i].red++;
cristy7e9726d2010-01-26 02:08:40 +0000402 u=0;
403 v=0;
cristy7396d882010-01-27 02:37:56 +0000404 while (grays[u].green != ScaleQuantumToMap(p->green))
cristy7e9726d2010-01-26 02:08:40 +0000405 u++;
cristy7396d882010-01-27 02:37:56 +0000406 while (grays[v].green != ScaleQuantumToMap((p+offset)->green))
cristy7e9726d2010-01-26 02:08:40 +0000407 v++;
cristy7396d882010-01-27 02:37:56 +0000408 cooccurrence[u][v].direction[i].green++;
409 cooccurrence[v][u].direction[i].green++;
cristy7e9726d2010-01-26 02:08:40 +0000410 u=0;
411 v=0;
cristy7396d882010-01-27 02:37:56 +0000412 while (grays[u].blue != ScaleQuantumToMap(p->blue))
cristy7e9726d2010-01-26 02:08:40 +0000413 u++;
cristy7396d882010-01-27 02:37:56 +0000414 while (grays[v].blue != ScaleQuantumToMap((p+offset)->blue))
cristy7e9726d2010-01-26 02:08:40 +0000415 v++;
cristy7396d882010-01-27 02:37:56 +0000416 cooccurrence[u][v].direction[i].blue++;
417 cooccurrence[v][u].direction[i].blue++;
cristy7e9726d2010-01-26 02:08:40 +0000418 if (image->matte != MagickFalse)
419 {
420 u=0;
421 v=0;
cristy7396d882010-01-27 02:37:56 +0000422 while (grays[u].opacity != ScaleQuantumToMap(p->opacity))
cristy7e9726d2010-01-26 02:08:40 +0000423 u++;
cristy7396d882010-01-27 02:37:56 +0000424 while (grays[v].opacity != ScaleQuantumToMap((p+offset)->opacity))
cristy7e9726d2010-01-26 02:08:40 +0000425 v++;
cristy7396d882010-01-27 02:37:56 +0000426 cooccurrence[u][v].direction[i].opacity++;
427 cooccurrence[v][u].direction[i].opacity++;
cristy7e9726d2010-01-26 02:08:40 +0000428 }
429 if (image->colorspace == CMYKColorspace)
430 {
431 u=0;
432 v=0;
cristy7396d882010-01-27 02:37:56 +0000433 while (grays[u].index != ScaleQuantumToMap(indexes[x]))
cristy7e9726d2010-01-26 02:08:40 +0000434 u++;
cristy7396d882010-01-27 02:37:56 +0000435 while (grays[v].index != ScaleQuantumToMap(indexes[x+offset]))
cristy7e9726d2010-01-26 02:08:40 +0000436 v++;
cristy7396d882010-01-27 02:37:56 +0000437 cooccurrence[u][v].direction[i].index++;
438 cooccurrence[v][u].direction[i].index++;
cristy7e9726d2010-01-26 02:08:40 +0000439 }
cristyf2bf2c72010-01-25 19:54:15 +0000440 }
cristy7e9726d2010-01-26 02:08:40 +0000441 p++;
cristyf2bf2c72010-01-25 19:54:15 +0000442 }
443 }
444 image_view=DestroyCacheView(image_view);
445 if (status == MagickFalse)
446 {
447 (void) ThrowMagickException(exception,GetMagickModule(),
448 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
cristy7396d882010-01-27 02:37:56 +0000449 for (i=0; i < (long) number_grays; i++)
450 cooccurrence[i]=(ChannelStatistics *)
451 RelinquishMagickMemory(cooccurrence[i]);
452 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
453 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristyf2bf2c72010-01-25 19:54:15 +0000454 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
455 channel_features);
456 return(channel_features);
457 }
458 /*
cristy7e9726d2010-01-26 02:08:40 +0000459 Normalize spatial dependence matrix.
460 */
461#if defined(MAGICKCORE_OPENMP_SUPPORT)
462 #pragma omp parallel for schedule(dynamic,4) shared(status)
463#endif
cristybd822072010-01-27 00:30:00 +0000464 for (i=0; i < 4; i++)
cristy7e9726d2010-01-26 02:08:40 +0000465 {
cristy549a37e2010-01-26 15:24:15 +0000466 double
467 normalize;
468
cristybd822072010-01-27 00:30:00 +0000469 switch (i)
cristy7e9726d2010-01-26 02:08:40 +0000470 {
cristybd822072010-01-27 00:30:00 +0000471 case 0:
472 default:
cristy7e9726d2010-01-26 02:08:40 +0000473 {
cristybd822072010-01-27 00:30:00 +0000474 /*
cristy7396d882010-01-27 02:37:56 +0000475 Horizontal adjacency.
cristybd822072010-01-27 00:30:00 +0000476 */
477 normalize=2.0*image->rows*(image->columns-distance);
478 break;
479 }
480 case 1:
481 {
482 /*
cristy7396d882010-01-27 02:37:56 +0000483 Vertical adjacency.
cristybd822072010-01-27 00:30:00 +0000484 */
cristy7396d882010-01-27 02:37:56 +0000485 normalize=2.0*(image->rows-distance)*image->columns;
cristybd822072010-01-27 00:30:00 +0000486 break;
487 }
488 case 2:
489 {
490 /*
cristy7396d882010-01-27 02:37:56 +0000491 Right diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000492 */
cristy7396d882010-01-27 02:37:56 +0000493 normalize=2.0*(image->rows-distance)*(image->columns-distance);
cristybd822072010-01-27 00:30:00 +0000494 break;
495 }
496 case 3:
497 {
498 /*
cristy7396d882010-01-27 02:37:56 +0000499 Left diagonal adjacency.
cristybd822072010-01-27 00:30:00 +0000500 */
501 normalize=2.0*(image->rows-distance)*(image->columns-distance);
502 break;
503 }
504 }
cristy7396d882010-01-27 02:37:56 +0000505 for (y=0; y < (long) number_grays; y++)
cristybd822072010-01-27 00:30:00 +0000506 {
507 register long
508 x;
509
cristy7396d882010-01-27 02:37:56 +0000510 for (x=0; x < (long) number_grays; x++)
cristybd822072010-01-27 00:30:00 +0000511 {
cristy7396d882010-01-27 02:37:56 +0000512 cooccurrence[x][y].direction[i].red/=normalize;
513 cooccurrence[x][y].direction[i].green/=normalize;
514 cooccurrence[x][y].direction[i].blue/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000515 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000516 cooccurrence[x][y].direction[i].opacity/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000517 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000518 cooccurrence[x][y].direction[i].index/=normalize;
cristy549a37e2010-01-26 15:24:15 +0000519 }
520 }
521 }
522 /*
523 Compute texture features.
524 */
cristy7396d882010-01-27 02:37:56 +0000525 sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum));
526 if (sum == (ChannelStatistics *) NULL)
527 {
528 (void) ThrowMagickException(exception,GetMagickModule(),
529 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
530 for (i=0; i < (long) number_grays; i++)
531 cooccurrence[i]=(ChannelStatistics *)
532 RelinquishMagickMemory(cooccurrence[i]);
533 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
534 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
535 channel_features=(ChannelFeatures *) RelinquishMagickMemory(
536 channel_features);
537 return(channel_features);
538 }
539 (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum));
540 (void) ResetMagickMemory(&correlation,0,sizeof(correlation));
541 (void) ResetMagickMemory(&mean,0,sizeof(mean));
542 (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares));
cristy3a82f252010-01-26 20:31:51 +0000543#if defined(MAGICKCORE_OPENMP_SUPPORT)
544 #pragma omp parallel for schedule(dynamic,4) shared(status)
545#endif
cristybd822072010-01-27 00:30:00 +0000546 for (i=0; i < 4; i++)
cristy549a37e2010-01-26 15:24:15 +0000547 {
548 register long
cristybd822072010-01-27 00:30:00 +0000549 y;
cristy549a37e2010-01-26 15:24:15 +0000550
cristy7396d882010-01-27 02:37:56 +0000551 for (y=0; y < (long) number_grays; y++)
cristy549a37e2010-01-26 15:24:15 +0000552 {
cristybd822072010-01-27 00:30:00 +0000553 register long
554 x;
555
cristy7396d882010-01-27 02:37:56 +0000556 for (x=0; x < (long) number_grays; x++)
cristy549a37e2010-01-26 15:24:15 +0000557 {
558 /*
cristy3a82f252010-01-26 20:31:51 +0000559 Angular second moment: measure of homogeneity of the image.
cristy549a37e2010-01-26 15:24:15 +0000560 */
561 channel_features[RedChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000562 cooccurrence[x][y].direction[i].red*
563 cooccurrence[x][y].direction[i].red;
cristy549a37e2010-01-26 15:24:15 +0000564 channel_features[GreenChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000565 cooccurrence[x][y].direction[i].green*
566 cooccurrence[x][y].direction[i].green;
cristy549a37e2010-01-26 15:24:15 +0000567 channel_features[BlueChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000568 cooccurrence[x][y].direction[i].blue*
569 cooccurrence[x][y].direction[i].blue;
cristy549a37e2010-01-26 15:24:15 +0000570 if (image->matte != MagickFalse)
571 channel_features[OpacityChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000572 cooccurrence[x][y].direction[i].opacity*
573 cooccurrence[x][y].direction[i].opacity;
cristy549a37e2010-01-26 15:24:15 +0000574 if (image->colorspace == CMYKColorspace)
cristy3a82f252010-01-26 20:31:51 +0000575 channel_features[BlackChannel].angular_second_moment[i]+=
cristy7396d882010-01-27 02:37:56 +0000576 cooccurrence[x][y].direction[i].index*
577 cooccurrence[x][y].direction[i].index;
578 /*
579 Correlation: measure of linear-dependencies in the image.
580 */
581 sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000582 sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green;
cristycdf8e1b2010-01-28 01:52:33 +0000583 sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue;
584 if (image->matte != MagickFalse)
585 sum[y].direction[i].opacity+=cooccurrence[x][y].direction[i].opacity;
586 if (image->colorspace == CMYKColorspace)
587 sum[y].direction[i].index+=cooccurrence[x][y].direction[i].index;
588 correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red;
cristy7396d882010-01-27 02:37:56 +0000589 correlation.direction[i].green+=x*y*
590 cooccurrence[x][y].direction[i].green;
cristy7396d882010-01-27 02:37:56 +0000591 correlation.direction[i].blue+=x*y*
592 cooccurrence[x][y].direction[i].blue;
cristy7396d882010-01-27 02:37:56 +0000593 if (image->matte != MagickFalse)
cristycdf8e1b2010-01-28 01:52:33 +0000594 correlation.direction[i].opacity+=x*y*
595 cooccurrence[x][y].direction[i].opacity;
cristy7396d882010-01-27 02:37:56 +0000596 if (image->colorspace == CMYKColorspace)
cristycdf8e1b2010-01-28 01:52:33 +0000597 correlation.direction[i].index+=x*y*
598 cooccurrence[x][y].direction[i].index;
cristycf5e6492010-01-28 02:45:28 +0000599 /*
600 Inverse Difference Moment.
601 */
cristy9fc6dbf2010-01-28 02:49:50 +0000602 channel_features[RedChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000603 cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000604 channel_features[GreenChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000605 cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1);
cristy9fc6dbf2010-01-28 02:49:50 +0000606 channel_features[BlueChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000607 cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1);
608 if (image->matte != MagickFalse)
cristy9fc6dbf2010-01-28 02:49:50 +0000609 channel_features[OpacityChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000610 cooccurrence[x][y].direction[i].opacity/((y-x)*(y-x)+1);
611 if (image->colorspace == CMYKColorspace)
cristy9fc6dbf2010-01-28 02:49:50 +0000612 channel_features[IndexChannel].inverse_difference_moment[i]+=
cristycf5e6492010-01-28 02:45:28 +0000613 cooccurrence[x][y].direction[i].index/((y-x)*(y-x)+1);
cristy7e9726d2010-01-26 02:08:40 +0000614 }
cristycdf8e1b2010-01-28 01:52:33 +0000615 mean.direction[i].red+=y*sum[y].direction[i].red;
616 sum_squares.direction[i].red+=y*y*sum[y].direction[i].red;
617 mean.direction[i].green+=y*sum[y].direction[i].green;
618 sum_squares.direction[i].green+=y*y*sum[y].direction[i].green;
619 mean.direction[i].blue+=y*sum[y].direction[i].blue;
620 sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue;
621 if (image->matte != MagickFalse)
622 {
623 mean.direction[i].opacity+=y*sum[y].direction[i].opacity;
624 sum_squares.direction[i].opacity+=y*y*sum[y].direction[i].opacity;
625 }
626 if (image->colorspace == CMYKColorspace)
627 {
628 mean.direction[i].index+=y*sum[y].direction[i].index;
629 sum_squares.direction[i].index+=y*y*sum[y].direction[i].index;
630 }
cristy7e9726d2010-01-26 02:08:40 +0000631 }
cristycdf8e1b2010-01-28 01:52:33 +0000632 /*
633 Correlation: measure of linear-dependencies in the image.
634 */
635 channel_features[RedChannel].correlation[i]=
636 (correlation.direction[i].red-mean.direction[i].red*
637 mean.direction[i].red)/(sqrt(sum_squares.direction[i].red-
638 (mean.direction[i].red*mean.direction[i].red))*sqrt(
639 sum_squares.direction[i].red-(mean.direction[i].red*
640 mean.direction[i].red)));
641 channel_features[GreenChannel].correlation[i]=
642 (correlation.direction[i].green-mean.direction[i].green*
643 mean.direction[i].green)/(sqrt(sum_squares.direction[i].green-
644 (mean.direction[i].green*mean.direction[i].green))*sqrt(
645 sum_squares.direction[i].green-(mean.direction[i].green*
646 mean.direction[i].green)));
647 channel_features[BlueChannel].correlation[i]=
648 (correlation.direction[i].blue-mean.direction[i].blue*
649 mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue-
650 (mean.direction[i].blue*mean.direction[i].blue))*sqrt(
651 sum_squares.direction[i].blue-(mean.direction[i].blue*
652 mean.direction[i].blue)));
653 if (image->matte != MagickFalse)
654 channel_features[OpacityChannel].correlation[i]=
655 (correlation.direction[i].opacity-mean.direction[i].opacity*
656 mean.direction[i].opacity)/(sqrt(sum_squares.direction[i].opacity-
657 (mean.direction[i].opacity*mean.direction[i].opacity))*sqrt(
658 sum_squares.direction[i].opacity-(mean.direction[i].opacity*
659 mean.direction[i].opacity)));
660 if (image->colorspace == CMYKColorspace)
661 channel_features[IndexChannel].correlation[i]=
662 (correlation.direction[i].index-mean.direction[i].index*
663 mean.direction[i].index)/(sqrt(sum_squares.direction[i].index-
664 (mean.direction[i].index*mean.direction[i].index))*sqrt(
665 sum_squares.direction[i].index-(mean.direction[i].index*
666 mean.direction[i].index)));
cristy7e9726d2010-01-26 02:08:40 +0000667 }
cristycf5e6492010-01-28 02:45:28 +0000668 /*
669 Compute more texture features.
670 */
671 (void) ResetMagickMemory(&variance,0,sizeof(variance));
672#if defined(MAGICKCORE_OPENMP_SUPPORT)
673 #pragma omp parallel for schedule(dynamic,4) shared(status)
674#endif
675 for (i=0; i < 4; i++)
676 {
677 register long
678 y;
679
680 for (y=0; y < (long) number_grays; y++)
681 {
682 register long
683 x;
684
685 for (x=0; x < (long) number_grays; x++)
686 {
687 /*
688 Sum of Squares: Variance
689 */
690 variance.direction[i].red+=(y-mean.direction[i].red+1)*
691 (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red;
692 variance.direction[i].green+=(y-mean.direction[i].green+1)*
693 (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green;
694 variance.direction[i].blue+=(y-mean.direction[i].blue+1)*
695 (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue;
696 if (image->matte != MagickFalse)
697 variance.direction[i].opacity+=(y-mean.direction[i].opacity+1)*
698 (y-mean.direction[i].opacity+1)*
699 cooccurrence[x][y].direction[i].opacity;
700 if (image->colorspace == CMYKColorspace)
701 variance.direction[i].index+=(y-mean.direction[i].index+1)*
702 (y-mean.direction[i].index+1)*cooccurrence[x][y].direction[i].index;
703 }
704 }
705 channel_features[RedChannel].variance_sum_of_squares[i]=
706 variance.direction[i].red;
707 channel_features[GreenChannel].variance_sum_of_squares[i]=
708 variance.direction[i].green;
709 channel_features[BlueChannel].variance_sum_of_squares[i]=
710 variance.direction[i].blue;
711 if (image->matte != MagickFalse)
712 channel_features[RedChannel].variance_sum_of_squares[i]=
713 variance.direction[i].opacity;
714 if (image->colorspace == CMYKColorspace)
715 channel_features[RedChannel].variance_sum_of_squares[i]=
716 variance.direction[i].index;
717 }
718 /*
719 Compute more texture features.
720 */
cristy3a82f252010-01-26 20:31:51 +0000721#if defined(MAGICKCORE_OPENMP_SUPPORT)
722 #pragma omp parallel for schedule(dynamic,4) shared(status)
723#endif
cristybd822072010-01-27 00:30:00 +0000724 for (i=0; i < 4; i++)
cristy3a82f252010-01-26 20:31:51 +0000725 {
cristy7396d882010-01-27 02:37:56 +0000726 for (z=0; z < (long) number_grays; z++)
cristy3a82f252010-01-26 20:31:51 +0000727 {
728 register long
cristybd822072010-01-27 00:30:00 +0000729 y;
cristy3a82f252010-01-26 20:31:51 +0000730
cristy7396d882010-01-27 02:37:56 +0000731 ChannelStatistics
cristybd822072010-01-27 00:30:00 +0000732 pixel;
733
734 (void) ResetMagickMemory(&pixel,0,sizeof(pixel));
cristy7396d882010-01-27 02:37:56 +0000735 for (y=0; y < (long) number_grays; y++)
cristy3a82f252010-01-26 20:31:51 +0000736 {
cristybd822072010-01-27 00:30:00 +0000737 register long
738 x;
739
cristy7396d882010-01-27 02:37:56 +0000740 for (x=0; x < (long) number_grays; x++)
cristy3a82f252010-01-26 20:31:51 +0000741 {
742 /*
743 Contrast: amount of local variations present in an image.
744 */
745 if (((y-x) == z) || ((x-y) == z))
746 {
cristy7396d882010-01-27 02:37:56 +0000747 pixel.direction[i].red+=cooccurrence[x][y].direction[i].red;
748 pixel.direction[i].green+=cooccurrence[x][y].direction[i].green;
749 pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue;
cristy3a82f252010-01-26 20:31:51 +0000750 if (image->matte != MagickFalse)
cristy7396d882010-01-27 02:37:56 +0000751 pixel.direction[i].opacity+=
752 cooccurrence[x][y].direction[i].opacity;
cristy3a82f252010-01-26 20:31:51 +0000753 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000754 pixel.direction[i].index+=cooccurrence[x][y].direction[i].index;
cristy3a82f252010-01-26 20:31:51 +0000755 }
756 }
757 }
cristy7396d882010-01-27 02:37:56 +0000758 channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red;
759 channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green;
760 channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue;
cristy3a82f252010-01-26 20:31:51 +0000761 if (image->matte != MagickFalse)
762 channel_features[OpacityChannel].contrast[i]+=z*z*
cristy7396d882010-01-27 02:37:56 +0000763 pixel.direction[i].opacity;
cristy3a82f252010-01-26 20:31:51 +0000764 if (image->colorspace == CMYKColorspace)
cristy7396d882010-01-27 02:37:56 +0000765 channel_features[BlackChannel].contrast[i]+=z*z*
766 pixel.direction[i].index;
cristy3a82f252010-01-26 20:31:51 +0000767 }
768 }
cristy7e9726d2010-01-26 02:08:40 +0000769 /*
cristyf2bf2c72010-01-25 19:54:15 +0000770 Relinquish resources.
771 */
cristy7396d882010-01-27 02:37:56 +0000772 sum=(ChannelStatistics *) RelinquishMagickMemory(sum);
773 for (i=0; i < (long) number_grays; i++)
774 cooccurrence[i]=(ChannelStatistics *)
775 RelinquishMagickMemory(cooccurrence[i]);
776 cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence);
777 grays=(LongPixelPacket *) RelinquishMagickMemory(grays);
cristy3e2860c2010-01-24 01:36:30 +0000778 return(channel_features);
779}