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 | % % |
cristy | 7e41fe8 | 2010-12-04 23:12:08 +0000 | [diff] [blame] | 20 | % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 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 | 0c1c3fd | 2011-01-18 15:53:10 +0000 | [diff] [blame] | 111 | % channel_features=GetImageChannelFeatures(image,1,exception); |
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 | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 174 | MagickBooleanType |
| 175 | status; |
| 176 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 177 | register ssize_t |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 178 | i; |
| 179 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 180 | size_t |
| 181 | length; |
| 182 | |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 183 | ssize_t |
| 184 | y, |
| 185 | z; |
| 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 | 9a9230e | 2011-04-26 14:56:14 +0000 | [diff] [blame^] | 196 | length=CompositeChannels+1UL; |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 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 | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 250 | grays[ScaleQuantumToMap(GetRedPixelComponent(p))].red= |
| 251 | ScaleQuantumToMap(GetRedPixelComponent(p)); |
| 252 | grays[ScaleQuantumToMap(GetGreenPixelComponent(p))].green= |
| 253 | ScaleQuantumToMap(GetGreenPixelComponent(p)); |
| 254 | grays[ScaleQuantumToMap(GetBluePixelComponent(p))].blue= |
| 255 | ScaleQuantumToMap(GetBluePixelComponent(p)); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 256 | if (image->matte != MagickFalse) |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 257 | grays[ScaleQuantumToMap(GetOpacityPixelComponent(p))].opacity= |
| 258 | ScaleQuantumToMap(GetOpacityPixelComponent(p)); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 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 | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 408 | register const IndexPacket |
| 409 | *restrict indexes; |
| 410 | |
| 411 | register const PixelPacket |
| 412 | *restrict p; |
| 413 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 414 | register ssize_t |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 415 | x; |
| 416 | |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 417 | ssize_t |
cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 418 | offset, |
| 419 | u, |
| 420 | v; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 421 | |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 422 | if (status == MagickFalse) |
| 423 | continue; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 424 | p=GetCacheViewVirtualPixels(image_view,-(ssize_t) distance,y,image->columns+ |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 425 | 2*distance,distance+1,exception); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 426 | if (p == (const PixelPacket *) NULL) |
| 427 | { |
| 428 | status=MagickFalse; |
| 429 | continue; |
| 430 | } |
| 431 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 432 | p+=distance; |
| 433 | indexes+=distance; |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 434 | for (x=0; x < (ssize_t) image->columns; x++) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 435 | { |
| 436 | for (i=0; i < 4; i++) |
| 437 | { |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 438 | switch (i) |
| 439 | { |
| 440 | case 0: |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 441 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 442 | { |
| 443 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 444 | Horizontal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 445 | */ |
| 446 | offset=(ssize_t) distance; |
| 447 | break; |
| 448 | } |
| 449 | case 1: |
| 450 | { |
| 451 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 452 | Vertical adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 453 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 454 | offset=(ssize_t) (image->columns+2*distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | case 2: |
| 458 | { |
| 459 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 460 | Right diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 461 | */ |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 462 | offset=(ssize_t) ((image->columns+2*distance)-distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 463 | break; |
| 464 | } |
| 465 | case 3: |
| 466 | { |
| 467 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 468 | Left diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 469 | */ |
cristy | d99b096 | 2010-05-29 23:14:26 +0000 | [diff] [blame] | 470 | offset=(ssize_t) ((image->columns+2*distance)+distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | } |
| 474 | u=0; |
| 475 | v=0; |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 476 | while (grays[u].red != ScaleQuantumToMap(GetRedPixelComponent(p))) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 477 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 478 | while (grays[v].red != ScaleQuantumToMap((p+offset)->red)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 479 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 480 | cooccurrence[u][v].direction[i].red++; |
| 481 | cooccurrence[v][u].direction[i].red++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 482 | u=0; |
| 483 | v=0; |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 484 | while (grays[u].green != ScaleQuantumToMap(GetGreenPixelComponent(p))) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 485 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 486 | while (grays[v].green != ScaleQuantumToMap((p+offset)->green)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 487 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 488 | cooccurrence[u][v].direction[i].green++; |
| 489 | cooccurrence[v][u].direction[i].green++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 490 | u=0; |
| 491 | v=0; |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 492 | while (grays[u].blue != ScaleQuantumToMap(GetBluePixelComponent(p))) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 493 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 494 | while (grays[v].blue != ScaleQuantumToMap((p+offset)->blue)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 495 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 496 | cooccurrence[u][v].direction[i].blue++; |
| 497 | cooccurrence[v][u].direction[i].blue++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 498 | if (image->matte != MagickFalse) |
| 499 | { |
| 500 | u=0; |
| 501 | v=0; |
cristy | d05ecd1 | 2011-04-22 20:44:42 +0000 | [diff] [blame] | 502 | while (grays[u].opacity != ScaleQuantumToMap(GetOpacityPixelComponent(p))) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 503 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 504 | while (grays[v].opacity != ScaleQuantumToMap((p+offset)->opacity)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 505 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 506 | cooccurrence[u][v].direction[i].opacity++; |
| 507 | cooccurrence[v][u].direction[i].opacity++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 508 | } |
| 509 | if (image->colorspace == CMYKColorspace) |
| 510 | { |
| 511 | u=0; |
| 512 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 513 | while (grays[u].index != ScaleQuantumToMap(indexes[x])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 514 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 515 | while (grays[v].index != ScaleQuantumToMap(indexes[x+offset])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 516 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 517 | cooccurrence[u][v].direction[i].index++; |
| 518 | cooccurrence[v][u].direction[i].index++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 519 | } |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 520 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 521 | p++; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 522 | } |
| 523 | } |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 524 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 525 | image_view=DestroyCacheView(image_view); |
| 526 | if (status == MagickFalse) |
| 527 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 528 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 529 | cooccurrence[i]=(ChannelStatistics *) |
| 530 | RelinquishMagickMemory(cooccurrence[i]); |
| 531 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 532 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 533 | channel_features); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 534 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 535 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 536 | return(channel_features); |
| 537 | } |
| 538 | /* |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 539 | Normalize spatial dependence matrix. |
| 540 | */ |
| 541 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 542 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 543 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 544 | for (i=0; i < 4; i++) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 545 | { |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 546 | double |
| 547 | normalize; |
| 548 | |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 549 | switch (i) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 550 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 551 | case 0: |
| 552 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 553 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 554 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 555 | Horizontal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 556 | */ |
| 557 | normalize=2.0*image->rows*(image->columns-distance); |
| 558 | break; |
| 559 | } |
| 560 | case 1: |
| 561 | { |
| 562 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 563 | Vertical adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 564 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 565 | normalize=2.0*(image->rows-distance)*image->columns; |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 566 | break; |
| 567 | } |
| 568 | case 2: |
| 569 | { |
| 570 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 571 | Right diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 572 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 573 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 574 | break; |
| 575 | } |
| 576 | case 3: |
| 577 | { |
| 578 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 579 | Left diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 580 | */ |
| 581 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
| 582 | break; |
| 583 | } |
| 584 | } |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 585 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 586 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 587 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 588 | x; |
| 589 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 590 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 591 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 592 | cooccurrence[x][y].direction[i].red/=normalize; |
| 593 | cooccurrence[x][y].direction[i].green/=normalize; |
| 594 | cooccurrence[x][y].direction[i].blue/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 595 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 596 | cooccurrence[x][y].direction[i].opacity/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 597 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 598 | cooccurrence[x][y].direction[i].index/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
| 601 | } |
| 602 | /* |
| 603 | Compute texture features. |
| 604 | */ |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 605 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 606 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 607 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 608 | for (i=0; i < 4; i++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 609 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 610 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 611 | y; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 612 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 613 | for (y=0; y < (ssize_t) number_grays; 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 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 616 | x; |
| 617 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 618 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 619 | { |
| 620 | /* |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 621 | Angular second moment: measure of homogeneity of the image. |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 622 | */ |
| 623 | channel_features[RedChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 624 | cooccurrence[x][y].direction[i].red* |
| 625 | cooccurrence[x][y].direction[i].red; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 626 | channel_features[GreenChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 627 | cooccurrence[x][y].direction[i].green* |
| 628 | cooccurrence[x][y].direction[i].green; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 629 | channel_features[BlueChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 630 | cooccurrence[x][y].direction[i].blue* |
| 631 | cooccurrence[x][y].direction[i].blue; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 632 | if (image->matte != MagickFalse) |
| 633 | channel_features[OpacityChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 634 | cooccurrence[x][y].direction[i].opacity* |
| 635 | cooccurrence[x][y].direction[i].opacity; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 636 | if (image->colorspace == CMYKColorspace) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 637 | channel_features[BlackChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 638 | cooccurrence[x][y].direction[i].index* |
| 639 | cooccurrence[x][y].direction[i].index; |
| 640 | /* |
| 641 | Correlation: measure of linear-dependencies in the image. |
| 642 | */ |
| 643 | sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 644 | sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green; |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 645 | sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
| 646 | if (image->matte != MagickFalse) |
| 647 | sum[y].direction[i].opacity+=cooccurrence[x][y].direction[i].opacity; |
| 648 | if (image->colorspace == CMYKColorspace) |
| 649 | sum[y].direction[i].index+=cooccurrence[x][y].direction[i].index; |
| 650 | correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 651 | correlation.direction[i].green+=x*y* |
| 652 | cooccurrence[x][y].direction[i].green; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 653 | correlation.direction[i].blue+=x*y* |
| 654 | cooccurrence[x][y].direction[i].blue; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 655 | if (image->matte != MagickFalse) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 656 | correlation.direction[i].opacity+=x*y* |
| 657 | cooccurrence[x][y].direction[i].opacity; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 658 | if (image->colorspace == CMYKColorspace) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 659 | correlation.direction[i].index+=x*y* |
| 660 | cooccurrence[x][y].direction[i].index; |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 661 | /* |
| 662 | Inverse Difference Moment. |
| 663 | */ |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 664 | channel_features[RedChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 665 | cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 666 | channel_features[GreenChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 667 | cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 668 | channel_features[BlueChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 669 | cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1); |
| 670 | if (image->matte != MagickFalse) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 671 | channel_features[OpacityChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 672 | cooccurrence[x][y].direction[i].opacity/((y-x)*(y-x)+1); |
| 673 | if (image->colorspace == CMYKColorspace) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 674 | channel_features[IndexChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 675 | cooccurrence[x][y].direction[i].index/((y-x)*(y-x)+1); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 676 | /* |
| 677 | Sum average. |
| 678 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 679 | density_xy[y+x+2].direction[i].red+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 680 | cooccurrence[x][y].direction[i].red; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 681 | density_xy[y+x+2].direction[i].green+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 682 | cooccurrence[x][y].direction[i].green; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 683 | density_xy[y+x+2].direction[i].blue+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 684 | cooccurrence[x][y].direction[i].blue; |
| 685 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 686 | density_xy[y+x+2].direction[i].opacity+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 687 | cooccurrence[x][y].direction[i].opacity; |
| 688 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 689 | density_xy[y+x+2].direction[i].index+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 690 | cooccurrence[x][y].direction[i].index; |
| 691 | /* |
| 692 | Entropy. |
| 693 | */ |
| 694 | channel_features[RedChannel].entropy[i]-= |
| 695 | cooccurrence[x][y].direction[i].red* |
| 696 | log10(cooccurrence[x][y].direction[i].red+MagickEpsilon); |
| 697 | channel_features[GreenChannel].entropy[i]-= |
| 698 | cooccurrence[x][y].direction[i].green* |
| 699 | log10(cooccurrence[x][y].direction[i].green+MagickEpsilon); |
| 700 | channel_features[BlueChannel].entropy[i]-= |
| 701 | cooccurrence[x][y].direction[i].blue* |
| 702 | log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon); |
| 703 | if (image->matte != MagickFalse) |
| 704 | channel_features[OpacityChannel].entropy[i]-= |
| 705 | cooccurrence[x][y].direction[i].opacity* |
| 706 | log10(cooccurrence[x][y].direction[i].opacity+MagickEpsilon); |
| 707 | if (image->colorspace == CMYKColorspace) |
| 708 | channel_features[IndexChannel].entropy[i]-= |
| 709 | cooccurrence[x][y].direction[i].index* |
| 710 | log10(cooccurrence[x][y].direction[i].index+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 711 | /* |
| 712 | Information Measures of Correlation. |
| 713 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 714 | density_x[x].direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 715 | density_x[x].direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 716 | density_x[x].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 717 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 718 | density_x[x].direction[i].opacity+= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 719 | cooccurrence[x][y].direction[i].opacity; |
| 720 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 721 | density_x[x].direction[i].index+= |
| 722 | cooccurrence[x][y].direction[i].index; |
| 723 | density_y[y].direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 724 | density_y[y].direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 725 | density_y[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 726 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 727 | density_y[y].direction[i].opacity+= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 728 | cooccurrence[x][y].direction[i].opacity; |
| 729 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 730 | density_y[y].direction[i].index+= |
| 731 | cooccurrence[x][y].direction[i].index; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 732 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 733 | mean.direction[i].red+=y*sum[y].direction[i].red; |
| 734 | sum_squares.direction[i].red+=y*y*sum[y].direction[i].red; |
| 735 | mean.direction[i].green+=y*sum[y].direction[i].green; |
| 736 | sum_squares.direction[i].green+=y*y*sum[y].direction[i].green; |
| 737 | mean.direction[i].blue+=y*sum[y].direction[i].blue; |
| 738 | sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue; |
| 739 | if (image->matte != MagickFalse) |
| 740 | { |
| 741 | mean.direction[i].opacity+=y*sum[y].direction[i].opacity; |
| 742 | sum_squares.direction[i].opacity+=y*y*sum[y].direction[i].opacity; |
| 743 | } |
| 744 | if (image->colorspace == CMYKColorspace) |
| 745 | { |
| 746 | mean.direction[i].index+=y*sum[y].direction[i].index; |
| 747 | sum_squares.direction[i].index+=y*y*sum[y].direction[i].index; |
| 748 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 749 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 750 | /* |
| 751 | Correlation: measure of linear-dependencies in the image. |
| 752 | */ |
| 753 | channel_features[RedChannel].correlation[i]= |
| 754 | (correlation.direction[i].red-mean.direction[i].red* |
| 755 | mean.direction[i].red)/(sqrt(sum_squares.direction[i].red- |
| 756 | (mean.direction[i].red*mean.direction[i].red))*sqrt( |
| 757 | sum_squares.direction[i].red-(mean.direction[i].red* |
| 758 | mean.direction[i].red))); |
| 759 | channel_features[GreenChannel].correlation[i]= |
| 760 | (correlation.direction[i].green-mean.direction[i].green* |
| 761 | mean.direction[i].green)/(sqrt(sum_squares.direction[i].green- |
| 762 | (mean.direction[i].green*mean.direction[i].green))*sqrt( |
| 763 | sum_squares.direction[i].green-(mean.direction[i].green* |
| 764 | mean.direction[i].green))); |
| 765 | channel_features[BlueChannel].correlation[i]= |
| 766 | (correlation.direction[i].blue-mean.direction[i].blue* |
| 767 | mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue- |
| 768 | (mean.direction[i].blue*mean.direction[i].blue))*sqrt( |
| 769 | sum_squares.direction[i].blue-(mean.direction[i].blue* |
| 770 | mean.direction[i].blue))); |
| 771 | if (image->matte != MagickFalse) |
| 772 | channel_features[OpacityChannel].correlation[i]= |
| 773 | (correlation.direction[i].opacity-mean.direction[i].opacity* |
| 774 | mean.direction[i].opacity)/(sqrt(sum_squares.direction[i].opacity- |
| 775 | (mean.direction[i].opacity*mean.direction[i].opacity))*sqrt( |
| 776 | sum_squares.direction[i].opacity-(mean.direction[i].opacity* |
| 777 | mean.direction[i].opacity))); |
| 778 | if (image->colorspace == CMYKColorspace) |
| 779 | channel_features[IndexChannel].correlation[i]= |
| 780 | (correlation.direction[i].index-mean.direction[i].index* |
| 781 | mean.direction[i].index)/(sqrt(sum_squares.direction[i].index- |
| 782 | (mean.direction[i].index*mean.direction[i].index))*sqrt( |
| 783 | sum_squares.direction[i].index-(mean.direction[i].index* |
| 784 | mean.direction[i].index))); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 785 | } |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 786 | /* |
| 787 | Compute more texture features. |
| 788 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 789 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 790 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 791 | #endif |
| 792 | for (i=0; i < 4; i++) |
| 793 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 794 | register ssize_t |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 795 | x; |
| 796 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 797 | for (x=2; x < (ssize_t) (2*number_grays); x++) |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 798 | { |
| 799 | /* |
| 800 | Sum average. |
| 801 | */ |
| 802 | channel_features[RedChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 803 | x*density_xy[x].direction[i].red; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 804 | channel_features[GreenChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 805 | x*density_xy[x].direction[i].green; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 806 | channel_features[BlueChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 807 | x*density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 808 | if (image->matte != MagickFalse) |
| 809 | channel_features[OpacityChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 810 | x*density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 811 | if (image->colorspace == CMYKColorspace) |
| 812 | channel_features[IndexChannel].sum_average[i]+= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 813 | x*density_xy[x].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 814 | /* |
| 815 | Sum entropy. |
| 816 | */ |
| 817 | channel_features[RedChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 818 | density_xy[x].direction[i].red* |
| 819 | log10(density_xy[x].direction[i].red+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 820 | channel_features[GreenChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 821 | density_xy[x].direction[i].green* |
| 822 | log10(density_xy[x].direction[i].green+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 823 | channel_features[BlueChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 824 | density_xy[x].direction[i].blue* |
| 825 | log10(density_xy[x].direction[i].blue+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 826 | if (image->matte != MagickFalse) |
| 827 | channel_features[OpacityChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 828 | density_xy[x].direction[i].opacity* |
| 829 | log10(density_xy[x].direction[i].opacity+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 830 | if (image->colorspace == CMYKColorspace) |
| 831 | channel_features[IndexChannel].sum_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 832 | density_xy[x].direction[i].index* |
| 833 | log10(density_xy[x].direction[i].index+MagickEpsilon); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 834 | /* |
| 835 | Sum variance. |
| 836 | */ |
| 837 | channel_features[RedChannel].sum_variance[i]+= |
| 838 | (x-channel_features[RedChannel].sum_entropy[i])* |
| 839 | (x-channel_features[RedChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 840 | density_xy[x].direction[i].red; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 841 | channel_features[GreenChannel].sum_variance[i]+= |
| 842 | (x-channel_features[GreenChannel].sum_entropy[i])* |
| 843 | (x-channel_features[GreenChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 844 | density_xy[x].direction[i].green; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 845 | channel_features[BlueChannel].sum_variance[i]+= |
| 846 | (x-channel_features[BlueChannel].sum_entropy[i])* |
| 847 | (x-channel_features[BlueChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 848 | density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 849 | if (image->matte != MagickFalse) |
| 850 | channel_features[OpacityChannel].sum_variance[i]+= |
| 851 | (x-channel_features[OpacityChannel].sum_entropy[i])* |
| 852 | (x-channel_features[OpacityChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 853 | density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 854 | if (image->colorspace == CMYKColorspace) |
| 855 | channel_features[IndexChannel].sum_variance[i]+= |
| 856 | (x-channel_features[IndexChannel].sum_entropy[i])* |
| 857 | (x-channel_features[IndexChannel].sum_entropy[i])* |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 858 | density_xy[x].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | /* |
| 862 | Compute more texture features. |
| 863 | */ |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 864 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 865 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 866 | #endif |
| 867 | for (i=0; i < 4; i++) |
| 868 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 869 | register ssize_t |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 870 | y; |
| 871 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 872 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 873 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 874 | register ssize_t |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 875 | x; |
| 876 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 877 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 878 | { |
| 879 | /* |
| 880 | Sum of Squares: Variance |
| 881 | */ |
| 882 | variance.direction[i].red+=(y-mean.direction[i].red+1)* |
| 883 | (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red; |
| 884 | variance.direction[i].green+=(y-mean.direction[i].green+1)* |
| 885 | (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green; |
| 886 | variance.direction[i].blue+=(y-mean.direction[i].blue+1)* |
| 887 | (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue; |
| 888 | if (image->matte != MagickFalse) |
| 889 | variance.direction[i].opacity+=(y-mean.direction[i].opacity+1)* |
| 890 | (y-mean.direction[i].opacity+1)* |
| 891 | cooccurrence[x][y].direction[i].opacity; |
| 892 | if (image->colorspace == CMYKColorspace) |
| 893 | variance.direction[i].index+=(y-mean.direction[i].index+1)* |
| 894 | (y-mean.direction[i].index+1)*cooccurrence[x][y].direction[i].index; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 895 | /* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 896 | Sum average / Difference Variance. |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 897 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 898 | density_xy[MagickAbsoluteValue(y-x)].direction[i].red+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 899 | cooccurrence[x][y].direction[i].red; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 900 | density_xy[MagickAbsoluteValue(y-x)].direction[i].green+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 901 | cooccurrence[x][y].direction[i].green; |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 902 | density_xy[MagickAbsoluteValue(y-x)].direction[i].blue+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 903 | cooccurrence[x][y].direction[i].blue; |
| 904 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 905 | density_xy[MagickAbsoluteValue(y-x)].direction[i].opacity+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 906 | cooccurrence[x][y].direction[i].opacity; |
| 907 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 908 | density_xy[MagickAbsoluteValue(y-x)].direction[i].index+= |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 909 | cooccurrence[x][y].direction[i].index; |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 910 | /* |
| 911 | Information Measures of Correlation. |
| 912 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 913 | entropy_xy.direction[i].red-=cooccurrence[x][y].direction[i].red* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 914 | log10(cooccurrence[x][y].direction[i].red+MagickEpsilon); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 915 | entropy_xy.direction[i].green-=cooccurrence[x][y].direction[i].green* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 916 | log10(cooccurrence[x][y].direction[i].green+MagickEpsilon); |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 917 | entropy_xy.direction[i].blue-=cooccurrence[x][y].direction[i].blue* |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 918 | log10(cooccurrence[x][y].direction[i].blue+MagickEpsilon); |
| 919 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 920 | entropy_xy.direction[i].opacity-= |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 921 | cooccurrence[x][y].direction[i].opacity*log10( |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 922 | cooccurrence[x][y].direction[i].opacity+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 923 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 924 | entropy_xy.direction[i].index-=cooccurrence[x][y].direction[i].index* |
| 925 | log10(cooccurrence[x][y].direction[i].index+MagickEpsilon); |
| 926 | entropy_xy1.direction[i].red-=(cooccurrence[x][y].direction[i].red* |
| 927 | log10(density_x[x].direction[i].red*density_y[y].direction[i].red+ |
| 928 | MagickEpsilon)); |
| 929 | entropy_xy1.direction[i].green-=(cooccurrence[x][y].direction[i].green* |
| 930 | log10(density_x[x].direction[i].green*density_y[y].direction[i].green+ |
| 931 | MagickEpsilon)); |
| 932 | entropy_xy1.direction[i].blue-=(cooccurrence[x][y].direction[i].blue* |
| 933 | log10(density_x[x].direction[i].blue*density_y[y].direction[i].blue+ |
| 934 | MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 935 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 936 | entropy_xy1.direction[i].opacity-=( |
| 937 | cooccurrence[x][y].direction[i].opacity*log10( |
| 938 | density_x[x].direction[i].opacity*density_y[y].direction[i].opacity+ |
| 939 | MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 940 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 941 | entropy_xy1.direction[i].index-=( |
| 942 | cooccurrence[x][y].direction[i].index*log10( |
| 943 | density_x[x].direction[i].index*density_y[y].direction[i].index+ |
| 944 | MagickEpsilon)); |
| 945 | entropy_xy2.direction[i].red-=(density_x[x].direction[i].red* |
| 946 | density_y[y].direction[i].red*log10(density_x[x].direction[i].red* |
| 947 | density_y[y].direction[i].red+MagickEpsilon)); |
| 948 | entropy_xy2.direction[i].green-=(density_x[x].direction[i].green* |
| 949 | density_y[y].direction[i].green*log10(density_x[x].direction[i].green* |
| 950 | density_y[y].direction[i].green+MagickEpsilon)); |
| 951 | entropy_xy2.direction[i].blue-=(density_x[x].direction[i].blue* |
| 952 | density_y[y].direction[i].blue*log10(density_x[x].direction[i].blue* |
| 953 | density_y[y].direction[i].blue+MagickEpsilon)); |
| 954 | if (image->matte != MagickFalse) |
| 955 | entropy_xy2.direction[i].opacity-=(density_x[x].direction[i].opacity* |
| 956 | density_y[y].direction[i].opacity*log10( |
| 957 | density_x[x].direction[i].opacity*density_y[y].direction[i].opacity+ |
| 958 | MagickEpsilon)); |
| 959 | if (image->colorspace == CMYKColorspace) |
| 960 | entropy_xy2.direction[i].index-=(density_x[x].direction[i].index* |
| 961 | density_y[y].direction[i].index*log10( |
| 962 | density_x[x].direction[i].index*density_y[y].direction[i].index+ |
| 963 | MagickEpsilon)); |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 964 | } |
| 965 | } |
| 966 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 967 | variance.direction[i].red; |
| 968 | channel_features[GreenChannel].variance_sum_of_squares[i]= |
| 969 | variance.direction[i].green; |
| 970 | channel_features[BlueChannel].variance_sum_of_squares[i]= |
| 971 | variance.direction[i].blue; |
| 972 | if (image->matte != MagickFalse) |
| 973 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 974 | variance.direction[i].opacity; |
| 975 | if (image->colorspace == CMYKColorspace) |
| 976 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 977 | variance.direction[i].index; |
| 978 | } |
| 979 | /* |
| 980 | Compute more texture features. |
| 981 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 982 | (void) ResetMagickMemory(&variance,0,sizeof(variance)); |
| 983 | (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares)); |
| 984 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 985 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 986 | #endif |
| 987 | for (i=0; i < 4; i++) |
| 988 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 989 | register ssize_t |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 990 | x; |
| 991 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 992 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 993 | { |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 994 | /* |
| 995 | Difference variance. |
| 996 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 997 | variance.direction[i].red+=density_xy[x].direction[i].red; |
| 998 | variance.direction[i].green+=density_xy[x].direction[i].green; |
| 999 | variance.direction[i].blue+=density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1000 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1001 | variance.direction[i].opacity+=density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1002 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1003 | variance.direction[i].index+=density_xy[x].direction[i].index; |
| 1004 | sum_squares.direction[i].red+=density_xy[x].direction[i].red* |
| 1005 | density_xy[x].direction[i].red; |
| 1006 | sum_squares.direction[i].green+=density_xy[x].direction[i].green* |
| 1007 | density_xy[x].direction[i].green; |
| 1008 | sum_squares.direction[i].blue+=density_xy[x].direction[i].blue* |
| 1009 | density_xy[x].direction[i].blue; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1010 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1011 | sum_squares.direction[i].opacity+=density_xy[x].direction[i].opacity* |
| 1012 | density_xy[x].direction[i].opacity; |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1013 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1014 | sum_squares.direction[i].index+=density_xy[x].direction[i].index* |
| 1015 | density_xy[x].direction[i].index; |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1016 | /* |
| 1017 | Difference entropy. |
| 1018 | */ |
| 1019 | channel_features[RedChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1020 | density_xy[x].direction[i].red* |
| 1021 | log10(density_xy[x].direction[i].red+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1022 | channel_features[GreenChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1023 | density_xy[x].direction[i].green* |
| 1024 | log10(density_xy[x].direction[i].green+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1025 | channel_features[BlueChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1026 | density_xy[x].direction[i].blue* |
| 1027 | log10(density_xy[x].direction[i].blue+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1028 | if (image->matte != MagickFalse) |
| 1029 | channel_features[OpacityChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1030 | density_xy[x].direction[i].opacity* |
| 1031 | log10(density_xy[x].direction[i].opacity+MagickEpsilon); |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1032 | if (image->colorspace == CMYKColorspace) |
| 1033 | channel_features[IndexChannel].difference_entropy[i]-= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1034 | density_xy[x].direction[i].index* |
| 1035 | log10(density_xy[x].direction[i].index+MagickEpsilon); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1036 | /* |
| 1037 | Information Measures of Correlation. |
| 1038 | */ |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1039 | entropy_x.direction[i].red-=(density_x[x].direction[i].red* |
| 1040 | log10(density_x[x].direction[i].red+MagickEpsilon)); |
| 1041 | entropy_x.direction[i].green-=(density_x[x].direction[i].green* |
| 1042 | log10(density_x[x].direction[i].green+MagickEpsilon)); |
| 1043 | entropy_x.direction[i].blue-=(density_x[x].direction[i].blue* |
| 1044 | log10(density_x[x].direction[i].blue+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1045 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1046 | entropy_x.direction[i].opacity-=(density_x[x].direction[i].opacity* |
| 1047 | log10(density_x[x].direction[i].opacity+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1048 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1049 | entropy_x.direction[i].index-=(density_x[x].direction[i].index* |
| 1050 | log10(density_x[x].direction[i].index+MagickEpsilon)); |
| 1051 | entropy_y.direction[i].red-=(density_y[y].direction[i].red* |
| 1052 | log10(density_y[y].direction[i].red+MagickEpsilon)); |
| 1053 | entropy_y.direction[i].green-=(density_y[y].direction[i].green* |
| 1054 | log10(density_y[y].direction[i].green+MagickEpsilon)); |
| 1055 | entropy_y.direction[i].blue-=(density_y[y].direction[i].blue* |
| 1056 | log10(density_y[y].direction[i].blue+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1057 | if (image->matte != MagickFalse) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1058 | entropy_y.direction[i].opacity-=(density_y[y].direction[i].opacity* |
| 1059 | log10(density_y[y].direction[i].opacity+MagickEpsilon)); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1060 | if (image->colorspace == CMYKColorspace) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1061 | entropy_y.direction[i].index-=(density_y[y].direction[i].index* |
| 1062 | log10(density_y[y].direction[i].index+MagickEpsilon)); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1063 | } |
cristy | f6214de | 2010-01-29 02:47:41 +0000 | [diff] [blame] | 1064 | /* |
| 1065 | Difference variance. |
| 1066 | */ |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1067 | channel_features[RedChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1068 | (((double) number_grays*number_grays*sum_squares.direction[i].red)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1069 | (variance.direction[i].red*variance.direction[i].red))/ |
| 1070 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1071 | channel_features[GreenChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1072 | (((double) number_grays*number_grays*sum_squares.direction[i].green)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1073 | (variance.direction[i].green*variance.direction[i].green))/ |
| 1074 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1075 | channel_features[BlueChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1076 | (((double) number_grays*number_grays*sum_squares.direction[i].blue)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1077 | (variance.direction[i].blue*variance.direction[i].blue))/ |
| 1078 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1079 | if (image->matte != MagickFalse) |
| 1080 | channel_features[OpacityChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1081 | (((double) number_grays*number_grays*sum_squares.direction[i].opacity)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1082 | (variance.direction[i].opacity*variance.direction[i].opacity))/ |
| 1083 | ((double) number_grays*number_grays*number_grays*number_grays); |
| 1084 | if (image->colorspace == CMYKColorspace) |
| 1085 | channel_features[IndexChannel].difference_variance[i]= |
cristy | e0e19dc | 2010-01-29 02:13:08 +0000 | [diff] [blame] | 1086 | (((double) number_grays*number_grays*sum_squares.direction[i].index)- |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1087 | (variance.direction[i].index*variance.direction[i].index))/ |
| 1088 | ((double) number_grays*number_grays*number_grays*number_grays); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1089 | /* |
| 1090 | Information Measures of Correlation. |
| 1091 | */ |
| 1092 | channel_features[RedChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1093 | (entropy_xy.direction[i].red-entropy_xy1.direction[i].red)/ |
| 1094 | (entropy_x.direction[i].red > entropy_y.direction[i].red ? |
| 1095 | entropy_x.direction[i].red : entropy_y.direction[i].red); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1096 | channel_features[GreenChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1097 | (entropy_xy.direction[i].green-entropy_xy1.direction[i].green)/ |
| 1098 | (entropy_x.direction[i].green > entropy_y.direction[i].green ? |
| 1099 | entropy_x.direction[i].green : entropy_y.direction[i].green); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1100 | channel_features[BlueChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1101 | (entropy_xy.direction[i].blue-entropy_xy1.direction[i].blue)/ |
| 1102 | (entropy_x.direction[i].blue > entropy_y.direction[i].blue ? |
| 1103 | entropy_x.direction[i].blue : entropy_y.direction[i].blue); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1104 | if (image->matte != MagickFalse) |
| 1105 | channel_features[OpacityChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1106 | (entropy_xy.direction[i].opacity-entropy_xy1.direction[i].opacity)/ |
| 1107 | (entropy_x.direction[i].opacity > entropy_y.direction[i].opacity ? |
| 1108 | entropy_x.direction[i].opacity : entropy_y.direction[i].opacity); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1109 | if (image->colorspace == CMYKColorspace) |
| 1110 | channel_features[IndexChannel].measure_of_correlation_1[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1111 | (entropy_xy.direction[i].index-entropy_xy1.direction[i].index)/ |
| 1112 | (entropy_x.direction[i].index > entropy_y.direction[i].index ? |
| 1113 | entropy_x.direction[i].index : entropy_y.direction[i].index); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1114 | channel_features[RedChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1115 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].red- |
| 1116 | entropy_xy.direction[i].red))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1117 | channel_features[GreenChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1118 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].green- |
| 1119 | entropy_xy.direction[i].green))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1120 | channel_features[BlueChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1121 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].blue- |
| 1122 | entropy_xy.direction[i].blue))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1123 | if (image->matte != MagickFalse) |
| 1124 | channel_features[OpacityChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1125 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].opacity- |
| 1126 | entropy_xy.direction[i].opacity))))); |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1127 | if (image->colorspace == CMYKColorspace) |
| 1128 | channel_features[IndexChannel].measure_of_correlation_2[i]= |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1129 | (sqrt(fabs(1.0-exp(-2.0*(entropy_xy2.direction[i].index- |
| 1130 | entropy_xy.direction[i].index))))); |
cristy | e189779 | 2010-01-29 02:05:50 +0000 | [diff] [blame] | 1131 | } |
| 1132 | /* |
| 1133 | Compute more texture features. |
| 1134 | */ |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1135 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 1136 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 1137 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1138 | for (i=0; i < 4; i++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1139 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1140 | for (z=0; z < (ssize_t) number_grays; z++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1141 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1142 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1143 | y; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1144 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1145 | ChannelStatistics |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1146 | pixel; |
| 1147 | |
| 1148 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1149 | for (y=0; y < (ssize_t) number_grays; y++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1150 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1151 | register ssize_t |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 1152 | x; |
| 1153 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1154 | for (x=0; x < (ssize_t) number_grays; x++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1155 | { |
| 1156 | /* |
| 1157 | Contrast: amount of local variations present in an image. |
| 1158 | */ |
| 1159 | if (((y-x) == z) || ((x-y) == z)) |
| 1160 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1161 | pixel.direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 1162 | pixel.direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 1163 | pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1164 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1165 | pixel.direction[i].opacity+= |
| 1166 | cooccurrence[x][y].direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1167 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1168 | pixel.direction[i].index+=cooccurrence[x][y].direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1169 | } |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1170 | /* |
| 1171 | Maximum Correlation Coefficient. |
| 1172 | */ |
| 1173 | Q[z][y].direction[i].red+=cooccurrence[z][x].direction[i].red* |
| 1174 | cooccurrence[y][x].direction[i].red/density_x[z].direction[i].red/ |
| 1175 | density_y[x].direction[i].red; |
| 1176 | Q[z][y].direction[i].green+=cooccurrence[z][x].direction[i].green* |
| 1177 | cooccurrence[y][x].direction[i].green/ |
| 1178 | density_x[z].direction[i].green/density_y[x].direction[i].red; |
| 1179 | Q[z][y].direction[i].blue+=cooccurrence[z][x].direction[i].blue* |
| 1180 | cooccurrence[y][x].direction[i].blue/density_x[z].direction[i].blue/ |
| 1181 | density_y[x].direction[i].blue; |
| 1182 | if (image->matte != MagickFalse) |
| 1183 | Q[z][y].direction[i].opacity+= |
| 1184 | cooccurrence[z][x].direction[i].opacity* |
| 1185 | cooccurrence[y][x].direction[i].opacity/ |
| 1186 | density_x[z].direction[i].opacity/ |
| 1187 | density_y[x].direction[i].opacity; |
| 1188 | if (image->colorspace == CMYKColorspace) |
| 1189 | Q[z][y].direction[i].index+=cooccurrence[z][x].direction[i].index* |
| 1190 | cooccurrence[y][x].direction[i].index/ |
| 1191 | density_x[z].direction[i].index/density_y[x].direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1194 | channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red; |
| 1195 | channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green; |
| 1196 | channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1197 | if (image->matte != MagickFalse) |
| 1198 | channel_features[OpacityChannel].contrast[i]+=z*z* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1199 | pixel.direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1200 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1201 | channel_features[BlackChannel].contrast[i]+=z*z* |
| 1202 | pixel.direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1203 | } |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1204 | /* |
| 1205 | Maximum Correlation Coefficient. |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1206 | Future: return second largest eigenvalue of Q. |
cristy | e0acabf | 2010-01-30 00:52:38 +0000 | [diff] [blame] | 1207 | */ |
| 1208 | channel_features[RedChannel].maximum_correlation_coefficient[i]= |
| 1209 | sqrt((double) -1.0); |
| 1210 | channel_features[GreenChannel].maximum_correlation_coefficient[i]= |
| 1211 | sqrt((double) -1.0); |
| 1212 | channel_features[BlueChannel].maximum_correlation_coefficient[i]= |
| 1213 | sqrt((double) -1.0); |
| 1214 | if (image->matte != MagickFalse) |
| 1215 | channel_features[OpacityChannel].maximum_correlation_coefficient[i]= |
| 1216 | sqrt((double) -1.0); |
| 1217 | if (image->colorspace == CMYKColorspace) |
| 1218 | channel_features[IndexChannel].maximum_correlation_coefficient[i]= |
| 1219 | sqrt((double) -1.0); |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 1220 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 1221 | /* |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 1222 | Relinquish resources. |
| 1223 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1224 | sum=(ChannelStatistics *) RelinquishMagickMemory(sum); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1225 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | ffa10d0 | 2010-01-30 17:53:27 +0000 | [diff] [blame] | 1226 | Q[i]=(ChannelStatistics *) RelinquishMagickMemory(Q[i]); |
| 1227 | Q=(ChannelStatistics **) RelinquishMagickMemory(Q); |
| 1228 | density_y=(ChannelStatistics *) RelinquishMagickMemory(density_y); |
| 1229 | density_xy=(ChannelStatistics *) RelinquishMagickMemory(density_xy); |
| 1230 | density_x=(ChannelStatistics *) RelinquishMagickMemory(density_x); |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1231 | for (i=0; i < (ssize_t) number_grays; i++) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 1232 | cooccurrence[i]=(ChannelStatistics *) |
| 1233 | RelinquishMagickMemory(cooccurrence[i]); |
| 1234 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 1235 | return(channel_features); |
| 1236 | } |