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 | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 119 | % const unsigned long 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 | */ |
| 130 | MagickExport ChannelFeatures *GetImageChannelFeatures(const Image *image, |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 131 | const unsigned long distance,ExceptionInfo *exception) |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 132 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 133 | typedef struct _ChannelStatistics |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 134 | { |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 135 | DoublePixelPacket |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 136 | direction[4]; /* horizontal, vertical, left and right diagonals */ |
| 137 | } ChannelStatistics; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 138 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 139 | CacheView |
| 140 | *image_view; |
| 141 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 142 | ChannelFeatures |
| 143 | *channel_features; |
| 144 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 145 | ChannelStatistics |
| 146 | **cooccurrence, |
| 147 | correlation, |
| 148 | mean, |
| 149 | *sum, |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 150 | sum_squares, |
| 151 | variance; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 152 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 153 | LongPixelPacket |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 154 | gray, |
| 155 | *grays; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 156 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 157 | long |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 158 | y, |
| 159 | z; |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 160 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 161 | MagickBooleanType |
| 162 | status; |
| 163 | |
| 164 | register long |
| 165 | i; |
| 166 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 167 | size_t |
| 168 | length; |
| 169 | |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 170 | unsigned long |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 171 | number_grays; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 172 | |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 173 | assert(image != (Image *) NULL); |
| 174 | assert(image->signature == MagickSignature); |
| 175 | if (image->debug != MagickFalse) |
| 176 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 177 | if ((image->columns < (distance+1)) || (image->rows < (distance+1))) |
| 178 | return((ChannelFeatures *) NULL); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 179 | length=AllChannels+1UL; |
| 180 | channel_features=(ChannelFeatures *) AcquireQuantumMemory(length, |
| 181 | sizeof(*channel_features)); |
| 182 | if (channel_features == (ChannelFeatures *) NULL) |
| 183 | ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed"); |
| 184 | (void) ResetMagickMemory(channel_features,0,length* |
| 185 | sizeof(*channel_features)); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 186 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 187 | Form grays. |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 188 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 189 | grays=(LongPixelPacket *) AcquireQuantumMemory(MaxMap+1UL,sizeof(*grays)); |
| 190 | if (grays == (LongPixelPacket *) NULL) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 191 | { |
| 192 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 193 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 194 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 195 | channel_features); |
| 196 | return(channel_features); |
| 197 | } |
| 198 | for (i=0; i <= (long) MaxMap; i++) |
| 199 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 200 | grays[i].red=(~0UL); |
| 201 | grays[i].green=(~0UL); |
| 202 | grays[i].blue=(~0UL); |
| 203 | grays[i].opacity=(~0UL); |
| 204 | grays[i].index=(~0UL); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 205 | } |
| 206 | status=MagickTrue; |
| 207 | image_view=AcquireCacheView(image); |
| 208 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 209 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 210 | #endif |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 211 | for (y=0; y < (long) image->rows; y++) |
| 212 | { |
| 213 | register const IndexPacket |
| 214 | *restrict indexes; |
| 215 | |
| 216 | register const PixelPacket |
| 217 | *restrict p; |
| 218 | |
| 219 | register long |
| 220 | x; |
| 221 | |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 222 | if (status == MagickFalse) |
| 223 | continue; |
| 224 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 225 | if (p == (const PixelPacket *) NULL) |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 226 | { |
| 227 | status=MagickFalse; |
| 228 | continue; |
| 229 | } |
| 230 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 231 | for (x=0; x < (long) image->columns; x++) |
| 232 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 233 | grays[ScaleQuantumToMap(p->red)].red=ScaleQuantumToMap(p->red); |
| 234 | grays[ScaleQuantumToMap(p->green)].green=ScaleQuantumToMap(p->green); |
| 235 | grays[ScaleQuantumToMap(p->blue)].blue=ScaleQuantumToMap(p->blue); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 236 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 237 | grays[ScaleQuantumToMap(p->opacity)].opacity= |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 238 | ScaleQuantumToMap(p->opacity); |
| 239 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 240 | grays[ScaleQuantumToMap(indexes[x])].index= |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 241 | ScaleQuantumToMap(indexes[x]); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 242 | p++; |
| 243 | } |
| 244 | } |
cristy | 30c510a | 2010-01-24 03:43:00 +0000 | [diff] [blame] | 245 | image_view=DestroyCacheView(image_view); |
| 246 | if (status == MagickFalse) |
| 247 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 248 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | 30c510a | 2010-01-24 03:43:00 +0000 | [diff] [blame] | 249 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 250 | channel_features); |
| 251 | return(channel_features); |
| 252 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 253 | (void) ResetMagickMemory(&gray,0,sizeof(gray)); |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 254 | for (i=0; i <= (long) MaxMap; i++) |
| 255 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 256 | if (grays[i].red != ~0UL) |
| 257 | grays[gray.red++].red=grays[i].red; |
| 258 | if (grays[i].green != ~0UL) |
| 259 | grays[gray.green++].green=grays[i].green; |
| 260 | if (grays[i].blue != ~0UL) |
| 261 | grays[gray.blue++].blue=grays[i].blue; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 262 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 263 | if (grays[i].opacity != ~0UL) |
| 264 | grays[gray.opacity++].opacity=grays[i].opacity; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 265 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 266 | if (grays[i].index != ~0UL) |
| 267 | grays[gray.index++].index=grays[i].index; |
cristy | 2070fa5 | 2010-01-24 03:17:57 +0000 | [diff] [blame] | 268 | } |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 269 | /* |
| 270 | Allocate spatial dependence matrix. |
| 271 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 272 | number_grays=gray.red; |
| 273 | if (gray.green > number_grays) |
| 274 | number_grays=gray.green; |
| 275 | if (gray.blue > number_grays) |
| 276 | number_grays=gray.blue; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 277 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 278 | if (gray.opacity > number_grays) |
| 279 | number_grays=gray.opacity; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 280 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 281 | if (gray.index > number_grays) |
| 282 | number_grays=gray.index; |
| 283 | cooccurrence=(ChannelStatistics **) AcquireQuantumMemory(number_grays, |
| 284 | sizeof(*cooccurrence)); |
| 285 | if (cooccurrence == (ChannelStatistics **) NULL) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 286 | { |
| 287 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 288 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 289 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 290 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 291 | channel_features); |
| 292 | return(channel_features); |
| 293 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 294 | for (i=0; i < (long) number_grays; i++) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 295 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 296 | cooccurrence[i]=(ChannelStatistics *) AcquireQuantumMemory(number_grays, |
| 297 | sizeof(**cooccurrence)); |
| 298 | if (cooccurrence[i] == (ChannelStatistics *) NULL) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 299 | break; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 300 | (void) ResetMagickMemory(cooccurrence[i],0,number_grays* |
| 301 | sizeof(*cooccurrence)); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 302 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 303 | if (i < (long) number_grays) |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 304 | { |
| 305 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 306 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 307 | for (i--; i >= 0; i--) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 308 | cooccurrence[i]=(ChannelStatistics *) |
| 309 | RelinquishMagickMemory(cooccurrence[i]); |
| 310 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
| 311 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 312 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 313 | channel_features); |
| 314 | return(channel_features); |
| 315 | } |
| 316 | /* |
| 317 | Initialize spatial dependence matrix. |
| 318 | */ |
| 319 | status=MagickTrue; |
| 320 | image_view=AcquireCacheView(image); |
| 321 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 322 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 323 | #endif |
| 324 | for (y=0; y < (long) image->rows; y++) |
| 325 | { |
| 326 | long |
| 327 | u, |
| 328 | v; |
| 329 | |
| 330 | register const IndexPacket |
| 331 | *restrict indexes; |
| 332 | |
| 333 | register const PixelPacket |
| 334 | *restrict p; |
| 335 | |
| 336 | register long |
| 337 | x; |
| 338 | |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 339 | ssize_t |
| 340 | offset; |
| 341 | |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 342 | if (status == MagickFalse) |
| 343 | continue; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 344 | p=GetCacheViewVirtualPixels(image_view,-(long) distance,y,image->columns+ |
| 345 | 2*distance,distance+1,exception); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 346 | if (p == (const PixelPacket *) NULL) |
| 347 | { |
| 348 | status=MagickFalse; |
| 349 | continue; |
| 350 | } |
| 351 | indexes=GetCacheViewVirtualIndexQueue(image_view); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 352 | p+=distance; |
| 353 | indexes+=distance; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 354 | for (x=0; x < (long) image->columns; x++) |
| 355 | { |
| 356 | for (i=0; i < 4; i++) |
| 357 | { |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 358 | switch (i) |
| 359 | { |
| 360 | case 0: |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 361 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 362 | { |
| 363 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 364 | Horizontal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 365 | */ |
| 366 | offset=(ssize_t) distance; |
| 367 | break; |
| 368 | } |
| 369 | case 1: |
| 370 | { |
| 371 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 372 | Vertical adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 373 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 374 | offset=(ssize_t) (image->columns+2*distance); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 375 | break; |
| 376 | } |
| 377 | case 2: |
| 378 | { |
| 379 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 380 | Right diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 381 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 382 | offset=(ssize_t) (image->columns+2*distance)-distance; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 383 | break; |
| 384 | } |
| 385 | case 3: |
| 386 | { |
| 387 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 388 | Left diagonal adjacency. |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 389 | */ |
| 390 | offset=(ssize_t) (image->columns+2*distance)+distance; |
| 391 | break; |
| 392 | } |
| 393 | } |
| 394 | u=0; |
| 395 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 396 | while (grays[u].red != ScaleQuantumToMap(p->red)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 397 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 398 | while (grays[v].red != ScaleQuantumToMap((p+offset)->red)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 399 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 400 | cooccurrence[u][v].direction[i].red++; |
| 401 | cooccurrence[v][u].direction[i].red++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 402 | u=0; |
| 403 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 404 | while (grays[u].green != ScaleQuantumToMap(p->green)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 405 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 406 | while (grays[v].green != ScaleQuantumToMap((p+offset)->green)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 407 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 408 | cooccurrence[u][v].direction[i].green++; |
| 409 | cooccurrence[v][u].direction[i].green++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 410 | u=0; |
| 411 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 412 | while (grays[u].blue != ScaleQuantumToMap(p->blue)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 413 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 414 | while (grays[v].blue != ScaleQuantumToMap((p+offset)->blue)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 415 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 416 | cooccurrence[u][v].direction[i].blue++; |
| 417 | cooccurrence[v][u].direction[i].blue++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 418 | if (image->matte != MagickFalse) |
| 419 | { |
| 420 | u=0; |
| 421 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 422 | while (grays[u].opacity != ScaleQuantumToMap(p->opacity)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 423 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 424 | while (grays[v].opacity != ScaleQuantumToMap((p+offset)->opacity)) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 425 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 426 | cooccurrence[u][v].direction[i].opacity++; |
| 427 | cooccurrence[v][u].direction[i].opacity++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 428 | } |
| 429 | if (image->colorspace == CMYKColorspace) |
| 430 | { |
| 431 | u=0; |
| 432 | v=0; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 433 | while (grays[u].index != ScaleQuantumToMap(indexes[x])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 434 | u++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 435 | while (grays[v].index != ScaleQuantumToMap(indexes[x+offset])) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 436 | v++; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 437 | cooccurrence[u][v].direction[i].index++; |
| 438 | cooccurrence[v][u].direction[i].index++; |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 439 | } |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 440 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 441 | p++; |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | image_view=DestroyCacheView(image_view); |
| 445 | if (status == MagickFalse) |
| 446 | { |
| 447 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 448 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 449 | for (i=0; i < (long) number_grays; i++) |
| 450 | cooccurrence[i]=(ChannelStatistics *) |
| 451 | RelinquishMagickMemory(cooccurrence[i]); |
| 452 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
| 453 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 454 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 455 | channel_features); |
| 456 | return(channel_features); |
| 457 | } |
| 458 | /* |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 459 | Normalize spatial dependence matrix. |
| 460 | */ |
| 461 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 462 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 463 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 464 | for (i=0; i < 4; i++) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 465 | { |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 466 | double |
| 467 | normalize; |
| 468 | |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 469 | switch (i) |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 470 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 471 | case 0: |
| 472 | default: |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 473 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 474 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 475 | Horizontal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 476 | */ |
| 477 | normalize=2.0*image->rows*(image->columns-distance); |
| 478 | break; |
| 479 | } |
| 480 | case 1: |
| 481 | { |
| 482 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 483 | Vertical adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 484 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 485 | normalize=2.0*(image->rows-distance)*image->columns; |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | case 2: |
| 489 | { |
| 490 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 491 | Right diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 492 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 493 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 494 | break; |
| 495 | } |
| 496 | case 3: |
| 497 | { |
| 498 | /* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 499 | Left diagonal adjacency. |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 500 | */ |
| 501 | normalize=2.0*(image->rows-distance)*(image->columns-distance); |
| 502 | break; |
| 503 | } |
| 504 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 505 | for (y=0; y < (long) number_grays; y++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 506 | { |
| 507 | register long |
| 508 | x; |
| 509 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 510 | for (x=0; x < (long) number_grays; x++) |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 511 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 512 | cooccurrence[x][y].direction[i].red/=normalize; |
| 513 | cooccurrence[x][y].direction[i].green/=normalize; |
| 514 | cooccurrence[x][y].direction[i].blue/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 515 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 516 | cooccurrence[x][y].direction[i].opacity/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 517 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 518 | cooccurrence[x][y].direction[i].index/=normalize; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | } |
| 522 | /* |
| 523 | Compute texture features. |
| 524 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 525 | sum=(ChannelStatistics *) AcquireQuantumMemory(number_grays,sizeof(*sum)); |
| 526 | if (sum == (ChannelStatistics *) NULL) |
| 527 | { |
| 528 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 529 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 530 | for (i=0; i < (long) number_grays; i++) |
| 531 | cooccurrence[i]=(ChannelStatistics *) |
| 532 | RelinquishMagickMemory(cooccurrence[i]); |
| 533 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
| 534 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
| 535 | channel_features=(ChannelFeatures *) RelinquishMagickMemory( |
| 536 | channel_features); |
| 537 | return(channel_features); |
| 538 | } |
| 539 | (void) ResetMagickMemory(sum,0,number_grays*sizeof(*sum)); |
| 540 | (void) ResetMagickMemory(&correlation,0,sizeof(correlation)); |
| 541 | (void) ResetMagickMemory(&mean,0,sizeof(mean)); |
| 542 | (void) ResetMagickMemory(&sum_squares,0,sizeof(sum_squares)); |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 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 | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 547 | { |
| 548 | register long |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 549 | y; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 550 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 551 | for (y=0; y < (long) number_grays; y++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 552 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 553 | register long |
| 554 | x; |
| 555 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 556 | for (x=0; x < (long) number_grays; x++) |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 557 | { |
| 558 | /* |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 559 | Angular second moment: measure of homogeneity of the image. |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 560 | */ |
| 561 | channel_features[RedChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 562 | cooccurrence[x][y].direction[i].red* |
| 563 | cooccurrence[x][y].direction[i].red; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 564 | channel_features[GreenChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 565 | cooccurrence[x][y].direction[i].green* |
| 566 | cooccurrence[x][y].direction[i].green; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 567 | channel_features[BlueChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 568 | cooccurrence[x][y].direction[i].blue* |
| 569 | cooccurrence[x][y].direction[i].blue; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 570 | if (image->matte != MagickFalse) |
| 571 | channel_features[OpacityChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 572 | cooccurrence[x][y].direction[i].opacity* |
| 573 | cooccurrence[x][y].direction[i].opacity; |
cristy | 549a37e | 2010-01-26 15:24:15 +0000 | [diff] [blame] | 574 | if (image->colorspace == CMYKColorspace) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 575 | channel_features[BlackChannel].angular_second_moment[i]+= |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 576 | cooccurrence[x][y].direction[i].index* |
| 577 | cooccurrence[x][y].direction[i].index; |
| 578 | /* |
| 579 | Correlation: measure of linear-dependencies in the image. |
| 580 | */ |
| 581 | sum[y].direction[i].red+=cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 582 | sum[y].direction[i].green+=cooccurrence[x][y].direction[i].green; |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 583 | sum[y].direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
| 584 | if (image->matte != MagickFalse) |
| 585 | sum[y].direction[i].opacity+=cooccurrence[x][y].direction[i].opacity; |
| 586 | if (image->colorspace == CMYKColorspace) |
| 587 | sum[y].direction[i].index+=cooccurrence[x][y].direction[i].index; |
| 588 | correlation.direction[i].red+=x*y*cooccurrence[x][y].direction[i].red; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 589 | correlation.direction[i].green+=x*y* |
| 590 | cooccurrence[x][y].direction[i].green; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 591 | correlation.direction[i].blue+=x*y* |
| 592 | cooccurrence[x][y].direction[i].blue; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 593 | if (image->matte != MagickFalse) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 594 | correlation.direction[i].opacity+=x*y* |
| 595 | cooccurrence[x][y].direction[i].opacity; |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 596 | if (image->colorspace == CMYKColorspace) |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 597 | correlation.direction[i].index+=x*y* |
| 598 | cooccurrence[x][y].direction[i].index; |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 599 | /* |
| 600 | Inverse Difference Moment. |
| 601 | */ |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 602 | channel_features[RedChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 603 | cooccurrence[x][y].direction[i].red/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 604 | channel_features[GreenChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 605 | cooccurrence[x][y].direction[i].green/((y-x)*(y-x)+1); |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 606 | channel_features[BlueChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 607 | cooccurrence[x][y].direction[i].blue/((y-x)*(y-x)+1); |
| 608 | if (image->matte != MagickFalse) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 609 | channel_features[OpacityChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 610 | cooccurrence[x][y].direction[i].opacity/((y-x)*(y-x)+1); |
| 611 | if (image->colorspace == CMYKColorspace) |
cristy | 9fc6dbf | 2010-01-28 02:49:50 +0000 | [diff] [blame] | 612 | channel_features[IndexChannel].inverse_difference_moment[i]+= |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 613 | cooccurrence[x][y].direction[i].index/((y-x)*(y-x)+1); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 614 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 615 | mean.direction[i].red+=y*sum[y].direction[i].red; |
| 616 | sum_squares.direction[i].red+=y*y*sum[y].direction[i].red; |
| 617 | mean.direction[i].green+=y*sum[y].direction[i].green; |
| 618 | sum_squares.direction[i].green+=y*y*sum[y].direction[i].green; |
| 619 | mean.direction[i].blue+=y*sum[y].direction[i].blue; |
| 620 | sum_squares.direction[i].blue+=y*y*sum[y].direction[i].blue; |
| 621 | if (image->matte != MagickFalse) |
| 622 | { |
| 623 | mean.direction[i].opacity+=y*sum[y].direction[i].opacity; |
| 624 | sum_squares.direction[i].opacity+=y*y*sum[y].direction[i].opacity; |
| 625 | } |
| 626 | if (image->colorspace == CMYKColorspace) |
| 627 | { |
| 628 | mean.direction[i].index+=y*sum[y].direction[i].index; |
| 629 | sum_squares.direction[i].index+=y*y*sum[y].direction[i].index; |
| 630 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 631 | } |
cristy | cdf8e1b | 2010-01-28 01:52:33 +0000 | [diff] [blame] | 632 | /* |
| 633 | Correlation: measure of linear-dependencies in the image. |
| 634 | */ |
| 635 | channel_features[RedChannel].correlation[i]= |
| 636 | (correlation.direction[i].red-mean.direction[i].red* |
| 637 | mean.direction[i].red)/(sqrt(sum_squares.direction[i].red- |
| 638 | (mean.direction[i].red*mean.direction[i].red))*sqrt( |
| 639 | sum_squares.direction[i].red-(mean.direction[i].red* |
| 640 | mean.direction[i].red))); |
| 641 | channel_features[GreenChannel].correlation[i]= |
| 642 | (correlation.direction[i].green-mean.direction[i].green* |
| 643 | mean.direction[i].green)/(sqrt(sum_squares.direction[i].green- |
| 644 | (mean.direction[i].green*mean.direction[i].green))*sqrt( |
| 645 | sum_squares.direction[i].green-(mean.direction[i].green* |
| 646 | mean.direction[i].green))); |
| 647 | channel_features[BlueChannel].correlation[i]= |
| 648 | (correlation.direction[i].blue-mean.direction[i].blue* |
| 649 | mean.direction[i].blue)/(sqrt(sum_squares.direction[i].blue- |
| 650 | (mean.direction[i].blue*mean.direction[i].blue))*sqrt( |
| 651 | sum_squares.direction[i].blue-(mean.direction[i].blue* |
| 652 | mean.direction[i].blue))); |
| 653 | if (image->matte != MagickFalse) |
| 654 | channel_features[OpacityChannel].correlation[i]= |
| 655 | (correlation.direction[i].opacity-mean.direction[i].opacity* |
| 656 | mean.direction[i].opacity)/(sqrt(sum_squares.direction[i].opacity- |
| 657 | (mean.direction[i].opacity*mean.direction[i].opacity))*sqrt( |
| 658 | sum_squares.direction[i].opacity-(mean.direction[i].opacity* |
| 659 | mean.direction[i].opacity))); |
| 660 | if (image->colorspace == CMYKColorspace) |
| 661 | channel_features[IndexChannel].correlation[i]= |
| 662 | (correlation.direction[i].index-mean.direction[i].index* |
| 663 | mean.direction[i].index)/(sqrt(sum_squares.direction[i].index- |
| 664 | (mean.direction[i].index*mean.direction[i].index))*sqrt( |
| 665 | sum_squares.direction[i].index-(mean.direction[i].index* |
| 666 | mean.direction[i].index))); |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 667 | } |
cristy | cf5e649 | 2010-01-28 02:45:28 +0000 | [diff] [blame] | 668 | /* |
| 669 | Compute more texture features. |
| 670 | */ |
| 671 | (void) ResetMagickMemory(&variance,0,sizeof(variance)); |
| 672 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 673 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 674 | #endif |
| 675 | for (i=0; i < 4; i++) |
| 676 | { |
| 677 | register long |
| 678 | y; |
| 679 | |
| 680 | for (y=0; y < (long) number_grays; y++) |
| 681 | { |
| 682 | register long |
| 683 | x; |
| 684 | |
| 685 | for (x=0; x < (long) number_grays; x++) |
| 686 | { |
| 687 | /* |
| 688 | Sum of Squares: Variance |
| 689 | */ |
| 690 | variance.direction[i].red+=(y-mean.direction[i].red+1)* |
| 691 | (y-mean.direction[i].red+1)*cooccurrence[x][y].direction[i].red; |
| 692 | variance.direction[i].green+=(y-mean.direction[i].green+1)* |
| 693 | (y-mean.direction[i].green+1)*cooccurrence[x][y].direction[i].green; |
| 694 | variance.direction[i].blue+=(y-mean.direction[i].blue+1)* |
| 695 | (y-mean.direction[i].blue+1)*cooccurrence[x][y].direction[i].blue; |
| 696 | if (image->matte != MagickFalse) |
| 697 | variance.direction[i].opacity+=(y-mean.direction[i].opacity+1)* |
| 698 | (y-mean.direction[i].opacity+1)* |
| 699 | cooccurrence[x][y].direction[i].opacity; |
| 700 | if (image->colorspace == CMYKColorspace) |
| 701 | variance.direction[i].index+=(y-mean.direction[i].index+1)* |
| 702 | (y-mean.direction[i].index+1)*cooccurrence[x][y].direction[i].index; |
| 703 | } |
| 704 | } |
| 705 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 706 | variance.direction[i].red; |
| 707 | channel_features[GreenChannel].variance_sum_of_squares[i]= |
| 708 | variance.direction[i].green; |
| 709 | channel_features[BlueChannel].variance_sum_of_squares[i]= |
| 710 | variance.direction[i].blue; |
| 711 | if (image->matte != MagickFalse) |
| 712 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 713 | variance.direction[i].opacity; |
| 714 | if (image->colorspace == CMYKColorspace) |
| 715 | channel_features[RedChannel].variance_sum_of_squares[i]= |
| 716 | variance.direction[i].index; |
| 717 | } |
| 718 | /* |
| 719 | Compute more texture features. |
| 720 | */ |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 721 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| 722 | #pragma omp parallel for schedule(dynamic,4) shared(status) |
| 723 | #endif |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 724 | for (i=0; i < 4; i++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 725 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 726 | for (z=0; z < (long) number_grays; z++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 727 | { |
| 728 | register long |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 729 | y; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 730 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 731 | ChannelStatistics |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 732 | pixel; |
| 733 | |
| 734 | (void) ResetMagickMemory(&pixel,0,sizeof(pixel)); |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 735 | for (y=0; y < (long) number_grays; y++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 736 | { |
cristy | bd82207 | 2010-01-27 00:30:00 +0000 | [diff] [blame] | 737 | register long |
| 738 | x; |
| 739 | |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 740 | for (x=0; x < (long) number_grays; x++) |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 741 | { |
| 742 | /* |
| 743 | Contrast: amount of local variations present in an image. |
| 744 | */ |
| 745 | if (((y-x) == z) || ((x-y) == z)) |
| 746 | { |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 747 | pixel.direction[i].red+=cooccurrence[x][y].direction[i].red; |
| 748 | pixel.direction[i].green+=cooccurrence[x][y].direction[i].green; |
| 749 | pixel.direction[i].blue+=cooccurrence[x][y].direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 750 | if (image->matte != MagickFalse) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 751 | pixel.direction[i].opacity+= |
| 752 | cooccurrence[x][y].direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 753 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 754 | pixel.direction[i].index+=cooccurrence[x][y].direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 755 | } |
| 756 | } |
| 757 | } |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 758 | channel_features[RedChannel].contrast[i]+=z*z*pixel.direction[i].red; |
| 759 | channel_features[GreenChannel].contrast[i]+=z*z*pixel.direction[i].green; |
| 760 | channel_features[BlueChannel].contrast[i]+=z*z*pixel.direction[i].blue; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 761 | if (image->matte != MagickFalse) |
| 762 | channel_features[OpacityChannel].contrast[i]+=z*z* |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 763 | pixel.direction[i].opacity; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 764 | if (image->colorspace == CMYKColorspace) |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 765 | channel_features[BlackChannel].contrast[i]+=z*z* |
| 766 | pixel.direction[i].index; |
cristy | 3a82f25 | 2010-01-26 20:31:51 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
cristy | 7e9726d | 2010-01-26 02:08:40 +0000 | [diff] [blame] | 769 | /* |
cristy | f2bf2c7 | 2010-01-25 19:54:15 +0000 | [diff] [blame] | 770 | Relinquish resources. |
| 771 | */ |
cristy | 7396d88 | 2010-01-27 02:37:56 +0000 | [diff] [blame] | 772 | sum=(ChannelStatistics *) RelinquishMagickMemory(sum); |
| 773 | for (i=0; i < (long) number_grays; i++) |
| 774 | cooccurrence[i]=(ChannelStatistics *) |
| 775 | RelinquishMagickMemory(cooccurrence[i]); |
| 776 | cooccurrence=(ChannelStatistics **) RelinquishMagickMemory(cooccurrence); |
| 777 | grays=(LongPixelPacket *) RelinquishMagickMemory(grays); |
cristy | 3e2860c | 2010-01-24 01:36:30 +0000 | [diff] [blame] | 778 | return(channel_features); |
| 779 | } |