| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 3 | % % |
| 4 | % % |
| 5 | % % |
| 6 | % V V IIIII SSSSS IIIII OOO N N % |
| 7 | % V V I SS I O O NN N % |
| 8 | % V V I SSS I O O N N N % |
| 9 | % V V I SS I O O N NN % |
| 10 | % V IIIII SSSSS IIIII OOO N N % |
| 11 | % % |
| 12 | % % |
| 13 | % MagickCore Computer Vision Methods % |
| 14 | % % |
| 15 | % Software Design % |
| 16 | % Cristy % |
| 17 | % September 2014 % |
| 18 | % % |
| 19 | % % |
| cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 20 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 21 | % dedicated to making software imaging solutions freely available. % |
| 22 | % % |
| 23 | % You may not use this file except in compliance with the License. You may % |
| 24 | % obtain a copy of the License at % |
| 25 | % % |
| 26 | % http://www.imagemagick.org/script/license.php % |
| 27 | % % |
| 28 | % Unless required by applicable law or agreed to in writing, software % |
| 29 | % distributed under the License is distributed on an "AS IS" BASIS, % |
| 30 | % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. % |
| 31 | % See the License for the specific language governing permissions and % |
| 32 | % limitations under the License. % |
| 33 | % % |
| 34 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 35 | % |
| 36 | % |
| 37 | */ |
| 38 | |
| 39 | #include "MagickCore/studio.h" |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 40 | #include "MagickCore/artifact.h" |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 41 | #include "MagickCore/blob.h" |
| 42 | #include "MagickCore/cache-view.h" |
| 43 | #include "MagickCore/color.h" |
| 44 | #include "MagickCore/color-private.h" |
| 45 | #include "MagickCore/colorspace.h" |
| 46 | #include "MagickCore/constitute.h" |
| 47 | #include "MagickCore/decorate.h" |
| 48 | #include "MagickCore/distort.h" |
| 49 | #include "MagickCore/draw.h" |
| 50 | #include "MagickCore/enhance.h" |
| 51 | #include "MagickCore/exception.h" |
| 52 | #include "MagickCore/exception-private.h" |
| 53 | #include "MagickCore/effect.h" |
| 54 | #include "MagickCore/gem.h" |
| 55 | #include "MagickCore/geometry.h" |
| 56 | #include "MagickCore/image-private.h" |
| 57 | #include "MagickCore/list.h" |
| 58 | #include "MagickCore/log.h" |
| 59 | #include "MagickCore/matrix.h" |
| 60 | #include "MagickCore/memory_.h" |
| 61 | #include "MagickCore/memory-private.h" |
| 62 | #include "MagickCore/monitor.h" |
| 63 | #include "MagickCore/monitor-private.h" |
| 64 | #include "MagickCore/montage.h" |
| 65 | #include "MagickCore/morphology.h" |
| 66 | #include "MagickCore/morphology-private.h" |
| 67 | #include "MagickCore/opencl-private.h" |
| 68 | #include "MagickCore/paint.h" |
| 69 | #include "MagickCore/pixel-accessor.h" |
| 70 | #include "MagickCore/pixel-private.h" |
| 71 | #include "MagickCore/property.h" |
| 72 | #include "MagickCore/quantum.h" |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 73 | #include "MagickCore/quantum-private.h" |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 74 | #include "MagickCore/resource_.h" |
| 75 | #include "MagickCore/signature-private.h" |
| 76 | #include "MagickCore/string_.h" |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 77 | #include "MagickCore/string-private.h" |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 78 | #include "MagickCore/thread-private.h" |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 79 | #include "MagickCore/token.h" |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 80 | #include "MagickCore/vision.h" |
| 81 | |
| 82 | /* |
| 83 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 84 | % % |
| 85 | % % |
| 86 | % % |
| 87 | % C o n n e c t e d C o m p o n e n t s I m a g e % |
| 88 | % % |
| 89 | % % |
| 90 | % % |
| 91 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 92 | % |
| 93 | % ConnectedComponentsImage() returns the connected-components of the image |
| 94 | % uniquely labeled. Choose from 4 or 8-way connectivity. |
| 95 | % |
| 96 | % The format of the ConnectedComponentsImage method is: |
| 97 | % |
| 98 | % Image *ConnectedComponentsImage(const Image *image, |
| 99 | % const size_t connectivity,ExceptionInfo *exception) |
| 100 | % |
| 101 | % A description of each parameter follows: |
| 102 | % |
| 103 | % o image: the image. |
| 104 | % |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 105 | % o connectivity: how many neighbors to visit, choose from 4 or 8. |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 106 | % |
| 107 | % o exception: return any errors or warnings in this structure. |
| 108 | % |
| 109 | */ |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 110 | |
| 111 | typedef struct _CCObject |
| 112 | { |
| 113 | ssize_t |
| 114 | id; |
| 115 | |
| 116 | RectangleInfo |
| 117 | bounding_box; |
| 118 | |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 119 | PixelInfo |
| 120 | color; |
| 121 | |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 122 | PointInfo |
| 123 | centroid; |
| 124 | |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 125 | double |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 126 | area, |
| 127 | census; |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 128 | } CCObject; |
| 129 | |
| 130 | static int CCObjectCompare(const void *x,const void *y) |
| 131 | { |
| 132 | CCObject |
| 133 | *p, |
| 134 | *q; |
| 135 | |
| 136 | p=(CCObject *) x; |
| 137 | q=(CCObject *) y; |
| 138 | return((int) (q->area-(ssize_t) p->area)); |
| 139 | } |
| 140 | |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 141 | static MagickBooleanType MergeConnectedComponents(Image *image, |
| 142 | const size_t number_objects,const double area_threshold, |
| 143 | ExceptionInfo *exception) |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 144 | { |
| 145 | CacheView |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 146 | *image_view; |
| 147 | |
| 148 | CCObject |
| 149 | *object; |
| 150 | |
| 151 | MagickBooleanType |
| 152 | status; |
| 153 | |
| 154 | register ssize_t |
| 155 | i; |
| 156 | |
| 157 | ssize_t |
| 158 | y; |
| 159 | |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 160 | /* |
| 161 | Collect statistics on unique objects. |
| 162 | */ |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 163 | object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object)); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 164 | if (object == (CCObject *) NULL) |
| 165 | { |
| 166 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 167 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 168 | return(MagickFalse); |
| 169 | } |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 170 | (void) ResetMagickMemory(object,0,number_objects*sizeof(*object)); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 171 | for (i=0; i < (ssize_t) number_objects; i++) |
| 172 | { |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 173 | object[i].id=i; |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 174 | object[i].bounding_box.x=(ssize_t) image->columns; |
| 175 | object[i].bounding_box.y=(ssize_t) image->rows; |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 176 | } |
| 177 | status=MagickTrue; |
| 178 | image_view=AcquireVirtualCacheView(image,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 179 | for (y=0; y < (ssize_t) image->rows; y++) |
| 180 | { |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 181 | register const Quantum |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 182 | *restrict p; |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 183 | |
| 184 | register ssize_t |
| 185 | x; |
| 186 | |
| 187 | if (status == MagickFalse) |
| 188 | continue; |
| 189 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 190 | if (p == (const Quantum *) NULL) |
| 191 | { |
| 192 | status=MagickFalse; |
| 193 | continue; |
| 194 | } |
| 195 | for (x=0; x < (ssize_t) image->columns; x++) |
| 196 | { |
| cristy | 2ce3d8d | 2015-07-19 18:53:01 +0000 | [diff] [blame] | 197 | i=(ssize_t) GetPixelIntensity(image,p); |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 198 | if (x < object[i].bounding_box.x) |
| 199 | object[i].bounding_box.x=x; |
| 200 | if (x > (ssize_t) object[i].bounding_box.width) |
| 201 | object[i].bounding_box.width=(size_t) x; |
| 202 | if (y < object[i].bounding_box.y) |
| 203 | object[i].bounding_box.y=y; |
| 204 | if (y > (ssize_t) object[i].bounding_box.height) |
| 205 | object[i].bounding_box.height=(size_t) y; |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 206 | object[i].area++; |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 207 | p+=GetPixelChannels(image); |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
| cristy | 730e8f2 | 2014-10-27 00:30:57 +0000 | [diff] [blame] | 210 | image_view=DestroyCacheView(image_view); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 211 | for (i=0; i < (ssize_t) number_objects; i++) |
| 212 | { |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 213 | object[i].bounding_box.width-=(object[i].bounding_box.x-1); |
| 214 | object[i].bounding_box.height-=(object[i].bounding_box.y-1); |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 215 | } |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 216 | /* |
| 217 | Merge objects below area threshold. |
| 218 | */ |
| cristy | 730e8f2 | 2014-10-27 00:30:57 +0000 | [diff] [blame] | 219 | image_view=AcquireAuthenticCacheView(image,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 220 | for (i=0; i < (ssize_t) number_objects; i++) |
| 221 | { |
| 222 | double |
| 223 | census; |
| 224 | |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 225 | RectangleInfo |
| 226 | bounding_box; |
| 227 | |
| 228 | register ssize_t |
| 229 | j; |
| 230 | |
| 231 | size_t |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 232 | id; |
| 233 | |
| 234 | if (status == MagickFalse) |
| 235 | continue; |
| 236 | if ((double) object[i].area >= area_threshold) |
| 237 | continue; |
| 238 | for (j=0; j < (ssize_t) number_objects; j++) |
| 239 | object[j].census=0; |
| 240 | bounding_box=object[i].bounding_box; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 241 | for (y=0; y < (ssize_t) bounding_box.height+2; y++) |
| 242 | { |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 243 | register const Quantum |
| 244 | *restrict p; |
| 245 | |
| 246 | register ssize_t |
| 247 | x; |
| 248 | |
| 249 | if (status == MagickFalse) |
| 250 | continue; |
| 251 | p=GetCacheViewVirtualPixels(image_view,bounding_box.x-1,bounding_box.y+y- |
| 252 | 1,bounding_box.width+2,1,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 253 | if (p == (const Quantum *) NULL) |
| 254 | { |
| 255 | status=MagickFalse; |
| 256 | continue; |
| 257 | } |
| 258 | for (x=0; x < (ssize_t) bounding_box.width+2; x++) |
| 259 | { |
| cristy | 2ce3d8d | 2015-07-19 18:53:01 +0000 | [diff] [blame] | 260 | j=(ssize_t) GetPixelIntensity(image,p); |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 261 | if (j != i) |
| 262 | object[j].census++; |
| 263 | p+=GetPixelChannels(image); |
| 264 | } |
| 265 | } |
| 266 | census=0; |
| 267 | id=0; |
| 268 | for (j=0; j < (ssize_t) number_objects; j++) |
| cristy | 98973fa | 2014-12-23 00:52:04 +0000 | [diff] [blame] | 269 | if (census < object[j].census) |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 270 | { |
| 271 | census=object[j].census; |
| 272 | id=(size_t) j; |
| 273 | } |
| cristy | bd80771 | 2014-12-20 02:18:13 +0000 | [diff] [blame] | 274 | object[id].area+=object[i].area; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 275 | for (y=0; y < (ssize_t) bounding_box.height; y++) |
| 276 | { |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 277 | register Quantum |
| 278 | *restrict q; |
| 279 | |
| 280 | register ssize_t |
| 281 | x; |
| 282 | |
| 283 | if (status == MagickFalse) |
| 284 | continue; |
| 285 | q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,bounding_box.y+y, |
| 286 | bounding_box.width,1,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 287 | if (q == (Quantum *) NULL) |
| 288 | { |
| 289 | status=MagickFalse; |
| 290 | continue; |
| 291 | } |
| 292 | for (x=0; x < (ssize_t) bounding_box.width; x++) |
| 293 | { |
| cristy | 2ce3d8d | 2015-07-19 18:53:01 +0000 | [diff] [blame] | 294 | if ((ssize_t) GetPixelIntensity(image,q) == i) |
| cristy | 82eae11 | 2014-10-27 11:37:17 +0000 | [diff] [blame] | 295 | *q=(Quantum) id; |
| 296 | q+=GetPixelChannels(image); |
| 297 | } |
| 298 | if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse) |
| 299 | status=MagickFalse; |
| 300 | } |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 301 | } |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 302 | image_view=DestroyCacheView(image_view); |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 303 | object=(CCObject *) RelinquishMagickMemory(object); |
| 304 | return(status); |
| 305 | } |
| 306 | |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 307 | static MagickBooleanType StatisticsComponentsStatistics(const Image *image, |
| 308 | const Image *component_image,const size_t number_objects, |
| 309 | ExceptionInfo *exception) |
| 310 | { |
| 311 | CacheView |
| 312 | *component_view, |
| 313 | *image_view; |
| 314 | |
| 315 | CCObject |
| 316 | *object; |
| 317 | |
| 318 | MagickBooleanType |
| 319 | status; |
| 320 | |
| 321 | register ssize_t |
| 322 | i; |
| 323 | |
| 324 | ssize_t |
| 325 | y; |
| 326 | |
| 327 | /* |
| 328 | Collect statistics on unique objects. |
| 329 | */ |
| 330 | object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object)); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 331 | if (object == (CCObject *) NULL) |
| 332 | { |
| 333 | (void) ThrowMagickException(exception,GetMagickModule(), |
| 334 | ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename); |
| 335 | return(MagickFalse); |
| 336 | } |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 337 | (void) ResetMagickMemory(object,0,number_objects*sizeof(*object)); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 338 | for (i=0; i < (ssize_t) number_objects; i++) |
| 339 | { |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 340 | object[i].id=i; |
| 341 | object[i].bounding_box.x=(ssize_t) component_image->columns; |
| 342 | object[i].bounding_box.y=(ssize_t) component_image->rows; |
| 343 | GetPixelInfo(image,&object[i].color); |
| 344 | } |
| 345 | status=MagickTrue; |
| 346 | image_view=AcquireVirtualCacheView(image,exception); |
| 347 | component_view=AcquireVirtualCacheView(component_image,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 348 | for (y=0; y < (ssize_t) image->rows; y++) |
| 349 | { |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 350 | register const Quantum |
| 351 | *restrict p, |
| 352 | *restrict q; |
| 353 | |
| 354 | register ssize_t |
| 355 | x; |
| 356 | |
| 357 | if (status == MagickFalse) |
| 358 | continue; |
| 359 | p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception); |
| 360 | q=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,1, |
| 361 | exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 362 | if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL)) |
| 363 | { |
| 364 | status=MagickFalse; |
| 365 | continue; |
| 366 | } |
| 367 | for (x=0; x < (ssize_t) image->columns; x++) |
| 368 | { |
| cristy | 2ce3d8d | 2015-07-19 18:53:01 +0000 | [diff] [blame] | 369 | i=(ssize_t) GetPixelIntensity(image,q); |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 370 | if (x < object[i].bounding_box.x) |
| 371 | object[i].bounding_box.x=x; |
| 372 | if (x > (ssize_t) object[i].bounding_box.width) |
| 373 | object[i].bounding_box.width=(size_t) x; |
| 374 | if (y < object[i].bounding_box.y) |
| 375 | object[i].bounding_box.y=y; |
| 376 | if (y > (ssize_t) object[i].bounding_box.height) |
| 377 | object[i].bounding_box.height=(size_t) y; |
| 378 | object[i].color.red+=GetPixelRed(image,p); |
| 379 | object[i].color.green+=GetPixelGreen(image,p); |
| 380 | object[i].color.blue+=GetPixelBlue(image,p); |
| 381 | object[i].color.alpha+=GetPixelAlpha(image,p); |
| 382 | object[i].color.black+=GetPixelBlack(image,p); |
| 383 | object[i].centroid.x+=x; |
| 384 | object[i].centroid.y+=y; |
| 385 | object[i].area++; |
| 386 | p+=GetPixelChannels(image); |
| 387 | q+=GetPixelChannels(component_image); |
| 388 | } |
| 389 | } |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 390 | for (i=0; i < (ssize_t) number_objects; i++) |
| 391 | { |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 392 | object[i].bounding_box.width-=(object[i].bounding_box.x-1); |
| 393 | object[i].bounding_box.height-=(object[i].bounding_box.y-1); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 394 | object[i].color.red=object[i].color.red/object[i].area; |
| 395 | object[i].color.green=object[i].color.green/object[i].area; |
| 396 | object[i].color.blue=object[i].color.blue/object[i].area; |
| 397 | object[i].color.alpha=object[i].color.alpha/object[i].area; |
| 398 | object[i].color.black=object[i].color.black/object[i].area; |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 399 | object[i].centroid.x=object[i].centroid.x/object[i].area; |
| 400 | object[i].centroid.y=object[i].centroid.y/object[i].area; |
| 401 | } |
| 402 | component_view=DestroyCacheView(component_view); |
| 403 | image_view=DestroyCacheView(image_view); |
| 404 | /* |
| 405 | Report statistics on unique objects. |
| 406 | */ |
| 407 | qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare); |
| 408 | (void) fprintf(stdout, |
| 409 | "Objects (id: bounding-box centroid area mean-color):\n"); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 410 | for (i=0; i < (ssize_t) number_objects; i++) |
| 411 | { |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 412 | char |
| cristy | 151b66d | 2015-04-15 10:50:31 +0000 | [diff] [blame] | 413 | mean_color[MagickPathExtent]; |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 414 | |
| 415 | if (status == MagickFalse) |
| 416 | break; |
| 417 | if (object[i].area < MagickEpsilon) |
| 418 | continue; |
| 419 | GetColorTuple(&object[i].color,MagickFalse,mean_color); |
| 420 | (void) fprintf(stdout, |
| 421 | " %.20g: %.20gx%.20g%+.20g%+.20g %.1f,%.1f %.20g %s\n",(double) |
| 422 | object[i].id,(double) object[i].bounding_box.width,(double) |
| 423 | object[i].bounding_box.height,(double) object[i].bounding_box.x, |
| 424 | (double) object[i].bounding_box.y,object[i].centroid.x, |
| 425 | object[i].centroid.y,(double) object[i].area,mean_color); |
| 426 | } |
| 427 | object=(CCObject *) RelinquishMagickMemory(object); |
| 428 | return(status); |
| 429 | } |
| 430 | |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 431 | MagickExport Image *ConnectedComponentsImage(const Image *image, |
| 432 | const size_t connectivity,ExceptionInfo *exception) |
| 433 | { |
| 434 | #define ConnectedComponentsImageTag "ConnectedComponents/Image" |
| 435 | |
| 436 | CacheView |
| 437 | *image_view, |
| 438 | *component_view; |
| 439 | |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 440 | const char |
| 441 | *artifact; |
| 442 | |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 443 | double |
| 444 | area_threshold; |
| 445 | |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 446 | Image |
| 447 | *component_image; |
| 448 | |
| 449 | MagickBooleanType |
| 450 | status; |
| 451 | |
| 452 | MagickOffsetType |
| 453 | progress; |
| 454 | |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 455 | MatrixInfo |
| 456 | *equivalences; |
| 457 | |
| 458 | size_t |
| 459 | size; |
| 460 | |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 461 | ssize_t |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 462 | n, |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 463 | y; |
| 464 | |
| 465 | /* |
| 466 | Initialize connected components image attributes. |
| 467 | */ |
| 468 | assert(image != (Image *) NULL); |
| cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 469 | assert(image->signature == MagickCoreSignature); |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 470 | if (image->debug != MagickFalse) |
| 471 | (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename); |
| 472 | assert(exception != (ExceptionInfo *) NULL); |
| cristy | e1c94d9 | 2015-06-28 12:16:33 +0000 | [diff] [blame] | 473 | assert(exception->signature == MagickCoreSignature); |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 474 | component_image=CloneImage(image,image->columns,image->rows,MagickTrue, |
| 475 | exception); |
| 476 | if (component_image == (Image *) NULL) |
| 477 | return((Image *) NULL); |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 478 | component_image->depth=MAGICKCORE_QUANTUM_DEPTH; |
| 479 | component_image->colorspace=GRAYColorspace; |
| cristy | 7ac8c5b | 2014-11-16 14:29:44 +0000 | [diff] [blame] | 480 | status=SetImageStorageClass(component_image,DirectClass,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 481 | if (status == MagickFalse) |
| 482 | { |
| 483 | component_image=DestroyImage(component_image); |
| 484 | return((Image *) NULL); |
| 485 | } |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 486 | /* |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 487 | Initialize connected components equivalences. |
| 488 | */ |
| 489 | size=image->columns*image->rows; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 490 | if (image->columns != (size/image->rows)) |
| 491 | { |
| 492 | component_image=DestroyImage(component_image); |
| 493 | ThrowImageException(ResourceLimitError,"MemoryAllocationFailed"); |
| 494 | } |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 495 | equivalences=AcquireMatrixInfo(size,1,sizeof(ssize_t),exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 496 | if (equivalences == (MatrixInfo *) NULL) |
| 497 | { |
| 498 | component_image=DestroyImage(component_image); |
| 499 | return((Image *) NULL); |
| 500 | } |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 501 | for (n=0; n < (ssize_t) (image->columns*image->rows); n++) |
| 502 | status=SetMatrixElement(equivalences,n,0,&n); |
| 503 | /* |
| 504 | Find connected components. |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 505 | */ |
| 506 | status=MagickTrue; |
| 507 | progress=0; |
| 508 | image_view=AcquireVirtualCacheView(image,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 509 | for (n=0; n < (ssize_t) (connectivity > 4 ? 4 : 2); n++) |
| 510 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 511 | ssize_t |
| 512 | connect4[2][2] = { { -1, 0 }, { 0, -1 } }, |
| 513 | connect8[4][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 } }, |
| 514 | dx, |
| 515 | dy; |
| 516 | |
| 517 | if (status == MagickFalse) |
| 518 | continue; |
| 519 | dy=connectivity > 4 ? connect8[n][0] : connect4[n][0]; |
| 520 | dx=connectivity > 4 ? connect8[n][1] : connect4[n][1]; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 521 | for (y=0; y < (ssize_t) image->rows; y++) |
| 522 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 523 | register const Quantum |
| 524 | *restrict p; |
| 525 | |
| 526 | register ssize_t |
| 527 | x; |
| 528 | |
| 529 | if (status == MagickFalse) |
| 530 | continue; |
| 531 | p=GetCacheViewVirtualPixels(image_view,0,y-1,image->columns,3,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 532 | if (p == (const Quantum *) NULL) |
| 533 | { |
| 534 | status=MagickFalse; |
| 535 | continue; |
| 536 | } |
| cristy | 4f7c434 | 2014-12-07 15:55:04 +0000 | [diff] [blame] | 537 | p+=GetPixelChannels(image)*image->columns; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 538 | for (x=0; x < (ssize_t) image->columns; x++) |
| 539 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 540 | PixelInfo |
| 541 | pixel, |
| 542 | target; |
| 543 | |
| 544 | ssize_t |
| 545 | neighbor_offset, |
| 546 | object, |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 547 | offset, |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 548 | ox, |
| 549 | oy, |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 550 | root; |
| 551 | |
| 552 | /* |
| 553 | Is neighbor an authentic pixel and a different color than the pixel? |
| 554 | */ |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 555 | GetPixelInfoPixel(image,p,&pixel); |
| cristy | 4f7c434 | 2014-12-07 15:55:04 +0000 | [diff] [blame] | 556 | neighbor_offset=dy*(GetPixelChannels(image)*image->columns)+dx* |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 557 | GetPixelChannels(image); |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 558 | GetPixelInfoPixel(image,p+neighbor_offset,&target); |
| 559 | if (((x+dx) < 0) || ((x+dx) >= (ssize_t) image->columns) || |
| 560 | ((y+dy) < 0) || ((y+dy) >= (ssize_t) image->rows) || |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 561 | (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse)) |
| 562 | { |
| 563 | p+=GetPixelChannels(image); |
| 564 | continue; |
| 565 | } |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 566 | /* |
| 567 | Resolve this equivalence. |
| 568 | */ |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 569 | offset=y*image->columns+x; |
| 570 | neighbor_offset=dy*image->columns+dx; |
| 571 | ox=offset; |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 572 | status=GetMatrixElement(equivalences,ox,0,&object); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 573 | while (object != ox) |
| 574 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 575 | ox=object; |
| 576 | status=GetMatrixElement(equivalences,ox,0,&object); |
| 577 | } |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 578 | oy=offset+neighbor_offset; |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 579 | status=GetMatrixElement(equivalences,oy,0,&object); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 580 | while (object != oy) |
| 581 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 582 | oy=object; |
| 583 | status=GetMatrixElement(equivalences,oy,0,&object); |
| 584 | } |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 585 | if (ox < oy) |
| 586 | { |
| 587 | status=SetMatrixElement(equivalences,oy,0,&ox); |
| 588 | root=ox; |
| 589 | } |
| 590 | else |
| 591 | { |
| 592 | status=SetMatrixElement(equivalences,ox,0,&oy); |
| 593 | root=oy; |
| 594 | } |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 595 | ox=offset; |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 596 | status=GetMatrixElement(equivalences,ox,0,&object); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 597 | while (object != root) |
| 598 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 599 | status=GetMatrixElement(equivalences,ox,0,&object); |
| 600 | status=SetMatrixElement(equivalences,ox,0,&root); |
| 601 | } |
| cristy | 00f8eb4 | 2014-10-25 13:46:10 +0000 | [diff] [blame] | 602 | oy=offset+neighbor_offset; |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 603 | status=GetMatrixElement(equivalences,oy,0,&object); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 604 | while (object != root) |
| 605 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 606 | status=GetMatrixElement(equivalences,oy,0,&object); |
| 607 | status=SetMatrixElement(equivalences,oy,0,&root); |
| 608 | } |
| 609 | status=SetMatrixElement(equivalences,y*image->columns+x,0,&root); |
| 610 | p+=GetPixelChannels(image); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | image_view=DestroyCacheView(image_view); |
| 615 | /* |
| 616 | Label connected components. |
| 617 | */ |
| 618 | n=0; |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 619 | component_view=AcquireAuthenticCacheView(component_image,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 620 | for (y=0; y < (ssize_t) component_image->rows; y++) |
| 621 | { |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 622 | register Quantum |
| 623 | *restrict q; |
| 624 | |
| 625 | register ssize_t |
| 626 | x; |
| 627 | |
| 628 | if (status == MagickFalse) |
| 629 | continue; |
| 630 | q=QueueCacheViewAuthenticPixels(component_view,0,y,component_image->columns, |
| 631 | 1,exception); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 632 | if (q == (Quantum *) NULL) |
| 633 | { |
| 634 | status=MagickFalse; |
| 635 | continue; |
| 636 | } |
| 637 | for (x=0; x < (ssize_t) component_image->columns; x++) |
| 638 | { |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 639 | ssize_t |
| 640 | object, |
| 641 | offset; |
| 642 | |
| 643 | offset=y*image->columns+x; |
| 644 | status=GetMatrixElement(equivalences,offset,0,&object); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 645 | if (object == offset) |
| 646 | { |
| 647 | object=n++; |
| 648 | status=SetMatrixElement(equivalences,offset,0,&object); |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | status=GetMatrixElement(equivalences,object,0,&object); |
| 653 | status=SetMatrixElement(equivalences,offset,0,&object); |
| 654 | } |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 655 | *q=(Quantum) (object > (ssize_t) QuantumRange ? (ssize_t) QuantumRange : |
| 656 | object); |
| 657 | q+=GetPixelChannels(component_image); |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 658 | } |
| 659 | if (SyncCacheViewAuthenticPixels(component_view,exception) == MagickFalse) |
| 660 | status=MagickFalse; |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 661 | if (image->progress_monitor != (MagickProgressMonitor) NULL) |
| 662 | { |
| 663 | MagickBooleanType |
| 664 | proceed; |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 665 | |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 666 | proceed=SetImageProgress(image,ConnectedComponentsImageTag,progress++, |
| 667 | image->rows); |
| 668 | if (proceed == MagickFalse) |
| 669 | status=MagickFalse; |
| 670 | } |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 671 | } |
| 672 | component_view=DestroyCacheView(component_view); |
| cristy | 016b764 | 2014-10-25 13:09:57 +0000 | [diff] [blame] | 673 | equivalences=DestroyMatrixInfo(equivalences); |
| cristy | 3bdd925 | 2014-12-21 20:01:43 +0000 | [diff] [blame] | 674 | if (n > (ssize_t) QuantumRange) |
| 675 | { |
| 676 | component_image=DestroyImage(component_image); |
| 677 | ThrowImageException(ResourceLimitError,"TooManyObjects"); |
| 678 | } |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 679 | artifact=GetImageArtifact(image,"connected-components:area-threshold"); |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 680 | area_threshold=0.0; |
| cristy | c6ca571 | 2014-10-26 22:13:07 +0000 | [diff] [blame] | 681 | if (artifact != (const char *) NULL) |
| 682 | area_threshold=StringToDouble(artifact,(char **) NULL); |
| cristy | 4089840 | 2014-10-26 22:22:02 +0000 | [diff] [blame] | 683 | if (area_threshold > 0.0) |
| cristy | 7f4f7c6 | 2014-10-27 00:29:16 +0000 | [diff] [blame] | 684 | status=MergeConnectedComponents(component_image,(size_t) n,area_threshold, |
| 685 | exception); |
| cristy | dfc9c0e | 2014-10-31 17:20:14 +0000 | [diff] [blame] | 686 | artifact=GetImageArtifact(image,"connected-components:verbose"); |
| 687 | if (IsStringTrue(artifact) != MagickFalse) |
| cristy | 8bd0b70 | 2014-11-01 13:00:45 +0000 | [diff] [blame] | 688 | status=StatisticsComponentsStatistics(image,component_image,(size_t) n, |
| cristy | dfc9c0e | 2014-10-31 17:20:14 +0000 | [diff] [blame] | 689 | exception); |
| cristy | 6e0b3bc | 2014-10-19 17:51:42 +0000 | [diff] [blame] | 690 | if (status == MagickFalse) |
| 691 | component_image=DestroyImage(component_image); |
| 692 | return(component_image); |
| 693 | } |