| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % SSSSS EEEEE GGGG M M EEEEE N N TTTTT % |
| 7 | % SS E G MM MM E NN N T % |
| 8 | % SSS EEE G GGG M M M EEE N N N T % |
| 9 | % SS E G G M M E N NN T % |
| 10 | % SSSSS EEEEE GGGG M M EEEEE N N T % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Methods to Segment an Image with Thresholding Fuzzy c-Means % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % John Cristy % |
| 17 | % April 1993 % |
| 18 | % % |
| 19 | % % |
| cristy | 45ef08f | 2012-12-07 13:13:34 +0000 | [diff] [blame] | 20 | % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +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 | % Segment segments an image by analyzing the histograms of the color |
| 37 | % components and identifying units that are homogeneous with the fuzzy |
| 38 | % c-means technique. The scale-space filter analyzes the histograms of |
| 39 | % the three color components of the image and identifies a set of |
| 40 | % classes. The extents of each class is used to coarsely segment the |
| 41 | % image with thresholding. The color associated with each class is |
| 42 | % determined by the mean color of all pixels within the extents of a |
| 43 | % particular class. Finally, any unclassified pixels are assigned to |
| 44 | % the closest class with the fuzzy c-means technique. |
| 45 | % |
| 46 | % The fuzzy c-Means algorithm can be summarized as follows: |
| 47 | % |
| 48 | % o Build a histogram, one for each color component of the image. |
| 49 | % |
| 50 | % o For each histogram, successively apply the scale-space filter and |
| 51 | % build an interval tree of zero crossings in the second derivative |
| anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 52 | % at each scale. Analyze this scale-space ''fingerprint'' to |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 53 | % determine which peaks and valleys in the histogram are most |
| 54 | % predominant. |
| 55 | % |
| 56 | % o The fingerprint defines intervals on the axis of the histogram. |
| 57 | % Each interval contains either a minima or a maxima in the original |
| 58 | % signal. If each color component lies within the maxima interval, |
| anthony | e5b3965 | 2012-04-21 05:37:29 +0000 | [diff] [blame] | 59 | % that pixel is considered ''classified'' and is assigned an unique |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | % class number. |
| 61 | % |
| 62 | % o Any pixel that fails to be classified in the above thresholding |
| 63 | % pass is classified using the fuzzy c-Means technique. It is |
| 64 | % assigned to one of the classes discovered in the histogram analysis |
| 65 | % phase. |
| 66 | % |
| 67 | % The fuzzy c-Means technique attempts to cluster a pixel by finding |
| 68 | % the local minima of the generalized within group sum of squared error |
| 69 | % objective function. A pixel is assigned to the closest class of |
| 70 | % which the fuzzy membership has a maximum value. |
| 71 | % |
| 72 | % Segment is strongly based on software written by Andy Gallo, |
| 73 | % University of Delaware. |
| 74 | % |
| 75 | % The following reference was used in creating this program: |
| 76 | % |
| 77 | % Young Won Lim, Sang Uk Lee, "On The Color Image Segmentation |
| 78 | % Algorithm Based on the Thresholding and the Fuzzy c-Means |
| 79 | % Techniques", Pattern Recognition, Volume 23, Number 9, pages |
| 80 | % 935-952, 1990. |
| 81 | % |
| 82 | % |
| 83 | */ |
| 84 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 85 | #include "MagickCore/studio.h" |
| 86 | #include "MagickCore/cache.h" |
| 87 | #include "MagickCore/color.h" |
| 88 | #include "MagickCore/colormap.h" |
| 89 | #include "MagickCore/colorspace.h" |
| cristy | 6a56612 | 2011-07-07 00:12:37 +0000 | [diff] [blame] | 90 | #include "MagickCore/colorspace-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 91 | #include "MagickCore/exception.h" |
| 92 | #include "MagickCore/exception-private.h" |
| 93 | #include "MagickCore/image.h" |
| 94 | #include "MagickCore/image-private.h" |
| 95 | #include "MagickCore/memory_.h" |
| 96 | #include "MagickCore/monitor.h" |
| 97 | #include "MagickCore/monitor-private.h" |
| 98 | #include "MagickCore/pixel-accessor.h" |
| cristy | 35f1530 | 2012-06-07 14:59:02 +0000 | [diff] [blame] | 99 | #include "MagickCore/pixel-private.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 100 | #include "MagickCore/quantize.h" |
| 101 | #include "MagickCore/quantum.h" |
| 102 | #include "MagickCore/quantum-private.h" |
| cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 103 | #include "MagickCore/resource_.h" |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 104 | #include "MagickCore/segment.h" |
| 105 | #include "MagickCore/string_.h" |
| cristy | 16881e6 | 2012-05-06 14:41:29 +0000 | [diff] [blame] | 106 | #include "MagickCore/thread-private.h" |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | Define declarations. |
| 110 | */ |
| 111 | #define MaxDimension 3 |
| 112 | #define DeltaTau 0.5f |
| 113 | #if defined(FastClassify) |
| 114 | #define WeightingExponent 2.0 |
| 115 | #define SegmentPower(ratio) (ratio) |
| 116 | #else |
| 117 | #define WeightingExponent 2.5 |
| 118 | #define SegmentPower(ratio) pow(ratio,(double) (1.0/(weighting_exponent-1.0))); |
| 119 | #endif |
| 120 | #define Tau 5.2f |
| 121 | |
| 122 | /* |
| 123 | Typedef declarations. |
| 124 | */ |
| 125 | typedef struct _ExtentPacket |
| 126 | { |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 127 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 128 | center; |
| 129 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 130 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 131 | index, |
| 132 | left, |
| 133 | right; |
| 134 | } ExtentPacket; |
| 135 | |
| 136 | typedef struct _Cluster |
| 137 | { |
| 138 | struct _Cluster |
| 139 | *next; |
| 140 | |
| 141 | ExtentPacket |
| 142 | red, |
| 143 | green, |
| 144 | blue; |
| 145 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 146 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 147 | count, |
| 148 | id; |
| 149 | } Cluster; |
| 150 | |
| 151 | typedef struct _IntervalTree |
| 152 | { |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 153 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 154 | tau; |
| 155 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 156 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 157 | left, |
| 158 | right; |
| 159 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 160 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 161 | mean_stability, |
| 162 | stability; |
| 163 | |
| 164 | struct _IntervalTree |
| 165 | *sibling, |
| 166 | *child; |
| 167 | } IntervalTree; |
| 168 | |
| 169 | typedef struct _ZeroCrossing |
| 170 | { |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 171 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 172 | tau, |
| 173 | histogram[256]; |
| 174 | |
| 175 | short |
| 176 | crossings[256]; |
| 177 | } ZeroCrossing; |
| 178 | |
| 179 | /* |
| 180 | Constant declarations. |
| 181 | */ |
| 182 | static const int |
| 183 | Blue = 2, |
| 184 | Green = 1, |
| 185 | Red = 0, |
| 186 | SafeMargin = 3, |
| 187 | TreeLength = 600; |
| 188 | |
| 189 | /* |
| 190 | Method prototypes. |
| 191 | */ |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 192 | static double |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 193 | OptimalTau(const ssize_t *,const double,const double,const double, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 194 | const double,short *); |
| 195 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 196 | static ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | DefineRegion(const short *,ExtentPacket *); |
| 198 | |
| 199 | static void |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 200 | InitializeHistogram(const Image *,ssize_t **,ExceptionInfo *), |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 201 | ScaleSpace(const ssize_t *,const double,double *), |
| 202 | ZeroCrossHistogram(double *,const double,short *); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 206 | % % |
| 207 | % % |
| 208 | % % |
| 209 | + C l a s s i f y % |
| 210 | % % |
| 211 | % % |
| 212 | % % |
| 213 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 214 | % |
| 215 | % Classify() defines one or more classes. Each pixel is thresholded to |
| cristy | 33c5302 | 2010-06-25 12:17:27 +0000 | [diff] [blame] | 216 | % determine which class it belongs to. If the class is not identified it is |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | % assigned to the closest class based on the fuzzy c-Means technique. |
| 218 | % |
| 219 | % The format of the Classify method is: |
| 220 | % |
| 221 | % MagickBooleanType Classify(Image *image,short **extrema, |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 222 | % const double cluster_threshold, |
| 223 | % const double weighting_exponent, |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 224 | % const MagickBooleanType verbose,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 225 | % |
| 226 | % A description of each parameter follows. |
| 227 | % |
| 228 | % o image: the image. |
| 229 | % |
| 230 | % o extrema: Specifies a pointer to an array of integers. They |
| 231 | % represent the peaks and valleys of the histogram for each color |
| 232 | % component. |
| 233 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 234 | % o cluster_threshold: This double represents the minimum number of |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 235 | % pixels contained in a hexahedra before it can be considered valid |
| 236 | % (expressed as a percentage). |
| 237 | % |
| 238 | % o weighting_exponent: Specifies the membership weighting exponent. |
| 239 | % |
| 240 | % o verbose: A value greater than zero prints detailed information about |
| 241 | % the identified classes. |
| 242 | % |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 243 | % o exception: return any errors or warnings in this structure. |
| 244 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 245 | */ |
| 246 | static MagickBooleanType Classify(Image *image,short **extrema, |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 247 | const double cluster_threshold, |
| 248 | const double weighting_exponent,const MagickBooleanType verbose, |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 249 | ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 250 | { |
| 251 | #define SegmentImageTag "Segment/Image" |
| 252 | |
| cristy | c4c8d13 | 2010-01-07 01:58:38 +0000 | [diff] [blame] | 253 | CacheView |
| 254 | *image_view; |
| 255 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 256 | Cluster |
| 257 | *cluster, |
| 258 | *head, |
| 259 | *last_cluster, |
| 260 | *next_cluster; |
| 261 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 262 | ExtentPacket |
| 263 | blue, |
| 264 | green, |
| 265 | red; |
| 266 | |
| cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 267 | MagickOffsetType |
| 268 | progress; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 269 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 270 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 271 | *free_squares; |
| 272 | |
| 273 | MagickStatusType |
| 274 | status; |
| 275 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 276 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 277 | i; |
| 278 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 279 | register double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 280 | *squares; |
| 281 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 282 | size_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 283 | number_clusters; |
| 284 | |
| cristy | 5f95947 | 2010-05-27 22:19:46 +0000 | [diff] [blame] | 285 | ssize_t |
| 286 | count, |
| 287 | y; |
| 288 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 289 | /* |
| 290 | Form clusters. |
| 291 | */ |
| 292 | cluster=(Cluster *) NULL; |
| 293 | head=(Cluster *) NULL; |
| 294 | (void) ResetMagickMemory(&red,0,sizeof(red)); |
| 295 | (void) ResetMagickMemory(&green,0,sizeof(green)); |
| 296 | (void) ResetMagickMemory(&blue,0,sizeof(blue)); |
| 297 | while (DefineRegion(extrema[Red],&red) != 0) |
| 298 | { |
| 299 | green.index=0; |
| 300 | while (DefineRegion(extrema[Green],&green) != 0) |
| 301 | { |
| 302 | blue.index=0; |
| 303 | while (DefineRegion(extrema[Blue],&blue) != 0) |
| 304 | { |
| 305 | /* |
| 306 | Allocate a new class. |
| 307 | */ |
| 308 | if (head != (Cluster *) NULL) |
| 309 | { |
| 310 | cluster->next=(Cluster *) AcquireMagickMemory( |
| 311 | sizeof(*cluster->next)); |
| 312 | cluster=cluster->next; |
| 313 | } |
| 314 | else |
| 315 | { |
| cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 316 | cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 317 | head=cluster; |
| 318 | } |
| 319 | if (cluster == (Cluster *) NULL) |
| 320 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 321 | image->filename); |
| 322 | /* |
| 323 | Initialize a new class. |
| 324 | */ |
| 325 | cluster->count=0; |
| 326 | cluster->red=red; |
| 327 | cluster->green=green; |
| 328 | cluster->blue=blue; |
| 329 | cluster->next=(Cluster *) NULL; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | if (head == (Cluster *) NULL) |
| 334 | { |
| 335 | /* |
| 336 | No classes were identified-- create one. |
| 337 | */ |
| cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 338 | cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 339 | if (cluster == (Cluster *) NULL) |
| 340 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 341 | image->filename); |
| 342 | /* |
| 343 | Initialize a new class. |
| 344 | */ |
| 345 | cluster->count=0; |
| 346 | cluster->red=red; |
| 347 | cluster->green=green; |
| 348 | cluster->blue=blue; |
| 349 | cluster->next=(Cluster *) NULL; |
| 350 | head=cluster; |
| 351 | } |
| 352 | /* |
| 353 | Count the pixels for each cluster. |
| 354 | */ |
| 355 | status=MagickTrue; |
| 356 | count=0; |
| 357 | progress=0; |
| cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 358 | image_view=AcquireVirtualCacheView(image,exception); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 359 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 360 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 361 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 362 | *p; |
| 363 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 364 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 365 | x; |
| 366 | |
| 367 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 368 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 369 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 370 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 371 | { |
| 372 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 373 | if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | (cluster->red.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 375 | ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 376 | (cluster->red.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 377 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 378 | (cluster->green.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 379 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 380 | (cluster->green.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 381 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 382 | (cluster->blue.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 383 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 384 | (cluster->blue.right+SafeMargin))) |
| 385 | { |
| 386 | /* |
| 387 | Count this pixel. |
| 388 | */ |
| 389 | count++; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 390 | cluster->red.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 391 | GetPixelRed(image,p)); |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 392 | cluster->green.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 393 | GetPixelGreen(image,p)); |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 394 | cluster->blue.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 395 | GetPixelBlue(image,p)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | cluster->count++; |
| 397 | break; |
| 398 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 399 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 400 | } |
| 401 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 402 | { |
| 403 | MagickBooleanType |
| 404 | proceed; |
| 405 | |
| cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 406 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 407 | #pragma omp critical (MagickCore_Classify) |
| 408 | #endif |
| cristy | 78778cb | 2012-01-17 14:48:47 +0000 | [diff] [blame] | 409 | proceed=SetImageProgress(image,SegmentImageTag,progress++,2* |
| 410 | image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 411 | if (proceed == MagickFalse) |
| 412 | status=MagickFalse; |
| 413 | } |
| 414 | } |
| 415 | image_view=DestroyCacheView(image_view); |
| 416 | /* |
| 417 | Remove clusters that do not meet minimum cluster threshold. |
| 418 | */ |
| 419 | count=0; |
| 420 | last_cluster=head; |
| 421 | next_cluster=head; |
| 422 | for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster) |
| 423 | { |
| 424 | next_cluster=cluster->next; |
| 425 | if ((cluster->count > 0) && |
| 426 | (cluster->count >= (count*cluster_threshold/100.0))) |
| 427 | { |
| 428 | /* |
| 429 | Initialize cluster. |
| 430 | */ |
| 431 | cluster->id=count; |
| 432 | cluster->red.center/=cluster->count; |
| 433 | cluster->green.center/=cluster->count; |
| 434 | cluster->blue.center/=cluster->count; |
| 435 | count++; |
| 436 | last_cluster=cluster; |
| 437 | continue; |
| 438 | } |
| 439 | /* |
| 440 | Delete cluster. |
| 441 | */ |
| 442 | if (cluster == head) |
| 443 | head=next_cluster; |
| 444 | else |
| 445 | last_cluster->next=next_cluster; |
| 446 | cluster=(Cluster *) RelinquishMagickMemory(cluster); |
| 447 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 448 | number_clusters=(size_t) count; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 449 | if (verbose != MagickFalse) |
| 450 | { |
| 451 | /* |
| 452 | Print cluster statistics. |
| 453 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 454 | (void) FormatLocaleFile(stdout,"Fuzzy C-means Statistics\n"); |
| 455 | (void) FormatLocaleFile(stdout,"===================\n\n"); |
| 456 | (void) FormatLocaleFile(stdout,"\tCluster Threshold = %g\n",(double) |
| cristy | 4f3c0be | 2009-09-12 16:04:05 +0000 | [diff] [blame] | 457 | cluster_threshold); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 458 | (void) FormatLocaleFile(stdout,"\tWeighting Exponent = %g\n",(double) |
| cristy | 4f3c0be | 2009-09-12 16:04:05 +0000 | [diff] [blame] | 459 | weighting_exponent); |
| cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 460 | (void) FormatLocaleFile(stdout,"\tTotal Number of Clusters = %.20g\n\n", |
| 461 | (double) number_clusters); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 462 | /* |
| 463 | Print the total number of points per cluster. |
| 464 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 465 | (void) FormatLocaleFile(stdout,"\n\nNumber of Vectors Per Cluster\n"); |
| 466 | (void) FormatLocaleFile(stdout,"=============================\n\n"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 467 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 468 | (void) FormatLocaleFile(stdout,"Cluster #%.20g = %.20g\n",(double) |
| 469 | cluster->id,(double) cluster->count); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 470 | /* |
| 471 | Print the cluster extents. |
| 472 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 473 | (void) FormatLocaleFile(stdout, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 474 | "\n\n\nCluster Extents: (Vector Size: %d)\n",MaxDimension); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 475 | (void) FormatLocaleFile(stdout,"================"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 476 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| 477 | { |
| cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 478 | (void) FormatLocaleFile(stdout,"\n\nCluster #%.20g\n\n",(double) |
| 479 | cluster->id); |
| 480 | (void) FormatLocaleFile(stdout, |
| 481 | "%.20g-%.20g %.20g-%.20g %.20g-%.20g\n",(double) |
| cristy | e8c25f9 | 2010-06-03 00:53:06 +0000 | [diff] [blame] | 482 | cluster->red.left,(double) cluster->red.right,(double) |
| 483 | cluster->green.left,(double) cluster->green.right,(double) |
| 484 | cluster->blue.left,(double) cluster->blue.right); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 485 | } |
| 486 | /* |
| 487 | Print the cluster center values. |
| 488 | */ |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 489 | (void) FormatLocaleFile(stdout, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 490 | "\n\n\nCluster Center Values: (Vector Size: %d)\n",MaxDimension); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 491 | (void) FormatLocaleFile(stdout,"====================="); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 492 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| 493 | { |
| cristy | 1e60481 | 2011-05-19 18:07:50 +0000 | [diff] [blame] | 494 | (void) FormatLocaleFile(stdout,"\n\nCluster #%.20g\n\n",(double) |
| 495 | cluster->id); |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 496 | (void) FormatLocaleFile(stdout,"%g %g %g\n",(double) |
| cristy | 8cd5b31 | 2010-01-07 01:10:24 +0000 | [diff] [blame] | 497 | cluster->red.center,(double) cluster->green.center,(double) |
| 498 | cluster->blue.center); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 499 | } |
| cristy | b51dff5 | 2011-05-19 16:55:47 +0000 | [diff] [blame] | 500 | (void) FormatLocaleFile(stdout,"\n"); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 501 | } |
| 502 | if (number_clusters > 256) |
| 503 | ThrowBinaryException(ImageError,"TooManyClusters",image->filename); |
| 504 | /* |
| 505 | Speed up distance calculations. |
| 506 | */ |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 507 | squares=(double *) AcquireQuantumMemory(513UL,sizeof(*squares)); |
| 508 | if (squares == (double *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 509 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 510 | image->filename); |
| 511 | squares+=255; |
| 512 | for (i=(-255); i <= 255; i++) |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 513 | squares[i]=(double) i*(double) i; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | /* |
| 515 | Allocate image colormap. |
| 516 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 517 | if (AcquireImageColormap(image,number_clusters,exception) == MagickFalse) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 518 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 519 | image->filename); |
| 520 | i=0; |
| 521 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| 522 | { |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 523 | image->colormap[i].red=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | (cluster->red.center+0.5)); |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 525 | image->colormap[i].green=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 526 | (cluster->green.center+0.5)); |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 527 | image->colormap[i].blue=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 528 | (cluster->blue.center+0.5)); |
| 529 | i++; |
| 530 | } |
| 531 | /* |
| 532 | Do course grain classes. |
| 533 | */ |
| cristy | 46ff267 | 2012-12-14 15:32:26 +0000 | [diff] [blame] | 534 | image_view=AcquireAuthenticCacheView(image,exception); |
| cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 535 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| cristy | ac245f8 | 2012-05-05 17:13:57 +0000 | [diff] [blame] | 536 | #pragma omp parallel for schedule(static,4) shared(progress,status) \ |
| cristy | 5e6b259 | 2012-12-19 14:08:11 +0000 | [diff] [blame] | 537 | magick_threads(image,image,image->rows,1) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 538 | #endif |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 539 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 540 | { |
| 541 | Cluster |
| 542 | *cluster; |
| 543 | |
| cristy | 101ab70 | 2011-10-13 13:06:32 +0000 | [diff] [blame] | 544 | register const PixelInfo |
| cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 545 | *restrict p; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 547 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 548 | x; |
| 549 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 550 | register Quantum |
| cristy | c47d1f8 | 2009-11-26 01:44:43 +0000 | [diff] [blame] | 551 | *restrict q; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 552 | |
| 553 | if (status == MagickFalse) |
| 554 | continue; |
| 555 | q=GetCacheViewAuthenticPixels(image_view,0,y,image->columns,1,exception); |
| cristy | acd2ed2 | 2011-08-30 01:44:23 +0000 | [diff] [blame] | 556 | if (q == (Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 557 | { |
| 558 | status=MagickFalse; |
| 559 | continue; |
| 560 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 561 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 562 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 563 | SetPixelIndex(image,0,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 564 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| 565 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 566 | if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,q)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 567 | (cluster->red.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 568 | ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,q)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | (cluster->red.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 570 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,q)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 571 | (cluster->green.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 572 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,q)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 573 | (cluster->green.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 574 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,q)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | (cluster->blue.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 576 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,q)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 577 | (cluster->blue.right+SafeMargin))) |
| 578 | { |
| 579 | /* |
| 580 | Classify this pixel. |
| 581 | */ |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 582 | SetPixelIndex(image,(Quantum) cluster->id,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | } |
| 586 | if (cluster == (Cluster *) NULL) |
| 587 | { |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 588 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 589 | distance_squared, |
| 590 | local_minima, |
| 591 | numerator, |
| 592 | ratio, |
| 593 | sum; |
| 594 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 595 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 596 | j, |
| 597 | k; |
| 598 | |
| 599 | /* |
| 600 | Compute fuzzy membership. |
| 601 | */ |
| 602 | local_minima=0.0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 603 | for (j=0; j < (ssize_t) image->colors; j++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 604 | { |
| 605 | sum=0.0; |
| 606 | p=image->colormap+j; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 607 | distance_squared=squares[(ssize_t) ScaleQuantumToChar( |
| 608 | GetPixelRed(image,q))-(ssize_t) |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 609 | ScaleQuantumToChar(ClampToQuantum(p->red))]+squares[(ssize_t) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 610 | ScaleQuantumToChar(GetPixelGreen(image,q))-(ssize_t) |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 611 | ScaleQuantumToChar(ClampToQuantum(p->green))]+squares[(ssize_t) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 612 | ScaleQuantumToChar(GetPixelBlue(image,q))-(ssize_t) |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 613 | ScaleQuantumToChar(ClampToQuantum(p->blue))]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 614 | numerator=distance_squared; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 615 | for (k=0; k < (ssize_t) image->colors; k++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 616 | { |
| 617 | p=image->colormap+k; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 618 | distance_squared=squares[(ssize_t) ScaleQuantumToChar( |
| 619 | GetPixelRed(image,q))-(ssize_t) |
| cristy | e42f658 | 2012-02-11 17:59:50 +0000 | [diff] [blame] | 620 | ScaleQuantumToChar(ClampToQuantum(p->red))]+squares[ |
| 621 | (ssize_t) ScaleQuantumToChar(GetPixelGreen(image,q))-(ssize_t) |
| 622 | ScaleQuantumToChar(ClampToQuantum(p->green))]+squares[ |
| 623 | (ssize_t) ScaleQuantumToChar(GetPixelBlue(image,q))-(ssize_t) |
| 624 | ScaleQuantumToChar(ClampToQuantum(p->blue))]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 625 | ratio=numerator/distance_squared; |
| 626 | sum+=SegmentPower(ratio); |
| 627 | } |
| 628 | if ((sum != 0.0) && ((1.0/sum) > local_minima)) |
| 629 | { |
| 630 | /* |
| 631 | Classify this pixel. |
| 632 | */ |
| cristy | b519e20 | 2012-06-07 15:29:02 +0000 | [diff] [blame] | 633 | local_minima=1.0/sum; |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 634 | SetPixelIndex(image,(Quantum) j,q); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 638 | q+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 639 | } |
| 640 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 641 | status=MagickFalse; |
| 642 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 643 | { |
| 644 | MagickBooleanType |
| 645 | proceed; |
| 646 | |
| cristy | b5d5f72 | 2009-11-04 03:03:49 +0000 | [diff] [blame] | 647 | #if defined(MAGICKCORE_OPENMP_SUPPORT) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 648 | #pragma omp critical (MagickCore_Classify) |
| 649 | #endif |
| 650 | proceed=SetImageProgress(image,SegmentImageTag,progress++, |
| 651 | 2*image->rows); |
| 652 | if (proceed == MagickFalse) |
| 653 | status=MagickFalse; |
| 654 | } |
| 655 | } |
| 656 | image_view=DestroyCacheView(image_view); |
| cristy | ea1a8aa | 2011-10-20 13:24:06 +0000 | [diff] [blame] | 657 | status&=SyncImage(image,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 658 | /* |
| 659 | Relinquish resources. |
| 660 | */ |
| 661 | for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster) |
| 662 | { |
| 663 | next_cluster=cluster->next; |
| 664 | cluster=(Cluster *) RelinquishMagickMemory(cluster); |
| 665 | } |
| 666 | squares-=255; |
| 667 | free_squares=squares; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 668 | free_squares=(double *) RelinquishMagickMemory(free_squares); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 669 | return(MagickTrue); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 674 | % % |
| 675 | % % |
| 676 | % % |
| 677 | + C o n s o l i d a t e C r o s s i n g s % |
| 678 | % % |
| 679 | % % |
| 680 | % % |
| 681 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 682 | % |
| 683 | % ConsolidateCrossings() guarantees that an even number of zero crossings |
| 684 | % always lie between two crossings. |
| 685 | % |
| 686 | % The format of the ConsolidateCrossings method is: |
| 687 | % |
| 688 | % ConsolidateCrossings(ZeroCrossing *zero_crossing, |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 689 | % const size_t number_crossings) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 690 | % |
| 691 | % A description of each parameter follows. |
| 692 | % |
| 693 | % o zero_crossing: Specifies an array of structures of type ZeroCrossing. |
| 694 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 695 | % o number_crossings: This size_t specifies the number of elements |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 696 | % in the zero_crossing array. |
| 697 | % |
| 698 | */ |
| 699 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 700 | static inline ssize_t MagickAbsoluteValue(const ssize_t x) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 701 | { |
| 702 | if (x < 0) |
| 703 | return(-x); |
| 704 | return(x); |
| 705 | } |
| 706 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 707 | static inline ssize_t MagickMax(const ssize_t x,const ssize_t y) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 708 | { |
| 709 | if (x > y) |
| 710 | return(x); |
| 711 | return(y); |
| 712 | } |
| 713 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 714 | static inline ssize_t MagickMin(const ssize_t x,const ssize_t y) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 715 | { |
| 716 | if (x < y) |
| 717 | return(x); |
| 718 | return(y); |
| 719 | } |
| 720 | |
| 721 | static void ConsolidateCrossings(ZeroCrossing *zero_crossing, |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 722 | const size_t number_crossings) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 723 | { |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 724 | register ssize_t |
| 725 | i, |
| 726 | j, |
| 727 | k, |
| 728 | l; |
| 729 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 730 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 731 | center, |
| 732 | correct, |
| 733 | count, |
| 734 | left, |
| 735 | right; |
| 736 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 737 | /* |
| 738 | Consolidate zero crossings. |
| 739 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 740 | for (i=(ssize_t) number_crossings-1; i >= 0; i--) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 741 | for (j=0; j <= 255; j++) |
| 742 | { |
| 743 | if (zero_crossing[i].crossings[j] == 0) |
| 744 | continue; |
| 745 | /* |
| 746 | Find the entry that is closest to j and still preserves the |
| 747 | property that there are an even number of crossings between |
| 748 | intervals. |
| 749 | */ |
| 750 | for (k=j-1; k > 0; k--) |
| 751 | if (zero_crossing[i+1].crossings[k] != 0) |
| 752 | break; |
| 753 | left=MagickMax(k,0); |
| 754 | center=j; |
| 755 | for (k=j+1; k < 255; k++) |
| 756 | if (zero_crossing[i+1].crossings[k] != 0) |
| 757 | break; |
| 758 | right=MagickMin(k,255); |
| 759 | /* |
| 760 | K is the zero crossing just left of j. |
| 761 | */ |
| 762 | for (k=j-1; k > 0; k--) |
| 763 | if (zero_crossing[i].crossings[k] != 0) |
| 764 | break; |
| 765 | if (k < 0) |
| 766 | k=0; |
| 767 | /* |
| 768 | Check center for an even number of crossings between k and j. |
| 769 | */ |
| 770 | correct=(-1); |
| 771 | if (zero_crossing[i+1].crossings[j] != 0) |
| 772 | { |
| 773 | count=0; |
| 774 | for (l=k+1; l < center; l++) |
| 775 | if (zero_crossing[i+1].crossings[l] != 0) |
| 776 | count++; |
| 777 | if (((count % 2) == 0) && (center != k)) |
| 778 | correct=center; |
| 779 | } |
| 780 | /* |
| 781 | Check left for an even number of crossings between k and j. |
| 782 | */ |
| 783 | if (correct == -1) |
| 784 | { |
| 785 | count=0; |
| 786 | for (l=k+1; l < left; l++) |
| 787 | if (zero_crossing[i+1].crossings[l] != 0) |
| 788 | count++; |
| 789 | if (((count % 2) == 0) && (left != k)) |
| 790 | correct=left; |
| 791 | } |
| 792 | /* |
| 793 | Check right for an even number of crossings between k and j. |
| 794 | */ |
| 795 | if (correct == -1) |
| 796 | { |
| 797 | count=0; |
| 798 | for (l=k+1; l < right; l++) |
| 799 | if (zero_crossing[i+1].crossings[l] != 0) |
| 800 | count++; |
| 801 | if (((count % 2) == 0) && (right != k)) |
| 802 | correct=right; |
| 803 | } |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 804 | l=(ssize_t) zero_crossing[i].crossings[j]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 805 | zero_crossing[i].crossings[j]=0; |
| 806 | if (correct != -1) |
| 807 | zero_crossing[i].crossings[correct]=(short) l; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | /* |
| 812 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 813 | % % |
| 814 | % % |
| 815 | % % |
| 816 | + D e f i n e R e g i o n % |
| 817 | % % |
| 818 | % % |
| 819 | % % |
| 820 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 821 | % |
| 822 | % DefineRegion() defines the left and right boundaries of a peak region. |
| 823 | % |
| 824 | % The format of the DefineRegion method is: |
| 825 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 826 | % ssize_t DefineRegion(const short *extrema,ExtentPacket *extents) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 827 | % |
| 828 | % A description of each parameter follows. |
| 829 | % |
| 830 | % o extrema: Specifies a pointer to an array of integers. They |
| 831 | % represent the peaks and valleys of the histogram for each color |
| 832 | % component. |
| 833 | % |
| 834 | % o extents: This pointer to an ExtentPacket represent the extends |
| 835 | % of a particular peak or valley of a color component. |
| 836 | % |
| 837 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 838 | static ssize_t DefineRegion(const short *extrema,ExtentPacket *extents) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 839 | { |
| 840 | /* |
| 841 | Initialize to default values. |
| 842 | */ |
| 843 | extents->left=0; |
| 844 | extents->center=0.0; |
| 845 | extents->right=255; |
| 846 | /* |
| 847 | Find the left side (maxima). |
| 848 | */ |
| 849 | for ( ; extents->index <= 255; extents->index++) |
| 850 | if (extrema[extents->index] > 0) |
| 851 | break; |
| 852 | if (extents->index > 255) |
| 853 | return(MagickFalse); /* no left side - no region exists */ |
| 854 | extents->left=extents->index; |
| 855 | /* |
| 856 | Find the right side (minima). |
| 857 | */ |
| 858 | for ( ; extents->index <= 255; extents->index++) |
| 859 | if (extrema[extents->index] < 0) |
| 860 | break; |
| 861 | extents->right=extents->index-1; |
| 862 | return(MagickTrue); |
| 863 | } |
| 864 | |
| 865 | /* |
| 866 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 867 | % % |
| 868 | % % |
| 869 | % % |
| 870 | + D e r i v a t i v e H i s t o g r a m % |
| 871 | % % |
| 872 | % % |
| 873 | % % |
| 874 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 875 | % |
| 876 | % DerivativeHistogram() determines the derivative of the histogram using |
| 877 | % central differencing. |
| 878 | % |
| 879 | % The format of the DerivativeHistogram method is: |
| 880 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 881 | % DerivativeHistogram(const double *histogram, |
| 882 | % double *derivative) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 883 | % |
| 884 | % A description of each parameter follows. |
| 885 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 886 | % o histogram: Specifies an array of doubles representing the number |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 887 | % of pixels for each intensity of a particular color component. |
| 888 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 889 | % o derivative: This array of doubles is initialized by |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 890 | % DerivativeHistogram to the derivative of the histogram using central |
| 891 | % differencing. |
| 892 | % |
| 893 | */ |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 894 | static void DerivativeHistogram(const double *histogram, |
| 895 | double *derivative) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 896 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 897 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 898 | i, |
| 899 | n; |
| 900 | |
| 901 | /* |
| 902 | Compute endpoints using second order polynomial interpolation. |
| 903 | */ |
| 904 | n=255; |
| 905 | derivative[0]=(-1.5*histogram[0]+2.0*histogram[1]-0.5*histogram[2]); |
| 906 | derivative[n]=(0.5*histogram[n-2]-2.0*histogram[n-1]+1.5*histogram[n]); |
| 907 | /* |
| 908 | Compute derivative using central differencing. |
| 909 | */ |
| 910 | for (i=1; i < n; i++) |
| 911 | derivative[i]=(histogram[i+1]-histogram[i-1])/2.0; |
| 912 | return; |
| 913 | } |
| 914 | |
| 915 | /* |
| 916 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 917 | % % |
| 918 | % % |
| 919 | % % |
| 920 | + G e t I m a g e D y n a m i c T h r e s h o l d % |
| 921 | % % |
| 922 | % % |
| 923 | % % |
| 924 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 925 | % |
| 926 | % GetImageDynamicThreshold() returns the dynamic threshold for an image. |
| 927 | % |
| 928 | % The format of the GetImageDynamicThreshold method is: |
| 929 | % |
| 930 | % MagickBooleanType GetImageDynamicThreshold(const Image *image, |
| 931 | % const double cluster_threshold,const double smooth_threshold, |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 932 | % PixelInfo *pixel,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 933 | % |
| 934 | % A description of each parameter follows. |
| 935 | % |
| 936 | % o image: the image. |
| 937 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 938 | % o cluster_threshold: This double represents the minimum number of |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 939 | % pixels contained in a hexahedra before it can be considered valid |
| 940 | % (expressed as a percentage). |
| 941 | % |
| 942 | % o smooth_threshold: the smoothing threshold eliminates noise in the second |
| 943 | % derivative of the histogram. As the value is increased, you can expect a |
| 944 | % smoother second derivative. |
| 945 | % |
| 946 | % o pixel: return the dynamic threshold here. |
| 947 | % |
| 948 | % o exception: return any errors or warnings in this structure. |
| 949 | % |
| 950 | */ |
| 951 | MagickExport MagickBooleanType GetImageDynamicThreshold(const Image *image, |
| 952 | const double cluster_threshold,const double smooth_threshold, |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 953 | PixelInfo *pixel,ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 954 | { |
| 955 | Cluster |
| 956 | *background, |
| 957 | *cluster, |
| 958 | *object, |
| 959 | *head, |
| 960 | *last_cluster, |
| 961 | *next_cluster; |
| 962 | |
| 963 | ExtentPacket |
| 964 | blue, |
| 965 | green, |
| 966 | red; |
| 967 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 968 | MagickBooleanType |
| 969 | proceed; |
| 970 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 971 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 972 | threshold; |
| 973 | |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 974 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 975 | *p; |
| 976 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 977 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 978 | i, |
| 979 | x; |
| 980 | |
| 981 | short |
| 982 | *extrema[MaxDimension]; |
| 983 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 984 | ssize_t |
| 985 | count, |
| 986 | *histogram[MaxDimension], |
| 987 | y; |
| 988 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 989 | /* |
| 990 | Allocate histogram and extrema. |
| 991 | */ |
| 992 | assert(image != (Image *) NULL); |
| 993 | assert(image->signature == MagickSignature); |
| 994 | if (image->debug != MagickFalse) |
| 995 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 996 | GetPixelInfo(image,pixel); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 997 | for (i=0; i < MaxDimension; i++) |
| 998 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 999 | histogram[i]=(ssize_t *) AcquireQuantumMemory(256UL,sizeof(**histogram)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1000 | extrema[i]=(short *) AcquireQuantumMemory(256UL,sizeof(**histogram)); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1001 | if ((histogram[i] == (ssize_t *) NULL) || (extrema[i] == (short *) NULL)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1002 | { |
| 1003 | for (i-- ; i >= 0; i--) |
| 1004 | { |
| 1005 | extrema[i]=(short *) RelinquishMagickMemory(extrema[i]); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1006 | histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1007 | } |
| 1008 | (void) ThrowMagickException(exception,GetMagickModule(), |
| cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 1009 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1010 | return(MagickFalse); |
| 1011 | } |
| 1012 | } |
| 1013 | /* |
| 1014 | Initialize histogram. |
| 1015 | */ |
| 1016 | InitializeHistogram(image,histogram,exception); |
| 1017 | (void) OptimalTau(histogram[Red],Tau,0.2f,DeltaTau, |
| 1018 | (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Red]); |
| 1019 | (void) OptimalTau(histogram[Green],Tau,0.2f,DeltaTau, |
| 1020 | (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Green]); |
| 1021 | (void) OptimalTau(histogram[Blue],Tau,0.2f,DeltaTau, |
| 1022 | (smooth_threshold == 0.0f ? 1.0f : smooth_threshold),extrema[Blue]); |
| 1023 | /* |
| 1024 | Form clusters. |
| 1025 | */ |
| 1026 | cluster=(Cluster *) NULL; |
| 1027 | head=(Cluster *) NULL; |
| 1028 | (void) ResetMagickMemory(&red,0,sizeof(red)); |
| 1029 | (void) ResetMagickMemory(&green,0,sizeof(green)); |
| 1030 | (void) ResetMagickMemory(&blue,0,sizeof(blue)); |
| 1031 | while (DefineRegion(extrema[Red],&red) != 0) |
| 1032 | { |
| 1033 | green.index=0; |
| 1034 | while (DefineRegion(extrema[Green],&green) != 0) |
| 1035 | { |
| 1036 | blue.index=0; |
| 1037 | while (DefineRegion(extrema[Blue],&blue) != 0) |
| 1038 | { |
| 1039 | /* |
| 1040 | Allocate a new class. |
| 1041 | */ |
| 1042 | if (head != (Cluster *) NULL) |
| 1043 | { |
| 1044 | cluster->next=(Cluster *) AcquireMagickMemory( |
| 1045 | sizeof(*cluster->next)); |
| 1046 | cluster=cluster->next; |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 1050 | cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1051 | head=cluster; |
| 1052 | } |
| 1053 | if (cluster == (Cluster *) NULL) |
| 1054 | { |
| 1055 | (void) ThrowMagickException(exception,GetMagickModule(), |
| cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 1056 | ResourceLimitError,"MemoryAllocationFailed","`%s'", |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1057 | image->filename); |
| 1058 | return(MagickFalse); |
| 1059 | } |
| 1060 | /* |
| 1061 | Initialize a new class. |
| 1062 | */ |
| 1063 | cluster->count=0; |
| 1064 | cluster->red=red; |
| 1065 | cluster->green=green; |
| 1066 | cluster->blue=blue; |
| 1067 | cluster->next=(Cluster *) NULL; |
| 1068 | } |
| 1069 | } |
| 1070 | } |
| 1071 | if (head == (Cluster *) NULL) |
| 1072 | { |
| 1073 | /* |
| 1074 | No classes were identified-- create one. |
| 1075 | */ |
| cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 1076 | cluster=(Cluster *) AcquireMagickMemory(sizeof(*cluster)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1077 | if (cluster == (Cluster *) NULL) |
| 1078 | { |
| 1079 | (void) ThrowMagickException(exception,GetMagickModule(), |
| cristy | efe601c | 2013-01-05 17:51:12 +0000 | [diff] [blame] | 1080 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1081 | return(MagickFalse); |
| 1082 | } |
| 1083 | /* |
| 1084 | Initialize a new class. |
| 1085 | */ |
| 1086 | cluster->count=0; |
| 1087 | cluster->red=red; |
| 1088 | cluster->green=green; |
| 1089 | cluster->blue=blue; |
| 1090 | cluster->next=(Cluster *) NULL; |
| 1091 | head=cluster; |
| 1092 | } |
| 1093 | /* |
| 1094 | Count the pixels for each cluster. |
| 1095 | */ |
| 1096 | count=0; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1097 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1098 | { |
| 1099 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1100 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1101 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1102 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1103 | { |
| 1104 | for (cluster=head; cluster != (Cluster *) NULL; cluster=cluster->next) |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1105 | if (((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1106 | (cluster->red.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1107 | ((ssize_t) ScaleQuantumToChar(GetPixelRed(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1108 | (cluster->red.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1109 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1110 | (cluster->green.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1111 | ((ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1112 | (cluster->green.right+SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1113 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) >= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1114 | (cluster->blue.left-SafeMargin)) && |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1115 | ((ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p)) <= |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1116 | (cluster->blue.right+SafeMargin))) |
| 1117 | { |
| 1118 | /* |
| 1119 | Count this pixel. |
| 1120 | */ |
| 1121 | count++; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1122 | cluster->red.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1123 | GetPixelRed(image,p)); |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1124 | cluster->green.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1125 | GetPixelGreen(image,p)); |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1126 | cluster->blue.center+=(double) ScaleQuantumToChar( |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1127 | GetPixelBlue(image,p)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1128 | cluster->count++; |
| 1129 | break; |
| 1130 | } |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1131 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1132 | } |
| cristy | cee9711 | 2010-05-28 00:44:52 +0000 | [diff] [blame] | 1133 | proceed=SetImageProgress(image,SegmentImageTag,(MagickOffsetType) y, |
| 1134 | 2*image->rows); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1135 | if (proceed == MagickFalse) |
| 1136 | break; |
| 1137 | } |
| 1138 | /* |
| 1139 | Remove clusters that do not meet minimum cluster threshold. |
| 1140 | */ |
| 1141 | count=0; |
| 1142 | last_cluster=head; |
| 1143 | next_cluster=head; |
| 1144 | for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster) |
| 1145 | { |
| 1146 | next_cluster=cluster->next; |
| 1147 | if ((cluster->count > 0) && |
| 1148 | (cluster->count >= (count*cluster_threshold/100.0))) |
| 1149 | { |
| 1150 | /* |
| 1151 | Initialize cluster. |
| 1152 | */ |
| 1153 | cluster->id=count; |
| 1154 | cluster->red.center/=cluster->count; |
| 1155 | cluster->green.center/=cluster->count; |
| 1156 | cluster->blue.center/=cluster->count; |
| 1157 | count++; |
| 1158 | last_cluster=cluster; |
| 1159 | continue; |
| 1160 | } |
| 1161 | /* |
| 1162 | Delete cluster. |
| 1163 | */ |
| 1164 | if (cluster == head) |
| 1165 | head=next_cluster; |
| 1166 | else |
| 1167 | last_cluster->next=next_cluster; |
| 1168 | cluster=(Cluster *) RelinquishMagickMemory(cluster); |
| 1169 | } |
| 1170 | object=head; |
| 1171 | background=head; |
| 1172 | if (count > 1) |
| 1173 | { |
| 1174 | object=head->next; |
| 1175 | for (cluster=object; cluster->next != (Cluster *) NULL; ) |
| 1176 | { |
| 1177 | if (cluster->count < object->count) |
| 1178 | object=cluster; |
| 1179 | cluster=cluster->next; |
| 1180 | } |
| 1181 | background=head->next; |
| 1182 | for (cluster=background; cluster->next != (Cluster *) NULL; ) |
| 1183 | { |
| 1184 | if (cluster->count > background->count) |
| 1185 | background=cluster; |
| 1186 | cluster=cluster->next; |
| 1187 | } |
| 1188 | } |
| 1189 | threshold=(background->red.center+object->red.center)/2.0; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1190 | pixel->red=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1191 | (threshold+0.5)); |
| 1192 | threshold=(background->green.center+object->green.center)/2.0; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1193 | pixel->green=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1194 | (threshold+0.5)); |
| 1195 | threshold=(background->blue.center+object->blue.center)/2.0; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1196 | pixel->blue=(double) ScaleCharToQuantum((unsigned char) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1197 | (threshold+0.5)); |
| 1198 | /* |
| 1199 | Relinquish resources. |
| 1200 | */ |
| 1201 | for (cluster=head; cluster != (Cluster *) NULL; cluster=next_cluster) |
| 1202 | { |
| 1203 | next_cluster=cluster->next; |
| 1204 | cluster=(Cluster *) RelinquishMagickMemory(cluster); |
| 1205 | } |
| 1206 | for (i=0; i < MaxDimension; i++) |
| 1207 | { |
| 1208 | extrema[i]=(short *) RelinquishMagickMemory(extrema[i]); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1209 | histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1210 | } |
| 1211 | return(MagickTrue); |
| 1212 | } |
| 1213 | |
| 1214 | /* |
| 1215 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1216 | % % |
| 1217 | % % |
| 1218 | % % |
| 1219 | + I n i t i a l i z e H i s t o g r a m % |
| 1220 | % % |
| 1221 | % % |
| 1222 | % % |
| 1223 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1224 | % |
| 1225 | % InitializeHistogram() computes the histogram for an image. |
| 1226 | % |
| 1227 | % The format of the InitializeHistogram method is: |
| 1228 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1229 | % InitializeHistogram(const Image *image,ssize_t **histogram) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1230 | % |
| 1231 | % A description of each parameter follows. |
| 1232 | % |
| 1233 | % o image: Specifies a pointer to an Image structure; returned from |
| 1234 | % ReadImage. |
| 1235 | % |
| 1236 | % o histogram: Specifies an array of integers representing the number |
| 1237 | % of pixels for each intensity of a particular color component. |
| 1238 | % |
| 1239 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1240 | static void InitializeHistogram(const Image *image,ssize_t **histogram, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1241 | ExceptionInfo *exception) |
| 1242 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1243 | register const Quantum |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1244 | *p; |
| 1245 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1246 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1247 | i, |
| 1248 | x; |
| 1249 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1250 | ssize_t |
| 1251 | y; |
| 1252 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1253 | /* |
| 1254 | Initialize histogram. |
| 1255 | */ |
| 1256 | for (i=0; i <= 255; i++) |
| 1257 | { |
| 1258 | histogram[Red][i]=0; |
| 1259 | histogram[Green][i]=0; |
| 1260 | histogram[Blue][i]=0; |
| 1261 | } |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1262 | for (y=0; y < (ssize_t) image->rows; y++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1263 | { |
| 1264 | p=GetVirtualPixels(image,0,y,image->columns,1,exception); |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1265 | if (p == (const Quantum *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1266 | break; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1267 | for (x=0; x < (ssize_t) image->columns; x++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1268 | { |
| cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 1269 | histogram[Red][(ssize_t) ScaleQuantumToChar(GetPixelRed(image,p))]++; |
| 1270 | histogram[Green][(ssize_t) ScaleQuantumToChar(GetPixelGreen(image,p))]++; |
| 1271 | histogram[Blue][(ssize_t) ScaleQuantumToChar(GetPixelBlue(image,p))]++; |
| cristy | ed23157 | 2011-07-14 02:18:59 +0000 | [diff] [blame] | 1272 | p+=GetPixelChannels(image); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1279 | % % |
| 1280 | % % |
| 1281 | % % |
| 1282 | + I n i t i a l i z e I n t e r v a l T r e e % |
| 1283 | % % |
| 1284 | % % |
| 1285 | % % |
| 1286 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1287 | % |
| 1288 | % InitializeIntervalTree() initializes an interval tree from the lists of |
| 1289 | % zero crossings. |
| 1290 | % |
| 1291 | % The format of the InitializeIntervalTree method is: |
| 1292 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1293 | % InitializeIntervalTree(IntervalTree **list,ssize_t *number_nodes, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1294 | % IntervalTree *node) |
| 1295 | % |
| 1296 | % A description of each parameter follows. |
| 1297 | % |
| 1298 | % o zero_crossing: Specifies an array of structures of type ZeroCrossing. |
| 1299 | % |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1300 | % o number_crossings: This size_t specifies the number of elements |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1301 | % in the zero_crossing array. |
| 1302 | % |
| 1303 | */ |
| 1304 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1305 | static void InitializeList(IntervalTree **list,ssize_t *number_nodes, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1306 | IntervalTree *node) |
| 1307 | { |
| 1308 | if (node == (IntervalTree *) NULL) |
| 1309 | return; |
| 1310 | if (node->child == (IntervalTree *) NULL) |
| 1311 | list[(*number_nodes)++]=node; |
| 1312 | InitializeList(list,number_nodes,node->sibling); |
| 1313 | InitializeList(list,number_nodes,node->child); |
| 1314 | } |
| 1315 | |
| 1316 | static void MeanStability(IntervalTree *node) |
| 1317 | { |
| 1318 | register IntervalTree |
| 1319 | *child; |
| 1320 | |
| 1321 | if (node == (IntervalTree *) NULL) |
| 1322 | return; |
| 1323 | node->mean_stability=0.0; |
| 1324 | child=node->child; |
| 1325 | if (child != (IntervalTree *) NULL) |
| 1326 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1327 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1328 | count; |
| 1329 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1330 | register double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1331 | sum; |
| 1332 | |
| 1333 | sum=0.0; |
| 1334 | count=0; |
| 1335 | for ( ; child != (IntervalTree *) NULL; child=child->sibling) |
| 1336 | { |
| 1337 | sum+=child->stability; |
| 1338 | count++; |
| 1339 | } |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1340 | node->mean_stability=sum/(double) count; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1341 | } |
| 1342 | MeanStability(node->sibling); |
| 1343 | MeanStability(node->child); |
| 1344 | } |
| 1345 | |
| 1346 | static void Stability(IntervalTree *node) |
| 1347 | { |
| 1348 | if (node == (IntervalTree *) NULL) |
| 1349 | return; |
| 1350 | if (node->child == (IntervalTree *) NULL) |
| 1351 | node->stability=0.0; |
| 1352 | else |
| 1353 | node->stability=node->tau-(node->child)->tau; |
| 1354 | Stability(node->sibling); |
| 1355 | Stability(node->child); |
| 1356 | } |
| 1357 | |
| 1358 | static IntervalTree *InitializeIntervalTree(const ZeroCrossing *zero_crossing, |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1359 | const size_t number_crossings) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1360 | { |
| 1361 | IntervalTree |
| 1362 | *head, |
| 1363 | **list, |
| 1364 | *node, |
| 1365 | *root; |
| 1366 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1367 | register ssize_t |
| 1368 | i; |
| 1369 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1370 | ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1371 | j, |
| 1372 | k, |
| 1373 | left, |
| 1374 | number_nodes; |
| 1375 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1376 | /* |
| 1377 | Allocate interval tree. |
| 1378 | */ |
| 1379 | list=(IntervalTree **) AcquireQuantumMemory((size_t) TreeLength, |
| 1380 | sizeof(*list)); |
| 1381 | if (list == (IntervalTree **) NULL) |
| 1382 | return((IntervalTree *) NULL); |
| 1383 | /* |
| 1384 | The root is the entire histogram. |
| 1385 | */ |
| cristy | 73bd4a5 | 2010-10-05 11:24:23 +0000 | [diff] [blame] | 1386 | root=(IntervalTree *) AcquireMagickMemory(sizeof(*root)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1387 | root->child=(IntervalTree *) NULL; |
| 1388 | root->sibling=(IntervalTree *) NULL; |
| 1389 | root->tau=0.0; |
| 1390 | root->left=0; |
| 1391 | root->right=255; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1392 | for (i=(-1); i < (ssize_t) number_crossings; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1393 | { |
| 1394 | /* |
| 1395 | Initialize list with all nodes with no children. |
| 1396 | */ |
| 1397 | number_nodes=0; |
| 1398 | InitializeList(list,&number_nodes,root); |
| 1399 | /* |
| 1400 | Split list. |
| 1401 | */ |
| 1402 | for (j=0; j < number_nodes; j++) |
| 1403 | { |
| 1404 | head=list[j]; |
| 1405 | left=head->left; |
| 1406 | node=head; |
| 1407 | for (k=head->left+1; k < head->right; k++) |
| 1408 | { |
| 1409 | if (zero_crossing[i+1].crossings[k] != 0) |
| 1410 | { |
| 1411 | if (node == head) |
| 1412 | { |
| 1413 | node->child=(IntervalTree *) AcquireMagickMemory( |
| 1414 | sizeof(*node->child)); |
| 1415 | node=node->child; |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | node->sibling=(IntervalTree *) AcquireMagickMemory( |
| 1420 | sizeof(*node->sibling)); |
| 1421 | node=node->sibling; |
| 1422 | } |
| 1423 | node->tau=zero_crossing[i+1].tau; |
| 1424 | node->child=(IntervalTree *) NULL; |
| 1425 | node->sibling=(IntervalTree *) NULL; |
| 1426 | node->left=left; |
| 1427 | node->right=k; |
| 1428 | left=k; |
| 1429 | } |
| 1430 | } |
| 1431 | if (left != head->left) |
| 1432 | { |
| 1433 | node->sibling=(IntervalTree *) AcquireMagickMemory( |
| 1434 | sizeof(*node->sibling)); |
| 1435 | node=node->sibling; |
| 1436 | node->tau=zero_crossing[i+1].tau; |
| 1437 | node->child=(IntervalTree *) NULL; |
| 1438 | node->sibling=(IntervalTree *) NULL; |
| 1439 | node->left=left; |
| 1440 | node->right=head->right; |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | /* |
| 1445 | Determine the stability: difference between a nodes tau and its child. |
| 1446 | */ |
| 1447 | Stability(root->child); |
| 1448 | MeanStability(root->child); |
| 1449 | list=(IntervalTree **) RelinquishMagickMemory(list); |
| 1450 | return(root); |
| 1451 | } |
| 1452 | |
| 1453 | /* |
| 1454 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1455 | % % |
| 1456 | % % |
| 1457 | % % |
| 1458 | + O p t i m a l T a u % |
| 1459 | % % |
| 1460 | % % |
| 1461 | % % |
| 1462 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1463 | % |
| 1464 | % OptimalTau() finds the optimal tau for each band of the histogram. |
| 1465 | % |
| 1466 | % The format of the OptimalTau method is: |
| 1467 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1468 | % double OptimalTau(const ssize_t *histogram,const double max_tau, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1469 | % const double min_tau,const double delta_tau, |
| 1470 | % const double smooth_threshold,short *extrema) |
| 1471 | % |
| 1472 | % A description of each parameter follows. |
| 1473 | % |
| 1474 | % o histogram: Specifies an array of integers representing the number |
| 1475 | % of pixels for each intensity of a particular color component. |
| 1476 | % |
| 1477 | % o extrema: Specifies a pointer to an array of integers. They |
| 1478 | % represent the peaks and valleys of the histogram for each color |
| 1479 | % component. |
| 1480 | % |
| 1481 | */ |
| 1482 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1483 | static void ActiveNodes(IntervalTree **list,ssize_t *number_nodes, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1484 | IntervalTree *node) |
| 1485 | { |
| 1486 | if (node == (IntervalTree *) NULL) |
| 1487 | return; |
| 1488 | if (node->stability >= node->mean_stability) |
| 1489 | { |
| 1490 | list[(*number_nodes)++]=node; |
| 1491 | ActiveNodes(list,number_nodes,node->sibling); |
| 1492 | } |
| 1493 | else |
| 1494 | { |
| 1495 | ActiveNodes(list,number_nodes,node->sibling); |
| 1496 | ActiveNodes(list,number_nodes,node->child); |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | static void FreeNodes(IntervalTree *node) |
| 1501 | { |
| 1502 | if (node == (IntervalTree *) NULL) |
| 1503 | return; |
| 1504 | FreeNodes(node->sibling); |
| 1505 | FreeNodes(node->child); |
| 1506 | node=(IntervalTree *) RelinquishMagickMemory(node); |
| 1507 | } |
| 1508 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1509 | static double OptimalTau(const ssize_t *histogram,const double max_tau, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1510 | const double min_tau,const double delta_tau,const double smooth_threshold, |
| 1511 | short *extrema) |
| 1512 | { |
| 1513 | IntervalTree |
| 1514 | **list, |
| 1515 | *node, |
| 1516 | *root; |
| 1517 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1518 | MagickBooleanType |
| 1519 | peak; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1520 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1521 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1522 | average_tau, |
| 1523 | *derivative, |
| 1524 | *second_derivative, |
| 1525 | tau, |
| 1526 | value; |
| 1527 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1528 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1529 | i, |
| 1530 | x; |
| 1531 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1532 | size_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1533 | count, |
| 1534 | number_crossings; |
| 1535 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1536 | ssize_t |
| 1537 | index, |
| 1538 | j, |
| 1539 | k, |
| 1540 | number_nodes; |
| 1541 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1542 | ZeroCrossing |
| 1543 | *zero_crossing; |
| 1544 | |
| 1545 | /* |
| 1546 | Allocate interval tree. |
| 1547 | */ |
| 1548 | list=(IntervalTree **) AcquireQuantumMemory((size_t) TreeLength, |
| 1549 | sizeof(*list)); |
| 1550 | if (list == (IntervalTree **) NULL) |
| 1551 | return(0.0); |
| 1552 | /* |
| 1553 | Allocate zero crossing list. |
| 1554 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1555 | count=(size_t) ((max_tau-min_tau)/delta_tau)+2; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1556 | zero_crossing=(ZeroCrossing *) AcquireQuantumMemory((size_t) count, |
| 1557 | sizeof(*zero_crossing)); |
| 1558 | if (zero_crossing == (ZeroCrossing *) NULL) |
| 1559 | return(0.0); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1560 | for (i=0; i < (ssize_t) count; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1561 | zero_crossing[i].tau=(-1.0); |
| 1562 | /* |
| 1563 | Initialize zero crossing list. |
| 1564 | */ |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1565 | derivative=(double *) AcquireQuantumMemory(256,sizeof(*derivative)); |
| 1566 | second_derivative=(double *) AcquireQuantumMemory(256, |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1567 | sizeof(*second_derivative)); |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1568 | if ((derivative == (double *) NULL) || |
| 1569 | (second_derivative == (double *) NULL)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1570 | ThrowFatalException(ResourceLimitFatalError, |
| 1571 | "UnableToAllocateDerivatives"); |
| 1572 | i=0; |
| 1573 | for (tau=max_tau; tau >= min_tau; tau-=delta_tau) |
| 1574 | { |
| 1575 | zero_crossing[i].tau=tau; |
| 1576 | ScaleSpace(histogram,tau,zero_crossing[i].histogram); |
| 1577 | DerivativeHistogram(zero_crossing[i].histogram,derivative); |
| 1578 | DerivativeHistogram(derivative,second_derivative); |
| 1579 | ZeroCrossHistogram(second_derivative,smooth_threshold, |
| 1580 | zero_crossing[i].crossings); |
| 1581 | i++; |
| 1582 | } |
| 1583 | /* |
| 1584 | Add an entry for the original histogram. |
| 1585 | */ |
| 1586 | zero_crossing[i].tau=0.0; |
| 1587 | for (j=0; j <= 255; j++) |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1588 | zero_crossing[i].histogram[j]=(double) histogram[j]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1589 | DerivativeHistogram(zero_crossing[i].histogram,derivative); |
| 1590 | DerivativeHistogram(derivative,second_derivative); |
| 1591 | ZeroCrossHistogram(second_derivative,smooth_threshold, |
| 1592 | zero_crossing[i].crossings); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1593 | number_crossings=(size_t) i; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1594 | derivative=(double *) RelinquishMagickMemory(derivative); |
| 1595 | second_derivative=(double *) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1596 | RelinquishMagickMemory(second_derivative); |
| 1597 | /* |
| 1598 | Ensure the scale-space fingerprints form lines in scale-space, not loops. |
| 1599 | */ |
| 1600 | ConsolidateCrossings(zero_crossing,number_crossings); |
| 1601 | /* |
| 1602 | Force endpoints to be included in the interval. |
| 1603 | */ |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1604 | for (i=0; i <= (ssize_t) number_crossings; i++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1605 | { |
| 1606 | for (j=0; j < 255; j++) |
| 1607 | if (zero_crossing[i].crossings[j] != 0) |
| 1608 | break; |
| 1609 | zero_crossing[i].crossings[0]=(-zero_crossing[i].crossings[j]); |
| 1610 | for (j=255; j > 0; j--) |
| 1611 | if (zero_crossing[i].crossings[j] != 0) |
| 1612 | break; |
| 1613 | zero_crossing[i].crossings[255]=(-zero_crossing[i].crossings[j]); |
| 1614 | } |
| 1615 | /* |
| 1616 | Initialize interval tree. |
| 1617 | */ |
| 1618 | root=InitializeIntervalTree(zero_crossing,number_crossings); |
| 1619 | if (root == (IntervalTree *) NULL) |
| 1620 | return(0.0); |
| 1621 | /* |
| 1622 | Find active nodes: stability is greater (or equal) to the mean stability of |
| 1623 | its children. |
| 1624 | */ |
| 1625 | number_nodes=0; |
| 1626 | ActiveNodes(list,&number_nodes,root->child); |
| 1627 | /* |
| 1628 | Initialize extrema. |
| 1629 | */ |
| 1630 | for (i=0; i <= 255; i++) |
| 1631 | extrema[i]=0; |
| 1632 | for (i=0; i < number_nodes; i++) |
| 1633 | { |
| 1634 | /* |
| 1635 | Find this tau in zero crossings list. |
| 1636 | */ |
| 1637 | k=0; |
| 1638 | node=list[i]; |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1639 | for (j=0; j <= (ssize_t) number_crossings; j++) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1640 | if (zero_crossing[j].tau == node->tau) |
| 1641 | k=j; |
| 1642 | /* |
| 1643 | Find the value of the peak. |
| 1644 | */ |
| 1645 | peak=zero_crossing[k].crossings[node->right] == -1 ? MagickTrue : |
| 1646 | MagickFalse; |
| 1647 | index=node->left; |
| 1648 | value=zero_crossing[k].histogram[index]; |
| 1649 | for (x=node->left; x <= node->right; x++) |
| 1650 | { |
| 1651 | if (peak != MagickFalse) |
| 1652 | { |
| 1653 | if (zero_crossing[k].histogram[x] > value) |
| 1654 | { |
| 1655 | value=zero_crossing[k].histogram[x]; |
| 1656 | index=x; |
| 1657 | } |
| 1658 | } |
| 1659 | else |
| 1660 | if (zero_crossing[k].histogram[x] < value) |
| 1661 | { |
| 1662 | value=zero_crossing[k].histogram[x]; |
| 1663 | index=x; |
| 1664 | } |
| 1665 | } |
| 1666 | for (x=node->left; x <= node->right; x++) |
| 1667 | { |
| 1668 | if (index == 0) |
| 1669 | index=256; |
| 1670 | if (peak != MagickFalse) |
| 1671 | extrema[x]=(short) index; |
| 1672 | else |
| 1673 | extrema[x]=(short) (-index); |
| 1674 | } |
| 1675 | } |
| 1676 | /* |
| 1677 | Determine the average tau. |
| 1678 | */ |
| 1679 | average_tau=0.0; |
| 1680 | for (i=0; i < number_nodes; i++) |
| 1681 | average_tau+=list[i]->tau; |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1682 | average_tau/=(double) number_nodes; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1683 | /* |
| 1684 | Relinquish resources. |
| 1685 | */ |
| 1686 | FreeNodes(root); |
| 1687 | zero_crossing=(ZeroCrossing *) RelinquishMagickMemory(zero_crossing); |
| 1688 | list=(IntervalTree **) RelinquishMagickMemory(list); |
| 1689 | return(average_tau); |
| 1690 | } |
| 1691 | |
| 1692 | /* |
| 1693 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1694 | % % |
| 1695 | % % |
| 1696 | % % |
| 1697 | + S c a l e S p a c e % |
| 1698 | % % |
| 1699 | % % |
| 1700 | % % |
| 1701 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1702 | % |
| 1703 | % ScaleSpace() performs a scale-space filter on the 1D histogram. |
| 1704 | % |
| 1705 | % The format of the ScaleSpace method is: |
| 1706 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1707 | % ScaleSpace(const ssize_t *histogram,const double tau, |
| 1708 | % double *scale_histogram) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1709 | % |
| 1710 | % A description of each parameter follows. |
| 1711 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1712 | % o histogram: Specifies an array of doubles representing the number |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1713 | % of pixels for each intensity of a particular color component. |
| 1714 | % |
| 1715 | */ |
| 1716 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1717 | static void ScaleSpace(const ssize_t *histogram,const double tau, |
| 1718 | double *scale_histogram) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1719 | { |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1720 | double |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1721 | alpha, |
| 1722 | beta, |
| 1723 | *gamma, |
| 1724 | sum; |
| 1725 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1726 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1727 | u, |
| 1728 | x; |
| 1729 | |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1730 | gamma=(double *) AcquireQuantumMemory(256,sizeof(*gamma)); |
| 1731 | if (gamma == (double *) NULL) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1732 | ThrowFatalException(ResourceLimitFatalError, |
| 1733 | "UnableToAllocateGammaMap"); |
| cristy | 3e3ec3a | 2012-11-03 23:11:06 +0000 | [diff] [blame] | 1734 | alpha=PerceptibleReciprocal(tau*sqrt(2.0*MagickPI)); |
| 1735 | beta=(-1.0*PerceptibleReciprocal(2.0*tau*tau)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1736 | for (x=0; x <= 255; x++) |
| 1737 | gamma[x]=0.0; |
| 1738 | for (x=0; x <= 255; x++) |
| 1739 | { |
| 1740 | gamma[x]=exp((double) beta*x*x); |
| 1741 | if (gamma[x] < MagickEpsilon) |
| 1742 | break; |
| 1743 | } |
| 1744 | for (x=0; x <= 255; x++) |
| 1745 | { |
| 1746 | sum=0.0; |
| 1747 | for (u=0; u <= 255; u++) |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1748 | sum+=(double) histogram[u]*gamma[MagickAbsoluteValue(x-u)]; |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1749 | scale_histogram[x]=alpha*sum; |
| 1750 | } |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1751 | gamma=(double *) RelinquishMagickMemory(gamma); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1756 | % % |
| 1757 | % % |
| 1758 | % % |
| 1759 | % S e g m e n t I m a g e % |
| 1760 | % % |
| 1761 | % % |
| 1762 | % % |
| 1763 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1764 | % |
| 1765 | % SegmentImage() segment an image by analyzing the histograms of the color |
| 1766 | % components and identifying units that are homogeneous with the fuzzy |
| 1767 | % C-means technique. |
| 1768 | % |
| 1769 | % The format of the SegmentImage method is: |
| 1770 | % |
| 1771 | % MagickBooleanType SegmentImage(Image *image, |
| 1772 | % const ColorspaceType colorspace,const MagickBooleanType verbose, |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1773 | % const double cluster_threshold,const double smooth_threshold, |
| 1774 | % ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1775 | % |
| 1776 | % A description of each parameter follows. |
| 1777 | % |
| 1778 | % o image: the image. |
| 1779 | % |
| 1780 | % o colorspace: Indicate the colorspace. |
| 1781 | % |
| 1782 | % o verbose: Set to MagickTrue to print detailed information about the |
| 1783 | % identified classes. |
| 1784 | % |
| 1785 | % o cluster_threshold: This represents the minimum number of pixels |
| 1786 | % contained in a hexahedra before it can be considered valid (expressed |
| 1787 | % as a percentage). |
| 1788 | % |
| 1789 | % o smooth_threshold: the smoothing threshold eliminates noise in the second |
| 1790 | % derivative of the histogram. As the value is increased, you can expect a |
| 1791 | % smoother second derivative. |
| 1792 | % |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1793 | % o exception: return any errors or warnings in this structure. |
| 1794 | % |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1795 | */ |
| 1796 | MagickExport MagickBooleanType SegmentImage(Image *image, |
| 1797 | const ColorspaceType colorspace,const MagickBooleanType verbose, |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1798 | const double cluster_threshold,const double smooth_threshold, |
| 1799 | ExceptionInfo *exception) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1800 | { |
| cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 1801 | ColorspaceType |
| 1802 | previous_colorspace; |
| 1803 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1804 | MagickBooleanType |
| 1805 | status; |
| 1806 | |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1807 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1808 | i; |
| 1809 | |
| 1810 | short |
| 1811 | *extrema[MaxDimension]; |
| 1812 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1813 | ssize_t |
| 1814 | *histogram[MaxDimension]; |
| 1815 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1816 | /* |
| 1817 | Allocate histogram and extrema. |
| 1818 | */ |
| 1819 | assert(image != (Image *) NULL); |
| 1820 | assert(image->signature == MagickSignature); |
| 1821 | if (image->debug != MagickFalse) |
| 1822 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 1823 | for (i=0; i < MaxDimension; i++) |
| 1824 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1825 | histogram[i]=(ssize_t *) AcquireQuantumMemory(256,sizeof(**histogram)); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1826 | extrema[i]=(short *) AcquireQuantumMemory(256,sizeof(**extrema)); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1827 | if ((histogram[i] == (ssize_t *) NULL) || (extrema[i] == (short *) NULL)) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1828 | { |
| 1829 | for (i-- ; i >= 0; i--) |
| 1830 | { |
| 1831 | extrema[i]=(short *) RelinquishMagickMemory(extrema[i]); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1832 | histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1833 | } |
| 1834 | ThrowBinaryException(ResourceLimitError,"MemoryAllocationFailed", |
| 1835 | image->filename) |
| 1836 | } |
| 1837 | } |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1838 | /* |
| 1839 | Initialize histogram. |
| 1840 | */ |
| cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 1841 | previous_colorspace=image->colorspace; |
| 1842 | (void) TransformImageColorspace(image,colorspace,exception); |
| cristy | c82a27b | 2011-10-21 01:07:16 +0000 | [diff] [blame] | 1843 | InitializeHistogram(image,histogram,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1844 | (void) OptimalTau(histogram[Red],Tau,0.2,DeltaTau, |
| 1845 | smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Red]); |
| 1846 | (void) OptimalTau(histogram[Green],Tau,0.2,DeltaTau, |
| 1847 | smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Green]); |
| 1848 | (void) OptimalTau(histogram[Blue],Tau,0.2,DeltaTau, |
| 1849 | smooth_threshold == 0.0 ? 1.0 : smooth_threshold,extrema[Blue]); |
| 1850 | /* |
| 1851 | Classify using the fuzzy c-Means technique. |
| 1852 | */ |
| cristy | 018f07f | 2011-09-04 21:15:19 +0000 | [diff] [blame] | 1853 | status=Classify(image,extrema,cluster_threshold,WeightingExponent,verbose, |
| 1854 | exception); |
| cristy | 3d9f5ba | 2012-06-26 13:37:31 +0000 | [diff] [blame] | 1855 | (void) TransformImageColorspace(image,previous_colorspace,exception); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1856 | /* |
| 1857 | Relinquish resources. |
| 1858 | */ |
| 1859 | for (i=0; i < MaxDimension; i++) |
| 1860 | { |
| 1861 | extrema[i]=(short *) RelinquishMagickMemory(extrema[i]); |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1862 | histogram[i]=(ssize_t *) RelinquishMagickMemory(histogram[i]); |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1863 | } |
| 1864 | return(status); |
| 1865 | } |
| 1866 | |
| 1867 | /* |
| 1868 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1869 | % % |
| 1870 | % % |
| 1871 | % % |
| 1872 | + Z e r o C r o s s H i s t o g r a m % |
| 1873 | % % |
| 1874 | % % |
| 1875 | % % |
| 1876 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 1877 | % |
| 1878 | % ZeroCrossHistogram() find the zero crossings in a histogram and marks |
| 1879 | % directions as: 1 is negative to positive; 0 is zero crossing; and -1 |
| 1880 | % is positive to negative. |
| 1881 | % |
| 1882 | % The format of the ZeroCrossHistogram method is: |
| 1883 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1884 | % ZeroCrossHistogram(double *second_derivative, |
| 1885 | % const double smooth_threshold,short *crossings) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1886 | % |
| 1887 | % A description of each parameter follows. |
| 1888 | % |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1889 | % o second_derivative: Specifies an array of doubles representing the |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1890 | % second derivative of the histogram of a particular color component. |
| 1891 | % |
| 1892 | % o crossings: This array of integers is initialized with |
| 1893 | % -1, 0, or 1 representing the slope of the first derivative of the |
| 1894 | % of a particular color component. |
| 1895 | % |
| 1896 | */ |
| cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 1897 | static void ZeroCrossHistogram(double *second_derivative, |
| 1898 | const double smooth_threshold,short *crossings) |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1899 | { |
| cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 1900 | register ssize_t |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1901 | i; |
| 1902 | |
| cristy | 9d314ff | 2011-03-09 01:30:28 +0000 | [diff] [blame] | 1903 | ssize_t |
| 1904 | parity; |
| 1905 | |
| cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1906 | /* |
| 1907 | Merge low numbers to zero to help prevent noise. |
| 1908 | */ |
| 1909 | for (i=0; i <= 255; i++) |
| 1910 | if ((second_derivative[i] < smooth_threshold) && |
| 1911 | (second_derivative[i] >= -smooth_threshold)) |
| 1912 | second_derivative[i]=0.0; |
| 1913 | /* |
| 1914 | Mark zero crossings. |
| 1915 | */ |
| 1916 | parity=0; |
| 1917 | for (i=0; i <= 255; i++) |
| 1918 | { |
| 1919 | crossings[i]=0; |
| 1920 | if (second_derivative[i] < 0.0) |
| 1921 | { |
| 1922 | if (parity > 0) |
| 1923 | crossings[i]=(-1); |
| 1924 | parity=1; |
| 1925 | } |
| 1926 | else |
| 1927 | if (second_derivative[i] > 0.0) |
| 1928 | { |
| 1929 | if (parity < 0) |
| 1930 | crossings[i]=1; |
| 1931 | parity=(-1); |
| 1932 | } |
| 1933 | } |
| 1934 | } |