cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % FFFFF EEEEE AAA TTTTT U U RRRR EEEEE % |
| 7 | % F E A A T U U R R E % |
| 8 | % FFF EEE AAAAA T U U RRRR EEE % |
| 9 | % F E A A T U U R R E % |
| 10 | % F EEEEE A A T UUU R R EEEEE % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Image Feature Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % 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 | % |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 103 | % 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: |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 110 | % |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 111 | % channel_features=GetImageChannelFeatures(image,1,excepton); |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 112 | % contrast=channel_features[RedChannel].contrast[0]; |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 113 | % |
| 114 | % Use MagickRelinquishMemory() to free the features buffer. |
| 115 | % |
| 116 | % The format of the GetImageChannelFeatures method is: |
| 117 | % |
| 118 | % ChannelFeatures *GetImageChannelFeatures(const Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 119 | % const size_t distance,ExceptionInfo *exception) |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 120 | % |
| 121 | % A description of each parameter follows: |
| 122 | % |
| 123 | % o image: the image. |
| 124 | % |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 125 | % o distance: the distance. |
| 126 | % |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 127 | % o exception: return any errors or warnings in this structure. |
| 128 | % |
| 129 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 130 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 131 | static inline ssize_t MagickAbsoluteValue(const ssize_t x) |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 132 | { |
| 133 | if (x < 0) |
| 134 | return(-x); |
| 135 | return(x); |
| 136 | } |
| 137 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 138 | MagickExport ChannelFeatures *GetImageChannelFeatures(const Image *image, |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 139 | const size_t distance,ExceptionInfo *exception) |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 140 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 141 | typedef struct _ChannelStatistics |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 142 | { |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 143 | DoublePixelPacket |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 144 | direction[4]; /* horizontal, vertical, left and right diagonals */ |
| 145 | } ChannelStatistics; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 146 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 147 | CacheView |
| 148 | *image_view; |
| 149 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 150 | ChannelFeatures |
| 151 | *channel_features; |
| 152 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 153 | ChannelStatistics |
| 154 | **cooccurrence, |
| 155 | correlation, |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 156 | *density_x, |
| 157 | *density_xy, |
| 158 | *density_y, |
| 159 | entropy_x, |
| 160 | entropy_xy, |
| 161 | entropy_xy1, |
| 162 | entropy_xy2, |
| 163 | entropy_y, |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 164 | mean, |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 165 | **Q, |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 166 | *sum, |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 167 | sum_squares, |
| 168 | variance; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 169 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 170 | LongPixelPacket |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 171 | gray, |
| 172 | *grays; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 173 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 174 | ssize_t |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 175 | y, |
| 176 | z; |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 177 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 178 | MagickBooleanType |
| 179 | status; |
| 180 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 181 | register ssize_t |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 182 | i; |
| 183 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 184 | size_t |
| 185 | length; |
| 186 | |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 187 | unsigned int |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 188 | number_grays; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 189 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 190 | assert(image != (Image *) NULL); |
| 191 | assert(image->signature == MagickSignature); |
| 192 | if (image->debug != MagickFalse) |
| 193 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 194 | if ((image->columns < (distance+1)) || (image->rows < (distance+1))) |
| 195 | return((ChannelFeatures *) NULL); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 196 | length=AllChannels+1UL; |
| 197 | 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)); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 203 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 204 | Form grays. |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 205 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 206 | grays=(LongPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays)); |
| 207 | if (grays == (LongPixelPacket *) NULL) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 208 | { |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 209 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 210 | channel_features); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 211 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 212 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 213 | return(channel_features); |
| 214 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 215 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 216 | { |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 217 | grays[i].red=(~0U); |
| 218 | grays[i].green=(~0U); |
| 219 | grays[i].blue=(~0U); |
| 220 | grays[i].opacity=(~0U); |
| 221 | grays[i].index=(~0U); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 222 | } |
| 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 |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 228 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 229 | { |
| 230 | register const IndexPacket |
| 231 | *restrict indexes; |
| 232 | |
| 233 | register const PixelPacket |
| 234 | *restrict p; |
| 235 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 236 | register ssize_t |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 237 | x; |
| 238 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 239 | if (status == MagickFalse) |
| 240 | continue; |
| 241 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 242 | if (p == (const PixelPacket *) NULL) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 243 | { |
| 244 | status=MagickFalse; |
| 245 | continue; |
| 246 | } |
| 247 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 248 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 249 | { |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 250 | grays[ScaleQuantumToMap(p->red)].red= |
cristy | ca13f4c | 2010-05-29 23:39:09 +0000 | [diff] [blame] | 251 | ScaleQuantumToMap(p->red); |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 252 | grays[ScaleQuantumToMap(p->green)].green= |
cristy | ca13f4c | 2010-05-29 23:39:09 +0000 | [diff] [blame] | 253 | ScaleQuantumToMap(p->green); |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 254 | grays[ScaleQuantumToMap(p->blue)].blue= |
cristy | ca13f4c | 2010-05-29 23:39:09 +0000 | [diff] [blame] | 255 | ScaleQuantumToMap(p->blue); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 256 | if (image->matte != MagickFalse) |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 257 | grays[ScaleQuantumToMap(p->opacity)].opacity= |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 258 | ScaleQuantumToMap(p->opacity); |
| 259 | if (image->colorspace == CMYKColorspace) |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 260 | grays[ScaleQuantumToMap(indexes[x])].index= |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 261 | ScaleQuantumToMap(indexes[x]); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 262 | p++; |
| 263 | } |
| 264 | } |
cristy | 30c510a | 2010-01-24 03:43:00 +0000 | [diff] [blame] | 265 | image_view=DestroyCacheView(image_view); |
| 266 | if (status == MagickFalse) |
| 267 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 268 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | 30c510a | 2010-01-24 03:43:00 +0000 | [diff] [blame] | 269 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 270 | channel_features); |
| 271 | return(channel_features); |
| 272 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 273 | (void) ResetMagickMemory(&gray,0,sizeof(gray)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 274 | for (i=0; i <= (ssize_t) MaxMap; i++) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 275 | { |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 276 | if (grays[i].red != ~0U) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 277 | grays[gray.red++].red=grays[i].red; |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 278 | if (grays[i].green != ~0U) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 279 | grays[gray.green++].green=grays[i].green; |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 280 | if (grays[i].blue != ~0U) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 281 | grays[gray.blue++].blue=grays[i].blue; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 282 | if (image->matte != MagickFalse) |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 283 | if (grays[i].opacity != ~0U) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 284 | grays[gray.opacity++].opacity=grays[i].opacity; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 285 | if (image->colorspace == CMYKColorspace) |
cristy | ecd0ab5 | 2010-05-30 14:59:20 +0000 | [diff] [blame^] | 286 | if (grays[i].index != ~0U) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 287 | grays[gray.index++].index=grays[i].index; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 288 | } |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 289 | /* |
| 290 | Allocate spatial dependence matrix. |
| 291 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 292 | number_grays=gray.red; |
| 293 | if (gray.green > number_grays) |
| 294 | number_grays=gray.green; |
| 295 | if (gray.blue > number_grays) |
| 296 | number_grays=gray.blue; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 297 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 298 | if (gray.opacity > number_grays) |
| 299 | number_grays=gray.opacity; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 300 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 301 | if (gray.index > number_grays) |
| 302 | number_grays=gray.index; |
| 303 | cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays, |
| 304 | sizeof(*cooccurrence)); |
cristy | 77173e5 | 2010-02-02 02:51:35 +0000 | [diff] [blame] | 305 | density_x=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1), |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 306 | sizeof(*density_x)); |
| 307 | density_xy=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1), |
| 308 | sizeof(*density_xy)); |
cristy | 77173e5 | 2010-02-02 02:51:35 +0000 | [diff] [blame] | 309 | density_y=(ChannelStatistics *) AcquireQuantumMemory(2*(number_grays+1), |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 310 | sizeof(*density_y)); |
| 311 | Q=(ChannelStatistics **) AcquireQuantumMemory(number_grays,sizeof(*Q)); |
| 312 | sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum)); |
| 313 | if ((cooccurrence == (ChannelStatistics **) NULL) || |
| 314 | (density_x == (ChannelStatistics *) NULL) || |
| 315 | (density_xy == (ChannelStatistics *) NULL) || |
| 316 | (density_y == (ChannelStatistics *) NULL) || |
| 317 | (Q == (ChannelStatistics **) NULL) || |
| 318 | (sum == (ChannelStatistics *) NULL)) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 319 | { |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 320 | if (Q != (ChannelStatistics **) NULL) |
| 321 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 322 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 323 | Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]); |
| 324 | Q=(ChannelStatistics **) RelinquishMagickMemory(Q); |
| 325 | } |
| 326 | if (sum != (ChannelStatistics *) NULL) |
| 327 | sum=(ChannelStatistics *) RelinquishMagickMemory(sum); |
| 328 | if (density_y != (ChannelStatistics *) NULL) |
| 329 | density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y); |
| 330 | if (density_xy != (ChannelStatistics *) NULL) |
| 331 | density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy); |
| 332 | if (density_x != (ChannelStatistics *) NULL) |
| 333 | density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x); |
| 334 | if (cooccurrence != (ChannelStatistics **) NULL) |
| 335 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 336 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 337 | cooccurrence[i]=(ChannelStatistics *) |
| 338 | RelinquishMagickMemory(cooccurrence[i]); |
| 339 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory( |
| 340 | cooccurrence); |
| 341 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 342 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 343 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 344 | channel_features); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 345 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 346 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 347 | return(channel_features); |
| 348 | } |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 349 | (void) ResetMagickMemory(&correlation,0,sizeof(correlation)); |
cristy | 77173e5 | 2010-02-02 02:51:35 +0000 | [diff] [blame] | 350 | (void) ResetMagickMemory(density_x,0,2*(number_grays+1)*sizeof(*density_x)); |
| 351 | (void) ResetMagickMemory(density_xy,0,2*(number_grays+1)*sizeof(*density_xy)); |
| 352 | (void) ResetMagickMemory(density_y,0,2*(number_grays+1)*sizeof(*density_y)); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 353 | (void) ResetMagickMemory(&mean,0,sizeof(mean)); |
| 354 | (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum)); |
| 355 | (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares)); |
| 356 | (void) ResetMagickMemory(density_xy,0,2*number_grays*sizeof(*density_xy)); |
| 357 | (void) ResetMagickMemory(&entropy_x,0,sizeof(entropy_x)); |
| 358 | (void) ResetMagickMemory(&entropy_xy,0,sizeof(entropy_xy)); |
| 359 | (void) ResetMagickMemory(&entropy_xy1,0,sizeof(entropy_xy1)); |
| 360 | (void) ResetMagickMemory(&entropy_xy2,0,sizeof(entropy_xy2)); |
| 361 | (void) ResetMagickMemory(&entropy_y,0,sizeof(entropy_y)); |
| 362 | (void) ResetMagickMemory(&variance,0,sizeof(variance)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 363 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 364 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 365 | cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays, |
| 366 | sizeof(**cooccurrence)); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 367 | Q[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(**Q)); |
| 368 | if ((cooccurrence[i] == (ChannelStatistics *) NULL) || |
| 369 | (Q[i] == (ChannelStatistics *) NULL)) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 370 | break; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 371 | (void) ResetMagickMemory(cooccurrence[i],0,number_grays* |
cristy | 3749be4 | 2010-02-02 02:46:51 +0000 | [diff] [blame] | 372 | sizeof(**cooccurrence)); |
| 373 | (void) ResetMagickMemory(Q[i],0,number_grays*sizeof(**Q)); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 374 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 375 | if (i < (ssize_t) number_grays) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 376 | { |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 377 | for (i--; i >= 0; i--) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 378 | { |
| 379 | if (Q[i] != (ChannelStatistics *) NULL) |
| 380 | Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]); |
| 381 | if (cooccurrence[i] != (ChannelStatistics *) NULL) |
| 382 | cooccurrence[i]=(ChannelStatistics *) |
| 383 | RelinquishMagickMemory(cooccurrence[i]); |
| 384 | } |
| 385 | Q=(ChannelStatistics **) RelinquishMagickMemory(Q); |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 386 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 387 | sum=(ChannelStatistics *) RelinquishMagickMemory(sum); |
| 388 | density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y); |
| 389 | density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy); |
| 390 | density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x); |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 391 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 392 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 393 | channel_features); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 394 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 395 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 396 | return(channel_features); |
| 397 | } |
| 398 | /* |
| 399 | Initialize spatial dependence matrix. |
| 400 | */ |
| 401 | status=MagickTrue; |
| 402 | image_view=AcquireCacheView(image); |
| 403 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 404 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 405 | #endif |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 406 | for (y=0; y < (ssize_t) image->rows; y++) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 407 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 408 | ssize_t |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 409 | u, |
| 410 | v; |
| 411 | |
| 412 | register const IndexPacket |
| 413 | *restrict indexes; |
| 414 | |
| 415 | register const PixelPacket |
| 416 | *restrict p; |
| 417 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 418 | register ssize_t |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 419 | x; |
| 420 | |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 421 | ssize_t |
| 422 | offset; |
| 423 | |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 424 | if (status == MagickFalse) |
| 425 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 426 | p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+ |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 427 | 2*distance,distance+1,exception); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 428 | if (p == (const PixelPacket *) NULL) |
| 429 | { |
| 430 | status=MagickFalse; |
| 431 | continue; |
| 432 | } |
| 433 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 434 | p+=distance; |
| 435 | indexes+=distance; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 436 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 437 | { |
| 438 | for (i=0; i < 4; i++) |
| 439 | { |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 440 | switch (i) |
| 441 | { |
| 442 | case 0: |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 443 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 444 | { |
| 445 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 446 | Horizontal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 447 | */ |
| 448 | offset=(ssize_t) distance; |
| 449 | break; |
| 450 | } |
| 451 | case 1: |
| 452 | { |
| 453 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 454 | Vertical adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 455 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 456 | offset=(ssize_t) (image->columns+2*distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 457 | break; |
| 458 | } |
| 459 | case 2: |
| 460 | { |
| 461 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 462 | Right diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 463 | */ |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 464 | offset=(ssize_t) ((image->columns+2*distance)-distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 465 | break; |
| 466 | } |
| 467 | case 3: |
| 468 | { |
| 469 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 470 | Left diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 471 | */ |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 472 | offset=(ssize_t) ((image->columns+2*distance)+distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
| 475 | } |
| 476 | u=0; |
| 477 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 478 | while (grays[u].red != ScaleQuantumToMap(p->red)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 479 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 480 | while (grays[v].red != ScaleQuantumToMap((p+offset)->red)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 481 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 482 | cooccurrence[u][v].direction[i].red++; |
| 483 | cooccurrence[v][u].direction[i].red++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 484 | u=0; |
| 485 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 486 | while (grays[u].green != ScaleQuantumToMap(p->green)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 487 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 488 | while (grays[v].green != ScaleQuantumToMap((p+offset)->green)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 489 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 490 | cooccurrence[u][v].direction[i].green++; |
| 491 | cooccurrence[v][u].direction[i].green++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 492 | u=0; |
| 493 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 494 | while (grays[u].blue != ScaleQuantumToMap(p->blue)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 495 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 496 | while (grays[v].blue != ScaleQuantumToMap((p+offset)->blue)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 497 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 498 | cooccurrence[u][v].direction[i].blue++; |
| 499 | cooccurrence[v][u].direction[i].blue++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 500 | if (image->matte != MagickFalse) |
| 501 | { |
| 502 | u=0; |
| 503 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 504 | while (grays[u].opacity != ScaleQuantumToMap(p->opacity)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 505 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 506 | while (grays[v].opacity != ScaleQuantumToMap((p+offset)->opacity)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 507 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 508 | cooccurrence[u][v].direction[i].opacity++; |
| 509 | cooccurrence[v][u].direction[i].opacity++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 510 | } |
| 511 | if (image->colorspace == CMYKColorspace) |
| 512 | { |
| 513 | u=0; |
| 514 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 515 | while (grays[u].index != ScaleQuantumToMap(indexes[x])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 516 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 517 | while (grays[v].index != ScaleQuantumToMap(indexes[x+offset])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 518 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 519 | cooccurrence[u][v].direction[i].index++; |
| 520 | cooccurrence[v][u].direction[i].index++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 521 | } |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 522 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 523 | p++; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 524 | } |
| 525 | } |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 526 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 527 | image_view=DestroyCacheView(image_view); |
| 528 | if (status == MagickFalse) |
| 529 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 530 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 531 | cooccurrence[i]=(ChannelStatistics *) |
| 532 | RelinquishMagickMemory(cooccurrence[i]); |
| 533 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 534 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 535 | channel_features); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 536 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 537 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 538 | return(channel_features); |
| 539 | } |
| 540 | /* |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 541 | Normalize spatial dependence matrix. |
| 542 | */ |
| 543 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 544 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 545 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 546 | for (i=0; i < 4; i++) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 547 | { |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 548 | double |
| 549 | normalize; |
| 550 | |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 551 | switch (i) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 552 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 553 | case 0: |
| 554 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 555 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 556 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 557 | Horizontal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 558 | */ |
| 559 | normalize=2.0*image->rows*(image->columns-distance); |
| 560 | break; |
| 561 | } |
| 562 | case 1: |
| 563 | { |
| 564 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 565 | Vertical adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 566 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 567 | normalize=2.0*(image->rows-distance)*image->columns; |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 568 | break; |
| 569 | } |
| 570 | case 2: |
| 571 | { |
| 572 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 573 | Right diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 574 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 575 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 576 | break; |
| 577 | } |
| 578 | case 3: |
| 579 | { |
| 580 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 581 | Left diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 582 | */ |
| 583 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
| 584 | break; |
| 585 | } |
| 586 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 587 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 588 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 589 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 590 | x; |
| 591 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 592 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 593 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 594 | cooccurrence[x][y].direction[i].red/=normalize; |
| 595 | cooccurrence[x][y].direction[i].green/=normalize; |
| 596 | cooccurrence[x][y].direction[i].blue/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 597 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 598 | cooccurrence[x][y].direction[i].opacity/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 599 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 600 | cooccurrence[x][y].direction[i].index/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | } |
| 604 | /* |
| 605 | Compute texture features. |
| 606 | */ |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 607 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 608 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 609 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 610 | for (i=0; i < 4; i++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 611 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 612 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 613 | y; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 614 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 615 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 616 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 617 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 618 | x; |
| 619 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 620 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 621 | { |
| 622 | /* |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 623 | Angular second moment: measure of homogeneity of the image. |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 624 | */ |
| 625 | channel_features[RedChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 626 | cooccurrence[x][y].direction[i].red* |
| 627 | cooccurrence[x][y].direction[i].red; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 628 | channel_features[GreenChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 629 | cooccurrence[x][y].direction[i].green* |
| 630 | cooccurrence[x][y].direction[i].green; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 631 | channel_features[BlueChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 632 | cooccurrence[x][y].direction[i].blue* |
| 633 | cooccurrence[x][y].direction[i].blue; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 634 | if (image->matte != MagickFalse) |
| 635 | channel_features[OpacityChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 636 | cooccurrence[x][y].direction[i].opacity* |
| 637 | cooccurrence[x][y].direction[i].opacity; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 638 | if (image->colorspace == CMYKColorspace) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 639 | channel_features[BlackChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 640 | cooccurrence[x][y].direction[i].index* |
| 641 | cooccurrence[x][y].direction[i].index; |
| 642 | /* |
| 643 | Correlation: measure of linear-dependencies in the image. |
| 644 | */ |
| 645 | sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 646 | sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green; |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 647 | sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
| 648 | if (image->matte != MagickFalse) |
| 649 | sum[y].direction[i].opacity+=cooccurrence[x][y].direction[i].opacity; |
| 650 | if (image->colorspace == CMYKColorspace) |
| 651 | sum[y].direction[i].index+=cooccurrence[x][y].direction[i].index; |
| 652 | correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 653 | correlation.direction[i].green+=x*y* |
| 654 | cooccurrence[x][y].direction[i].green; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 655 | correlation.direction[i].blue+=x*y* |
| 656 | cooccurrence[x][y].direction[i].blue; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 657 | if (image->matte != MagickFalse) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 658 | correlation.direction[i].opacity+=x*y* |
| 659 | cooccurrence[x][y].direction[i].opacity; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 660 | if (image->colorspace == CMYKColorspace) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 661 | correlation.direction[i].index+=x*y* |
| 662 | cooccurrence[x][y].direction[i].index; |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 663 | /* |
| 664 | Inverse Difference Moment. |
| 665 | */ |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 666 | channel_features[RedChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 667 | cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 668 | channel_features[GreenChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 669 | cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 670 | channel_features[BlueChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 671 | cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1); |
| 672 | if (image->matte != MagickFalse) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 673 | channel_features[OpacityChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 674 | cooccurrence[x][y].direction[i].opacity/((y-x)*(y-x)+1); |
| 675 | if (image->colorspace == CMYKColorspace) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 676 | channel_features[IndexChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 677 | cooccurrence[x][y].direction[i].index/((y-x)*(y-x)+1); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 678 | /* |
| 679 | Sum average. |
| 680 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 681 | density_xy[y+x+2].direction[i].red+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 682 | cooccurrence[x][y].direction[i].red; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 683 | density_xy[y+x+2].direction[i].green+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 684 | cooccurrence[x][y].direction[i].green; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 685 | density_xy[y+x+2].direction[i].blue+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 686 | cooccurrence[x][y].direction[i].blue; |
| 687 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 688 | density_xy[y+x+2].direction[i].opacity+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 689 | cooccurrence[x][y].direction[i].opacity; |
| 690 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 691 | density_xy[y+x+2].direction[i].index+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 692 | cooccurrence[x][y].direction[i].index; |
| 693 | /* |
| 694 | Entropy. |
| 695 | */ |
| 696 | channel_features[RedChannel].entropy[i]-= |
| 697 | cooccurrence[x][y].direction[i].red* |
| 698 | log10(cooccurrence[x][y].direction[i].red+MagickEpsilon); |
| 699 | channel_features[GreenChannel].entropy[i]-= |
| 700 | cooccurrence[x][y].direction[i].green* |
| 701 | log10(cooccurrence[x][y].direction[i].green+MagickEpsilon); |
| 702 | channel_features[BlueChannel].entropy[i]-= |
| 703 | cooccurrence[x][y].direction[i].blue* |
| 704 | log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon); |
| 705 | if (image->matte != MagickFalse) |
| 706 | channel_features[OpacityChannel].entropy[i]-= |
| 707 | cooccurrence[x][y].direction[i].opacity* |
| 708 | log10(cooccurrence[x][y].direction[i].opacity+MagickEpsilon); |
| 709 | if (image->colorspace == CMYKColorspace) |
| 710 | channel_features[IndexChannel].entropy[i]-= |
| 711 | cooccurrence[x][y].direction[i].index* |
| 712 | log10(cooccurrence[x][y].direction[i].index+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 713 | /* |
| 714 | Information Measures of Correlation. |
| 715 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 716 | density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 717 | density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 718 | density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 719 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 720 | density_x[x].direction[i].opacity+= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 721 | cooccurrence[x][y].direction[i].opacity; |
| 722 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 723 | density_x[x].direction[i].index+= |
| 724 | cooccurrence[x][y].direction[i].index; |
| 725 | density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 726 | density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 727 | density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 728 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 729 | density_y[y].direction[i].opacity+= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 730 | cooccurrence[x][y].direction[i].opacity; |
| 731 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 732 | density_y[y].direction[i].index+= |
| 733 | cooccurrence[x][y].direction[i].index; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 734 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 735 | mean.direction[i].red+=y*sum[y].direction[i].red; |
| 736 | sum_squares.direction[i].red+=y*y*sum[y].direction[i].red; |
| 737 | mean.direction[i].green+=y*sum[y].direction[i].green; |
| 738 | sum_squares.direction[i].green+=y*y*sum[y].direction[i].green; |
| 739 | mean.direction[i].blue+=y*sum[y].direction[i].blue; |
| 740 | sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue; |
| 741 | if (image->matte != MagickFalse) |
| 742 | { |
| 743 | mean.direction[i].opacity+=y*sum[y].direction[i].opacity; |
| 744 | sum_squares.direction[i].opacity+=y*y*sum[y].direction[i].opacity; |
| 745 | } |
| 746 | if (image->colorspace == CMYKColorspace) |
| 747 | { |
| 748 | mean.direction[i].index+=y*sum[y].direction[i].index; |
| 749 | sum_squares.direction[i].index+=y*y*sum[y].direction[i].index; |
| 750 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 751 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 752 | /* |
| 753 | Correlation: measure of linear-dependencies in the image. |
| 754 | */ |
| 755 | channel_features[RedChannel].correlation[i]= |
| 756 | (correlation.direction[i].red-mean.direction[i].red* |
| 757 | mean.direction[i].red)/(sqrt(sum_squares.direction[i].red- |
| 758 | (mean.direction[i].red*mean.direction[i].red))*sqrt( |
| 759 | sum_squares.direction[i].red-(mean.direction[i].red* |
| 760 | mean.direction[i].red))); |
| 761 | channel_features[GreenChannel].correlation[i]= |
| 762 | (correlation.direction[i].green-mean.direction[i].green* |
| 763 | mean.direction[i].green)/(sqrt(sum_squares.direction[i].green- |
| 764 | (mean.direction[i].green*mean.direction[i].green))*sqrt( |
| 765 | sum_squares.direction[i].green-(mean.direction[i].green* |
| 766 | mean.direction[i].green))); |
| 767 | channel_features[BlueChannel].correlation[i]= |
| 768 | (correlation.direction[i].blue-mean.direction[i].blue* |
| 769 | mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue- |
| 770 | (mean.direction[i].blue*mean.direction[i].blue))*sqrt( |
| 771 | sum_squares.direction[i].blue-(mean.direction[i].blue* |
| 772 | mean.direction[i].blue))); |
| 773 | if (image->matte != MagickFalse) |
| 774 | channel_features[OpacityChannel].correlation[i]= |
| 775 | (correlation.direction[i].opacity-mean.direction[i].opacity* |
| 776 | mean.direction[i].opacity)/(sqrt(sum_squares.direction[i].opacity- |
| 777 | (mean.direction[i].opacity*mean.direction[i].opacity))*sqrt( |
| 778 | sum_squares.direction[i].opacity-(mean.direction[i].opacity* |
| 779 | mean.direction[i].opacity))); |
| 780 | if (image->colorspace == CMYKColorspace) |
| 781 | channel_features[IndexChannel].correlation[i]= |
| 782 | (correlation.direction[i].index-mean.direction[i].index* |
| 783 | mean.direction[i].index)/(sqrt(sum_squares.direction[i].index- |
| 784 | (mean.direction[i].index*mean.direction[i].index))*sqrt( |
| 785 | sum_squares.direction[i].index-(mean.direction[i].index* |
| 786 | mean.direction[i].index))); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 787 | } |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 788 | /* |
| 789 | Compute more texture features. |
| 790 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 791 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 792 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 793 | #endif |
| 794 | for (i=0; i < 4; i++) |
| 795 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 796 | register ssize_t |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 797 | x; |
| 798 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 799 | for (x=2; x < (ssize_t) (2*number_grays); x++) |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 800 | { |
| 801 | /* |
| 802 | Sum average. |
| 803 | */ |
| 804 | channel_features[RedChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 805 | x*density_xy[x].direction[i].red; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 806 | channel_features[GreenChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 807 | x*density_xy[x].direction[i].green; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 808 | channel_features[BlueChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 809 | x*density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 810 | if (image->matte != MagickFalse) |
| 811 | channel_features[OpacityChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 812 | x*density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 813 | if (image->colorspace == CMYKColorspace) |
| 814 | channel_features[IndexChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 815 | x*density_xy[x].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 816 | /* |
| 817 | Sum entropy. |
| 818 | */ |
| 819 | channel_features[RedChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 820 | density_xy[x].direction[i].red* |
| 821 | log10(density_xy[x].direction[i].red+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 822 | channel_features[GreenChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 823 | density_xy[x].direction[i].green* |
| 824 | log10(density_xy[x].direction[i].green+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 825 | channel_features[BlueChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 826 | density_xy[x].direction[i].blue* |
| 827 | log10(density_xy[x].direction[i].blue+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 828 | if (image->matte != MagickFalse) |
| 829 | channel_features[OpacityChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 830 | density_xy[x].direction[i].opacity* |
| 831 | log10(density_xy[x].direction[i].opacity+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 832 | if (image->colorspace == CMYKColorspace) |
| 833 | channel_features[IndexChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 834 | density_xy[x].direction[i].index* |
| 835 | log10(density_xy[x].direction[i].index+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 836 | /* |
| 837 | Sum variance. |
| 838 | */ |
| 839 | channel_features[RedChannel].sum_variance[i]+= |
| 840 | (x-channel_features[RedChannel].sum_entropy[i])* |
| 841 | (x-channel_features[RedChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 842 | density_xy[x].direction[i].red; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 843 | channel_features[GreenChannel].sum_variance[i]+= |
| 844 | (x-channel_features[GreenChannel].sum_entropy[i])* |
| 845 | (x-channel_features[GreenChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 846 | density_xy[x].direction[i].green; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 847 | channel_features[BlueChannel].sum_variance[i]+= |
| 848 | (x-channel_features[BlueChannel].sum_entropy[i])* |
| 849 | (x-channel_features[BlueChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 850 | density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 851 | if (image->matte != MagickFalse) |
| 852 | channel_features[OpacityChannel].sum_variance[i]+= |
| 853 | (x-channel_features[OpacityChannel].sum_entropy[i])* |
| 854 | (x-channel_features[OpacityChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 855 | density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 856 | if (image->colorspace == CMYKColorspace) |
| 857 | channel_features[IndexChannel].sum_variance[i]+= |
| 858 | (x-channel_features[IndexChannel].sum_entropy[i])* |
| 859 | (x-channel_features[IndexChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 860 | density_xy[x].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 861 | } |
| 862 | } |
| 863 | /* |
| 864 | Compute more texture features. |
| 865 | */ |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 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 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 871 | register ssize_t |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 872 | y; |
| 873 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 874 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 875 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 876 | register ssize_t |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 877 | x; |
| 878 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 879 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 880 | { |
| 881 | /* |
| 882 | Sum of Squares: Variance |
| 883 | */ |
| 884 | variance.direction[i].red+=(y-mean.direction[i].red+1)* |
| 885 | (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red; |
| 886 | variance.direction[i].green+=(y-mean.direction[i].green+1)* |
| 887 | (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green; |
| 888 | variance.direction[i].blue+=(y-mean.direction[i].blue+1)* |
| 889 | (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue; |
| 890 | if (image->matte != MagickFalse) |
| 891 | variance.direction[i].opacity+=(y-mean.direction[i].opacity+1)* |
| 892 | (y-mean.direction[i].opacity+1)* |
| 893 | cooccurrence[x][y].direction[i].opacity; |
| 894 | if (image->colorspace == CMYKColorspace) |
| 895 | variance.direction[i].index+=(y-mean.direction[i].index+1)* |
| 896 | (y-mean.direction[i].index+1)*cooccurrence[x][y].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 897 | /* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 898 | Sum average / Difference Variance. |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 899 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 900 | density_xy[MagickAbsoluteValue(y-x)].direction[i].red+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 901 | cooccurrence[x][y].direction[i].red; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 902 | density_xy[MagickAbsoluteValue(y-x)].direction[i].green+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 903 | cooccurrence[x][y].direction[i].green; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 904 | density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 905 | cooccurrence[x][y].direction[i].blue; |
| 906 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 907 | density_xy[MagickAbsoluteValue(y-x)].direction[i].opacity+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 908 | cooccurrence[x][y].direction[i].opacity; |
| 909 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 910 | density_xy[MagickAbsoluteValue(y-x)].direction[i].index+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 911 | cooccurrence[x][y].direction[i].index; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 912 | /* |
| 913 | Information Measures of Correlation. |
| 914 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 915 | entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 916 | log10(cooccurrence[x][y].direction[i].red+MagickEpsilon); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 917 | entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 918 | log10(cooccurrence[x][y].direction[i].green+MagickEpsilon); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 919 | entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 920 | log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon); |
| 921 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 922 | entropy_xy.direction[i].opacity-= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 923 | cooccurrence[x][y].direction[i].opacity*log10( |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 924 | cooccurrence[x][y].direction[i].opacity+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 925 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 926 | entropy_xy.direction[i].index-=cooccurrence[x][y].direction[i].index* |
| 927 | log10(cooccurrence[x][y].direction[i].index+MagickEpsilon); |
| 928 | entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red* |
| 929 | log10(density_x[x].direction[i].red*density_y[y].direction[i].red+ |
| 930 | MagickEpsilon)); |
| 931 | entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green* |
| 932 | log10(density_x[x].direction[i].green*density_y[y].direction[i].green+ |
| 933 | MagickEpsilon)); |
| 934 | entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue* |
| 935 | log10(density_x[x].direction[i].blue*density_y[y].direction[i].blue+ |
| 936 | MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 937 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 938 | entropy_xy1.direction[i].opacity-=( |
| 939 | cooccurrence[x][y].direction[i].opacity*log10( |
| 940 | density_x[x].direction[i].opacity*density_y[y].direction[i].opacity+ |
| 941 | MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 942 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 943 | entropy_xy1.direction[i].index-=( |
| 944 | cooccurrence[x][y].direction[i].index*log10( |
| 945 | density_x[x].direction[i].index*density_y[y].direction[i].index+ |
| 946 | MagickEpsilon)); |
| 947 | entropy_xy2.direction[i].red-=(density_x[x].direction[i].red* |
| 948 | density_y[y].direction[i].red*log10(density_x[x].direction[i].red* |
| 949 | density_y[y].direction[i].red+MagickEpsilon)); |
| 950 | entropy_xy2.direction[i].green-=(density_x[x].direction[i].green* |
| 951 | density_y[y].direction[i].green*log10(density_x[x].direction[i].green* |
| 952 | density_y[y].direction[i].green+MagickEpsilon)); |
| 953 | entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue* |
| 954 | density_y[y].direction[i].blue*log10(density_x[x].direction[i].blue* |
| 955 | density_y[y].direction[i].blue+MagickEpsilon)); |
| 956 | if (image->matte != MagickFalse) |
| 957 | entropy_xy2.direction[i].opacity-=(density_x[x].direction[i].opacity* |
| 958 | density_y[y].direction[i].opacity*log10( |
| 959 | density_x[x].direction[i].opacity*density_y[y].direction[i].opacity+ |
| 960 | MagickEpsilon)); |
| 961 | if (image->colorspace == CMYKColorspace) |
| 962 | entropy_xy2.direction[i].index-=(density_x[x].direction[i].index* |
| 963 | density_y[y].direction[i].index*log10( |
| 964 | density_x[x].direction[i].index*density_y[y].direction[i].index+ |
| 965 | MagickEpsilon)); |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 969 | variance.direction[i].red; |
| 970 | channel_features[GreenChannel].variance_sum_of_squares[i]= |
| 971 | variance.direction[i].green; |
| 972 | channel_features[BlueChannel].variance_sum_of_squares[i]= |
| 973 | variance.direction[i].blue; |
| 974 | if (image->matte != MagickFalse) |
| 975 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 976 | variance.direction[i].opacity; |
| 977 | if (image->colorspace == CMYKColorspace) |
| 978 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 979 | variance.direction[i].index; |
| 980 | } |
| 981 | /* |
| 982 | Compute more texture features. |
| 983 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 984 | (void) ResetMagickMemory(&variance,0,sizeof(variance)); |
| 985 | (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares)); |
| 986 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 987 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 988 | #endif |
| 989 | for (i=0; i < 4; i++) |
| 990 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 991 | register ssize_t |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 992 | x; |
| 993 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 994 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 995 | { |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 996 | /* |
| 997 | Difference variance. |
| 998 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 999 | variance.direction[i].red+=density_xy[x].direction[i].red; |
| 1000 | variance.direction[i].green+=density_xy[x].direction[i].green; |
| 1001 | variance.direction[i].blue+=density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1002 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1003 | variance.direction[i].opacity+=density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1004 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1005 | variance.direction[i].index+=density_xy[x].direction[i].index; |
| 1006 | sum_squares.direction[i].red+=density_xy[x].direction[i].red* |
| 1007 | density_xy[x].direction[i].red; |
| 1008 | sum_squares.direction[i].green+=density_xy[x].direction[i].green* |
| 1009 | density_xy[x].direction[i].green; |
| 1010 | sum_squares.direction[i].blue+=density_xy[x].direction[i].blue* |
| 1011 | density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1012 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1013 | sum_squares.direction[i].opacity+=density_xy[x].direction[i].opacity* |
| 1014 | density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1015 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1016 | sum_squares.direction[i].index+=density_xy[x].direction[i].index* |
| 1017 | density_xy[x].direction[i].index; |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1018 | /* |
| 1019 | Difference entropy. |
| 1020 | */ |
| 1021 | channel_features[RedChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1022 | density_xy[x].direction[i].red* |
| 1023 | log10(density_xy[x].direction[i].red+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1024 | channel_features[GreenChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1025 | density_xy[x].direction[i].green* |
| 1026 | log10(density_xy[x].direction[i].green+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1027 | channel_features[BlueChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1028 | density_xy[x].direction[i].blue* |
| 1029 | log10(density_xy[x].direction[i].blue+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1030 | if (image->matte != MagickFalse) |
| 1031 | channel_features[OpacityChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1032 | density_xy[x].direction[i].opacity* |
| 1033 | log10(density_xy[x].direction[i].opacity+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1034 | if (image->colorspace == CMYKColorspace) |
| 1035 | channel_features[IndexChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1036 | density_xy[x].direction[i].index* |
| 1037 | log10(density_xy[x].direction[i].index+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1038 | /* |
| 1039 | Information Measures of Correlation. |
| 1040 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1041 | entropy_x.direction[i].red-=(density_x[x].direction[i].red* |
| 1042 | log10(density_x[x].direction[i].red+MagickEpsilon)); |
| 1043 | entropy_x.direction[i].green-=(density_x[x].direction[i].green* |
| 1044 | log10(density_x[x].direction[i].green+MagickEpsilon)); |
| 1045 | entropy_x.direction[i].blue-=(density_x[x].direction[i].blue* |
| 1046 | log10(density_x[x].direction[i].blue+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1047 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1048 | entropy_x.direction[i].opacity-=(density_x[x].direction[i].opacity* |
| 1049 | log10(density_x[x].direction[i].opacity+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1050 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1051 | entropy_x.direction[i].index-=(density_x[x].direction[i].index* |
| 1052 | log10(density_x[x].direction[i].index+MagickEpsilon)); |
| 1053 | entropy_y.direction[i].red-=(density_y[y].direction[i].red* |
| 1054 | log10(density_y[y].direction[i].red+MagickEpsilon)); |
| 1055 | entropy_y.direction[i].green-=(density_y[y].direction[i].green* |
| 1056 | log10(density_y[y].direction[i].green+MagickEpsilon)); |
| 1057 | entropy_y.direction[i].blue-=(density_y[y].direction[i].blue* |
| 1058 | log10(density_y[y].direction[i].blue+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1059 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1060 | entropy_y.direction[i].opacity-=(density_y[y].direction[i].opacity* |
| 1061 | log10(density_y[y].direction[i].opacity+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1062 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1063 | entropy_y.direction[i].index-=(density_y[y].direction[i].index* |
| 1064 | log10(density_y[y].direction[i].index+MagickEpsilon)); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1065 | } |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1066 | /* |
| 1067 | Difference variance. |
| 1068 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1069 | channel_features[RedChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1070 | (((double) number_grays*number_grays*sum_squares.direction[i].red)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1071 | (variance.direction[i].red*variance.direction[i].red))/ |
| 1072 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1073 | channel_features[GreenChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1074 | (((double) number_grays*number_grays*sum_squares.direction[i].green)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1075 | (variance.direction[i].green*variance.direction[i].green))/ |
| 1076 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1077 | channel_features[BlueChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1078 | (((double) number_grays*number_grays*sum_squares.direction[i].blue)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1079 | (variance.direction[i].blue*variance.direction[i].blue))/ |
| 1080 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1081 | if (image->matte != MagickFalse) |
| 1082 | channel_features[OpacityChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1083 | (((double) number_grays*number_grays*sum_squares.direction[i].opacity)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1084 | (variance.direction[i].opacity*variance.direction[i].opacity))/ |
| 1085 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1086 | if (image->colorspace == CMYKColorspace) |
| 1087 | channel_features[IndexChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1088 | (((double) number_grays*number_grays*sum_squares.direction[i].index)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1089 | (variance.direction[i].index*variance.direction[i].index))/ |
| 1090 | ((double) number_grays*number_grays*number_grays*number_grays); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1091 | /* |
| 1092 | Information Measures of Correlation. |
| 1093 | */ |
| 1094 | channel_features[RedChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1095 | (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/ |
| 1096 | (entropy_x.direction[i].red > entropy_y.direction[i].red ? |
| 1097 | entropy_x.direction[i].red : entropy_y.direction[i].red); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1098 | channel_features[GreenChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1099 | (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/ |
| 1100 | (entropy_x.direction[i].green > entropy_y.direction[i].green ? |
| 1101 | entropy_x.direction[i].green : entropy_y.direction[i].green); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1102 | channel_features[BlueChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1103 | (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/ |
| 1104 | (entropy_x.direction[i].blue > entropy_y.direction[i].blue ? |
| 1105 | entropy_x.direction[i].blue : entropy_y.direction[i].blue); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1106 | if (image->matte != MagickFalse) |
| 1107 | channel_features[OpacityChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1108 | (entropy_xy.direction[i].opacity-entropy_xy1.direction[i].opacity)/ |
| 1109 | (entropy_x.direction[i].opacity > entropy_y.direction[i].opacity ? |
| 1110 | entropy_x.direction[i].opacity : entropy_y.direction[i].opacity); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1111 | if (image->colorspace == CMYKColorspace) |
| 1112 | channel_features[IndexChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1113 | (entropy_xy.direction[i].index-entropy_xy1.direction[i].index)/ |
| 1114 | (entropy_x.direction[i].index > entropy_y.direction[i].index ? |
| 1115 | entropy_x.direction[i].index : entropy_y.direction[i].index); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1116 | channel_features[RedChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1117 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].red- |
| 1118 | entropy_xy.direction[i].red))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1119 | channel_features[GreenChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1120 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].green- |
| 1121 | entropy_xy.direction[i].green))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1122 | channel_features[BlueChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1123 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].blue- |
| 1124 | entropy_xy.direction[i].blue))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1125 | if (image->matte != MagickFalse) |
| 1126 | channel_features[OpacityChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1127 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].opacity- |
| 1128 | entropy_xy.direction[i].opacity))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1129 | if (image->colorspace == CMYKColorspace) |
| 1130 | channel_features[IndexChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1131 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].index- |
| 1132 | entropy_xy.direction[i].index))))); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1133 | } |
| 1134 | /* |
| 1135 | Compute more texture features. |
| 1136 | */ |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1137 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1138 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 1139 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1140 | for (i=0; i < 4; i++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1141 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1142 | for (z=0; z < (ssize_t) number_grays; z++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1143 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1144 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1145 | y; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1146 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1147 | ChannelStatistics |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1148 | pixel; |
| 1149 | |
| 1150 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1151 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1152 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1153 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1154 | x; |
| 1155 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1156 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1157 | { |
| 1158 | /* |
| 1159 | Contrast: amount of local variations present in an image. |
| 1160 | */ |
| 1161 | if (((y-x) == z) || ((x-y) == z)) |
| 1162 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1163 | pixel.direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 1164 | pixel.direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 1165 | pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1166 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1167 | pixel.direction[i].opacity+= |
| 1168 | cooccurrence[x][y].direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1169 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1170 | pixel.direction[i].index+=cooccurrence[x][y].direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1171 | } |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1172 | /* |
| 1173 | Maximum Correlation Coefficient. |
| 1174 | */ |
| 1175 | Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red* |
| 1176 | cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/ |
| 1177 | density_y[x].direction[i].red; |
| 1178 | Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green* |
| 1179 | cooccurrence[y][x].direction[i].green/ |
| 1180 | density_x[z].direction[i].green/density_y[x].direction[i].red; |
| 1181 | Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue* |
| 1182 | cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/ |
| 1183 | density_y[x].direction[i].blue; |
| 1184 | if (image->matte != MagickFalse) |
| 1185 | Q[z][y].direction[i].opacity+= |
| 1186 | cooccurrence[z][x].direction[i].opacity* |
| 1187 | cooccurrence[y][x].direction[i].opacity/ |
| 1188 | density_x[z].direction[i].opacity/ |
| 1189 | density_y[x].direction[i].opacity; |
| 1190 | if (image->colorspace == CMYKColorspace) |
| 1191 | Q[z][y].direction[i].index+=cooccurrence[z][x].direction[i].index* |
| 1192 | cooccurrence[y][x].direction[i].index/ |
| 1193 | density_x[z].direction[i].index/density_y[x].direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1194 | } |
| 1195 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1196 | channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red; |
| 1197 | channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green; |
| 1198 | channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1199 | if (image->matte != MagickFalse) |
| 1200 | channel_features[OpacityChannel].contrast[i]+=z*z* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1201 | pixel.direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1202 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1203 | channel_features[BlackChannel].contrast[i]+=z*z* |
| 1204 | pixel.direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1205 | } |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1206 | /* |
| 1207 | Maximum Correlation Coefficient. |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1208 | Future: return second largest eigenvalue of Q. |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1209 | */ |
| 1210 | channel_features[RedChannel].maximum_correlation_coefficient[i]= |
| 1211 | sqrt((double) -1.0); |
| 1212 | channel_features[GreenChannel].maximum_correlation_coefficient[i]= |
| 1213 | sqrt((double) -1.0); |
| 1214 | channel_features[BlueChannel].maximum_correlation_coefficient[i]= |
| 1215 | sqrt((double) -1.0); |
| 1216 | if (image->matte != MagickFalse) |
| 1217 | channel_features[OpacityChannel].maximum_correlation_coefficient[i]= |
| 1218 | sqrt((double) -1.0); |
| 1219 | if (image->colorspace == CMYKColorspace) |
| 1220 | channel_features[IndexChannel].maximum_correlation_coefficient[i]= |
| 1221 | sqrt((double) -1.0); |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1222 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 1223 | /* |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 1224 | Relinquish resources. |
| 1225 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1226 | sum=(ChannelStatistics *) RelinquishMagickMemory(sum); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1227 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1228 | Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]); |
| 1229 | Q=(ChannelStatistics **) RelinquishMagickMemory(Q); |
| 1230 | density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y); |
| 1231 | density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy); |
| 1232 | density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1233 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1234 | cooccurrence[i]=(ChannelStatistics *) |
| 1235 | RelinquishMagickMemory(cooccurrence[i]); |
| 1236 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 1237 | return(channel_features); |
| 1238 | } |