blob: bfee1fd40b050673a1e04a383a3c41611ec5ef7e [file] [log] [blame]
cristy6e0b3bc2014-10-19 17:51:42 +00001/*
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% %
20% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39#include "MagickCore/studio.h"
cristy016b7642014-10-25 13:09:57 +000040#include "MagickCore/artifact.h"
cristy6e0b3bc2014-10-19 17:51:42 +000041#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"
73#include "MagickCore/resource_.h"
74#include "MagickCore/signature-private.h"
75#include "MagickCore/string_.h"
cristyc6ca5712014-10-26 22:13:07 +000076#include "MagickCore/string-private.h"
cristy6e0b3bc2014-10-19 17:51:42 +000077#include "MagickCore/thread-private.h"
cristy016b7642014-10-25 13:09:57 +000078#include "MagickCore/token.h"
cristy6e0b3bc2014-10-19 17:51:42 +000079#include "MagickCore/vision.h"
80
81/*
82%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83% %
84% %
85% %
86% C o n n e c t e d C o m p o n e n t s I m a g e %
87% %
88% %
89% %
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91%
92% ConnectedComponentsImage() returns the connected-components of the image
93% uniquely labeled. Choose from 4 or 8-way connectivity.
94%
95% The format of the ConnectedComponentsImage method is:
96%
97% Image *ConnectedComponentsImage(const Image *image,
98% const size_t connectivity,ExceptionInfo *exception)
99%
100% A description of each parameter follows:
101%
102% o image: the image.
103%
cristy016b7642014-10-25 13:09:57 +0000104% o connectivity: how many neighbors to visit, choose from 4 or 8.
cristy6e0b3bc2014-10-19 17:51:42 +0000105%
106% o exception: return any errors or warnings in this structure.
107%
108*/
cristy016b7642014-10-25 13:09:57 +0000109
110typedef struct _CCObject
111{
112 ssize_t
113 id;
114
115 RectangleInfo
116 bounding_box;
117
cristyc6ca5712014-10-26 22:13:07 +0000118 PixelInfo
119 color;
120
cristy016b7642014-10-25 13:09:57 +0000121 PointInfo
122 centroid;
123
cristy82eae112014-10-27 11:37:17 +0000124 size_t
125 area,
126 census;
cristy016b7642014-10-25 13:09:57 +0000127} CCObject;
128
129static int CCObjectCompare(const void *x,const void *y)
130{
131 CCObject
132 *p,
133 *q;
134
135 p=(CCObject *) x;
136 q=(CCObject *) y;
137 return((int) (q->area-(ssize_t) p->area));
138}
139
140static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
cristyc6ca5712014-10-26 22:13:07 +0000141 const Image *component_image,const size_t number_objects,
142 ExceptionInfo *exception)
cristy016b7642014-10-25 13:09:57 +0000143{
144 CacheView
cristyc6ca5712014-10-26 22:13:07 +0000145 *component_view,
cristy016b7642014-10-25 13:09:57 +0000146 *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
cristy7f4f7c62014-10-27 00:29:16 +0000160 /*
161 Collect statistics on unique objects.
162 */
cristy016b7642014-10-25 13:09:57 +0000163 object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
164 if (object == (CCObject *) NULL)
165 {
166 (void) ThrowMagickException(exception,GetMagickModule(),
167 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
168 return(MagickFalse);
169 }
170 (void) ResetMagickMemory(object,0,number_objects*sizeof(*object));
171 for (i=0; i < (ssize_t) number_objects; i++)
172 {
173 object[i].id=i;
cristyc6ca5712014-10-26 22:13:07 +0000174 object[i].bounding_box.x=(ssize_t) component_image->columns;
175 object[i].bounding_box.y=(ssize_t) component_image->rows;
176 GetPixelInfo(image,&object[i].color);
cristy016b7642014-10-25 13:09:57 +0000177 }
cristy016b7642014-10-25 13:09:57 +0000178 status=MagickTrue;
179 image_view=AcquireVirtualCacheView(image,exception);
cristyc6ca5712014-10-26 22:13:07 +0000180 component_view=AcquireVirtualCacheView(component_image,exception);
cristy016b7642014-10-25 13:09:57 +0000181 for (y=0; y < (ssize_t) image->rows; y++)
182 {
183 register const Quantum
cristyc6ca5712014-10-26 22:13:07 +0000184 *restrict p,
185 *restrict q;
cristy016b7642014-10-25 13:09:57 +0000186
187 register ssize_t
188 x;
189
190 if (status == MagickFalse)
191 continue;
192 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristyc6ca5712014-10-26 22:13:07 +0000193 q=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,1,
194 exception);
195 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
cristy016b7642014-10-25 13:09:57 +0000196 {
197 status=MagickFalse;
198 continue;
199 }
200 for (x=0; x < (ssize_t) image->columns; x++)
201 {
cristyc6ca5712014-10-26 22:13:07 +0000202 i=(ssize_t) *q;
cristy016b7642014-10-25 13:09:57 +0000203 if (x < object[i].bounding_box.x)
204 object[i].bounding_box.x=x;
205 if (x > (ssize_t) object[i].bounding_box.width)
206 object[i].bounding_box.width=(size_t) x;
207 if (y < object[i].bounding_box.y)
208 object[i].bounding_box.y=y;
209 if (y > (ssize_t) object[i].bounding_box.height)
210 object[i].bounding_box.height=(size_t) y;
cristyc6ca5712014-10-26 22:13:07 +0000211 object[i].color.red+=GetPixelRed(image,p);
212 object[i].color.green+=GetPixelGreen(image,p);
213 object[i].color.blue+=GetPixelBlue(image,p);
214 object[i].color.alpha+=GetPixelAlpha(image,p);
215 object[i].color.black+=GetPixelBlack(image,p);
cristyc75924c2014-10-25 14:58:34 +0000216 object[i].centroid.x+=x;
217 object[i].centroid.y+=y;
cristy016b7642014-10-25 13:09:57 +0000218 object[i].area++;
219 p+=GetPixelChannels(image);
cristyc6ca5712014-10-26 22:13:07 +0000220 q+=GetPixelChannels(component_image);
cristy016b7642014-10-25 13:09:57 +0000221 }
222 }
223 for (i=0; i < (ssize_t) number_objects; i++)
224 {
225 object[i].bounding_box.width-=(object[i].bounding_box.x-1);
226 object[i].bounding_box.height-=(object[i].bounding_box.y-1);
cristy40898402014-10-26 22:22:02 +0000227 object[i].color.red=(MagickRealType) (object[i].color.red/
228 object[i].area);
229 object[i].color.green=(MagickRealType) (object[i].color.green/
230 object[i].area);
231 object[i].color.blue=(MagickRealType) (object[i].color.blue/
232 object[i].area);
233 object[i].color.alpha=(MagickRealType) (object[i].color.alpha/
234 object[i].area);
235 object[i].color.black=(MagickRealType) (object[i].color.black/
236 object[i].area);
cristyc75924c2014-10-25 14:58:34 +0000237 object[i].centroid.x=object[i].centroid.x/object[i].area;
238 object[i].centroid.y=object[i].centroid.y/object[i].area;
cristy016b7642014-10-25 13:09:57 +0000239 }
cristyc6ca5712014-10-26 22:13:07 +0000240 component_view=DestroyCacheView(component_view);
cristy016b7642014-10-25 13:09:57 +0000241 image_view=DestroyCacheView(image_view);
cristy7f4f7c62014-10-27 00:29:16 +0000242 /*
243 Report statistics on unique objects.
244 */
cristy016b7642014-10-25 13:09:57 +0000245 qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare);
cristyc6ca5712014-10-26 22:13:07 +0000246 (void) fprintf(stdout,
247 "Objects (id: bounding-box centroid area mean-color):\n");
cristy016b7642014-10-25 13:09:57 +0000248 for (i=0; i < (ssize_t) number_objects; i++)
249 {
cristyc6ca5712014-10-26 22:13:07 +0000250 char
251 mean_color[MaxTextExtent];
252
cristy016b7642014-10-25 13:09:57 +0000253 if (status == MagickFalse)
254 break;
cristyc6ca5712014-10-26 22:13:07 +0000255 GetColorTuple(&object[i].color,MagickFalse,mean_color);
cristy016b7642014-10-25 13:09:57 +0000256 (void) fprintf(stdout,
cristy7f4f7c62014-10-27 00:29:16 +0000257 " %.20g: %.20gx%.20g%+.20g%+.20g %.1f,%.1f %.20g %s\n",(double)
cristy016b7642014-10-25 13:09:57 +0000258 object[i].id,(double) object[i].bounding_box.width,(double)
259 object[i].bounding_box.height,(double) object[i].bounding_box.x,
260 (double) object[i].bounding_box.y,object[i].centroid.x,
cristyc6ca5712014-10-26 22:13:07 +0000261 object[i].centroid.y,(double) object[i].area,mean_color);
262 }
263 object=(CCObject *) RelinquishMagickMemory(object);
264 return(status);
265}
266
cristy7f4f7c62014-10-27 00:29:16 +0000267static MagickBooleanType MergeConnectedComponents(Image *image,
268 const size_t number_objects,const double area_threshold,
269 ExceptionInfo *exception)
cristyc6ca5712014-10-26 22:13:07 +0000270{
271 CacheView
cristyc6ca5712014-10-26 22:13:07 +0000272 *image_view;
273
274 CCObject
275 *object;
276
277 MagickBooleanType
278 status;
279
280 register ssize_t
281 i;
282
283 ssize_t
284 y;
285
cristy7f4f7c62014-10-27 00:29:16 +0000286 /*
287 Collect statistics on unique objects.
288 */
cristyc6ca5712014-10-26 22:13:07 +0000289 object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
290 if (object == (CCObject *) NULL)
291 {
292 (void) ThrowMagickException(exception,GetMagickModule(),
293 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
294 return(MagickFalse);
295 }
296 (void) ResetMagickMemory(object,0,number_objects*sizeof(*object));
297 for (i=0; i < (ssize_t) number_objects; i++)
298 {
299 object[i].id=i;
cristy7f4f7c62014-10-27 00:29:16 +0000300 object[i].bounding_box.x=(ssize_t) image->columns;
301 object[i].bounding_box.y=(ssize_t) image->rows;
cristyc6ca5712014-10-26 22:13:07 +0000302 }
303 status=MagickTrue;
304 image_view=AcquireVirtualCacheView(image,exception);
cristyc6ca5712014-10-26 22:13:07 +0000305 for (y=0; y < (ssize_t) image->rows; y++)
306 {
307 register const Quantum
cristy7f4f7c62014-10-27 00:29:16 +0000308 *restrict p;
cristyc6ca5712014-10-26 22:13:07 +0000309
310 register ssize_t
311 x;
312
313 if (status == MagickFalse)
314 continue;
315 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristy7f4f7c62014-10-27 00:29:16 +0000316 if (p == (const Quantum *) NULL)
cristyc6ca5712014-10-26 22:13:07 +0000317 {
318 status=MagickFalse;
319 continue;
320 }
321 for (x=0; x < (ssize_t) image->columns; x++)
322 {
cristy7f4f7c62014-10-27 00:29:16 +0000323 i=(ssize_t) *p;
cristyc6ca5712014-10-26 22:13:07 +0000324 if (x < object[i].bounding_box.x)
325 object[i].bounding_box.x=x;
326 if (x > (ssize_t) object[i].bounding_box.width)
327 object[i].bounding_box.width=(size_t) x;
328 if (y < object[i].bounding_box.y)
329 object[i].bounding_box.y=y;
330 if (y > (ssize_t) object[i].bounding_box.height)
331 object[i].bounding_box.height=(size_t) y;
cristy82eae112014-10-27 11:37:17 +0000332 object[i].area++;
cristyc6ca5712014-10-26 22:13:07 +0000333 p+=GetPixelChannels(image);
cristyc6ca5712014-10-26 22:13:07 +0000334 }
335 }
cristy730e8f22014-10-27 00:30:57 +0000336 image_view=DestroyCacheView(image_view);
cristyc6ca5712014-10-26 22:13:07 +0000337 for (i=0; i < (ssize_t) number_objects; i++)
338 {
339 object[i].bounding_box.width-=(object[i].bounding_box.x-1);
340 object[i].bounding_box.height-=(object[i].bounding_box.y-1);
cristyc6ca5712014-10-26 22:13:07 +0000341 }
cristy7f4f7c62014-10-27 00:29:16 +0000342 /*
343 Merge objects below area threshold.
344 */
cristy730e8f22014-10-27 00:30:57 +0000345 image_view=AcquireAuthenticCacheView(image,exception);
cristyc6ca5712014-10-26 22:13:07 +0000346 for (i=0; i < (ssize_t) number_objects; i++)
347 {
cristy82eae112014-10-27 11:37:17 +0000348 RectangleInfo
349 bounding_box;
350
351 register ssize_t
352 j;
353
354 size_t
355 census,
356 id;
357
358 if (status == MagickFalse)
359 continue;
360 if ((double) object[i].area >= area_threshold)
361 continue;
362 for (j=0; j < (ssize_t) number_objects; j++)
363 object[j].census=0;
364 bounding_box=object[i].bounding_box;
365 for (y=0; y < (ssize_t) bounding_box.height+2; y++)
366 {
367 register const Quantum
368 *restrict p;
369
370 register ssize_t
371 x;
372
373 if (status == MagickFalse)
374 continue;
375 p=GetCacheViewVirtualPixels(image_view,bounding_box.x-1,bounding_box.y+y-
376 1,bounding_box.width+2,1,exception);
377 if (p == (const Quantum *) NULL)
378 {
379 status=MagickFalse;
380 continue;
381 }
382 for (x=0; x < (ssize_t) bounding_box.width+2; x++)
383 {
384 j=(ssize_t) *p;
385 if (j != i)
386 object[j].census++;
387 p+=GetPixelChannels(image);
388 }
389 }
390 census=0;
391 id=0;
392 for (j=0; j < (ssize_t) number_objects; j++)
393 if (census < object[j].census)
394 {
395 census=object[j].census;
396 id=(size_t) j;
397 }
398 for (y=0; y < (ssize_t) bounding_box.height; y++)
399 {
400 register Quantum
401 *restrict q;
402
403 register ssize_t
404 x;
405
406 if (status == MagickFalse)
407 continue;
408 q=GetCacheViewAuthenticPixels(image_view,bounding_box.x,bounding_box.y+y,
409 bounding_box.width,1,exception);
410 if (q == (Quantum *) NULL)
411 {
412 status=MagickFalse;
413 continue;
414 }
415 for (x=0; x < (ssize_t) bounding_box.width; x++)
416 {
417 if ((ssize_t) *q == i)
418 *q=(Quantum) id;
419 q+=GetPixelChannels(image);
420 }
421 if (SyncCacheViewAuthenticPixels(image_view,exception) == MagickFalse)
422 status=MagickFalse;
423 }
cristy016b7642014-10-25 13:09:57 +0000424 }
cristy7f4f7c62014-10-27 00:29:16 +0000425 image_view=DestroyCacheView(image_view);
cristy016b7642014-10-25 13:09:57 +0000426 object=(CCObject *) RelinquishMagickMemory(object);
427 return(status);
428}
429
cristy6e0b3bc2014-10-19 17:51:42 +0000430MagickExport Image *ConnectedComponentsImage(const Image *image,
431 const size_t connectivity,ExceptionInfo *exception)
432{
433#define ConnectedComponentsImageTag "ConnectedComponents/Image"
434
435 CacheView
436 *image_view,
437 *component_view;
438
cristy016b7642014-10-25 13:09:57 +0000439 const char
440 *artifact;
441
cristyc6ca5712014-10-26 22:13:07 +0000442 double
443 area_threshold;
444
cristy6e0b3bc2014-10-19 17:51:42 +0000445 Image
446 *component_image;
447
448 MagickBooleanType
449 status;
450
451 MagickOffsetType
452 progress;
453
cristy016b7642014-10-25 13:09:57 +0000454 MatrixInfo
455 *equivalences;
456
457 size_t
458 size;
459
cristy6e0b3bc2014-10-19 17:51:42 +0000460 ssize_t
cristy016b7642014-10-25 13:09:57 +0000461 n,
cristy6e0b3bc2014-10-19 17:51:42 +0000462 y;
463
464 /*
465 Initialize connected components image attributes.
466 */
467 assert(image != (Image *) NULL);
468 assert(image->signature == MagickSignature);
469 if (image->debug != MagickFalse)
470 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
471 assert(exception != (ExceptionInfo *) NULL);
472 assert(exception->signature == MagickSignature);
473 component_image=CloneImage(image,image->columns,image->rows,MagickTrue,
474 exception);
475 if (component_image == (Image *) NULL)
476 return((Image *) NULL);
cristy016b7642014-10-25 13:09:57 +0000477 component_image->depth=MAGICKCORE_QUANTUM_DEPTH;
478 component_image->colorspace=GRAYColorspace;
cristy6e0b3bc2014-10-19 17:51:42 +0000479 if (SetImageStorageClass(component_image,DirectClass,exception) == MagickFalse)
480 {
481 component_image=DestroyImage(component_image);
482 return((Image *) NULL);
483 }
484 /*
cristy016b7642014-10-25 13:09:57 +0000485 Initialize connected components equivalences.
486 */
487 size=image->columns*image->rows;
488 if (image->columns != (size/image->rows))
489 {
490 component_image=DestroyImage(component_image);
491 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
492 }
493 equivalences=AcquireMatrixInfo(size,1,sizeof(ssize_t),exception);
494 if (equivalences == (MatrixInfo *) NULL)
495 {
496 component_image=DestroyImage(component_image);
497 return((Image *) NULL);
498 }
499 for (n=0; n < (ssize_t) (image->columns*image->rows); n++)
500 status=SetMatrixElement(equivalences,n,0,&n);
501 /*
502 Find connected components.
cristy6e0b3bc2014-10-19 17:51:42 +0000503 */
504 status=MagickTrue;
505 progress=0;
506 image_view=AcquireVirtualCacheView(image,exception);
cristy016b7642014-10-25 13:09:57 +0000507 for (n=0; n < (ssize_t) (connectivity > 4 ? 4 : 2); n++)
508 {
509 ssize_t
510 connect4[2][2] = { { -1, 0 }, { 0, -1 } },
511 connect8[4][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 } },
512 dx,
513 dy;
514
515 if (status == MagickFalse)
516 continue;
517 dy=connectivity > 4 ? connect8[n][0] : connect4[n][0];
518 dx=connectivity > 4 ? connect8[n][1] : connect4[n][1];
519 for (y=0; y < (ssize_t) image->rows; y++)
520 {
521 register const Quantum
522 *restrict p;
523
524 register ssize_t
525 x;
526
527 if (status == MagickFalse)
528 continue;
529 p=GetCacheViewVirtualPixels(image_view,0,y-1,image->columns,3,exception);
530 if (p == (const Quantum *) NULL)
531 {
532 status=MagickFalse;
533 continue;
534 }
cristy00f8eb42014-10-25 13:46:10 +0000535 p+=image->columns*GetPixelChannels(image);
cristy016b7642014-10-25 13:09:57 +0000536 for (x=0; x < (ssize_t) image->columns; x++)
537 {
538 PixelInfo
539 pixel,
540 target;
541
542 ssize_t
543 neighbor_offset,
544 object,
cristy00f8eb42014-10-25 13:46:10 +0000545 offset,
cristy016b7642014-10-25 13:09:57 +0000546 ox,
547 oy,
cristy016b7642014-10-25 13:09:57 +0000548 root;
549
550 /*
551 Is neighbor an authentic pixel and a different color than the pixel?
552 */
cristy00f8eb42014-10-25 13:46:10 +0000553 GetPixelInfoPixel(image,p,&pixel);
cristy016b7642014-10-25 13:09:57 +0000554 neighbor_offset=dy*(image->columns*GetPixelChannels(image))+dx*
555 GetPixelChannels(image);
cristy016b7642014-10-25 13:09:57 +0000556 GetPixelInfoPixel(image,p+neighbor_offset,&target);
557 if (((x+dx) < 0) || ((x+dx) >= (ssize_t) image->columns) ||
558 ((y+dy) < 0) || ((y+dy) >= (ssize_t) image->rows) ||
559 (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse))
560 {
561 p+=GetPixelChannels(image);
562 continue;
563 }
564 /*
565 Resolve this equivalence.
566 */
cristy00f8eb42014-10-25 13:46:10 +0000567 offset=y*image->columns+x;
568 neighbor_offset=dy*image->columns+dx;
569 ox=offset;
cristy016b7642014-10-25 13:09:57 +0000570 status=GetMatrixElement(equivalences,ox,0,&object);
571 while (object != ox) {
572 ox=object;
573 status=GetMatrixElement(equivalences,ox,0,&object);
574 }
cristy00f8eb42014-10-25 13:46:10 +0000575 oy=offset+neighbor_offset;
cristy016b7642014-10-25 13:09:57 +0000576 status=GetMatrixElement(equivalences,oy,0,&object);
577 while (object != oy) {
578 oy=object;
579 status=GetMatrixElement(equivalences,oy,0,&object);
580 }
581 if (ox < oy)
582 {
583 status=SetMatrixElement(equivalences,oy,0,&ox);
584 root=ox;
585 }
586 else
587 {
588 status=SetMatrixElement(equivalences,ox,0,&oy);
589 root=oy;
590 }
cristy00f8eb42014-10-25 13:46:10 +0000591 ox=offset;
cristy016b7642014-10-25 13:09:57 +0000592 status=GetMatrixElement(equivalences,ox,0,&object);
593 while (object != root) {
594 status=GetMatrixElement(equivalences,ox,0,&object);
595 status=SetMatrixElement(equivalences,ox,0,&root);
596 }
cristy00f8eb42014-10-25 13:46:10 +0000597 oy=offset+neighbor_offset;
cristy016b7642014-10-25 13:09:57 +0000598 status=GetMatrixElement(equivalences,oy,0,&object);
599 while (object != root) {
600 status=GetMatrixElement(equivalences,oy,0,&object);
601 status=SetMatrixElement(equivalences,oy,0,&root);
602 }
603 status=SetMatrixElement(equivalences,y*image->columns+x,0,&root);
604 p+=GetPixelChannels(image);
605 }
606 }
607 }
608 image_view=DestroyCacheView(image_view);
609 /*
610 Label connected components.
611 */
612 n=0;
cristy6e0b3bc2014-10-19 17:51:42 +0000613 component_view=AcquireAuthenticCacheView(component_image,exception);
cristy6e0b3bc2014-10-19 17:51:42 +0000614 for (y=0; y < (ssize_t) component_image->rows; y++)
615 {
616 register Quantum
617 *restrict q;
618
619 register ssize_t
620 x;
621
622 if (status == MagickFalse)
623 continue;
624 q=QueueCacheViewAuthenticPixels(component_view,0,y,component_image->columns,
625 1,exception);
626 if (q == (Quantum *) NULL)
627 {
628 status=MagickFalse;
629 continue;
630 }
631 for (x=0; x < (ssize_t) component_image->columns; x++)
632 {
cristy016b7642014-10-25 13:09:57 +0000633 ssize_t
634 object,
635 offset;
636
637 offset=y*image->columns+x;
638 status=GetMatrixElement(equivalences,offset,0,&object);
639 if (object == offset)
640 {
641 object=n++;
642 status=SetMatrixElement(equivalences,offset,0,&object);
643 }
644 else
645 {
646 status=GetMatrixElement(equivalences,object,0,&object);
647 status=SetMatrixElement(equivalences,offset,0,&object);
648 }
649 *q=(Quantum) (object > (ssize_t) QuantumRange ? (ssize_t) QuantumRange :
650 object);
651 q+=GetPixelChannels(component_image);
cristy6e0b3bc2014-10-19 17:51:42 +0000652 }
653 if (SyncCacheViewAuthenticPixels(component_view,exception) == MagickFalse)
654 status=MagickFalse;
655 if (image->progress_monitor != (MagickProgressMonitor) NULL)
656 {
657 MagickBooleanType
658 proceed;
659
cristy6e0b3bc2014-10-19 17:51:42 +0000660 proceed=SetImageProgress(image,ConnectedComponentsImageTag,progress++,
661 image->rows);
662 if (proceed == MagickFalse)
663 status=MagickFalse;
664 }
665 }
666 component_view=DestroyCacheView(component_view);
cristy016b7642014-10-25 13:09:57 +0000667 equivalences=DestroyMatrixInfo(equivalences);
668 artifact=GetImageArtifact(image,"connected-components:verbose");
cristyc6ca5712014-10-26 22:13:07 +0000669 if (IsStringTrue(artifact) != MagickFalse)
670 status=ConnectedComponentsStatistics(image,component_image,(size_t) n,
671 exception);
672 artifact=GetImageArtifact(image,"connected-components:area-threshold");
cristy7f4f7c62014-10-27 00:29:16 +0000673 area_threshold=0.0;
cristyc6ca5712014-10-26 22:13:07 +0000674 if (artifact != (const char *) NULL)
675 area_threshold=StringToDouble(artifact,(char **) NULL);
cristy40898402014-10-26 22:22:02 +0000676 if (area_threshold > 0.0)
cristy7f4f7c62014-10-27 00:29:16 +0000677 status=MergeConnectedComponents(component_image,(size_t) n,area_threshold,
678 exception);
cristy6e0b3bc2014-10-19 17:51:42 +0000679 if (status == MagickFalse)
680 component_image=DestroyImage(component_image);
681 return(component_image);
682}