blob: f08ff9a4539fb22ac2f39f7d570f100dc5070730 [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
124 ssize_t
125 area;
126} CCObject;
127
128static int CCObjectCompare(const void *x,const void *y)
129{
130 CCObject
131 *p,
132 *q;
133
134 p=(CCObject *) x;
135 q=(CCObject *) y;
136 return((int) (q->area-(ssize_t) p->area));
137}
138
139static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
cristyc6ca5712014-10-26 22:13:07 +0000140 const Image *component_image,const size_t number_objects,
141 ExceptionInfo *exception)
cristy016b7642014-10-25 13:09:57 +0000142{
143 CacheView
cristyc6ca5712014-10-26 22:13:07 +0000144 *component_view,
cristy016b7642014-10-25 13:09:57 +0000145 *image_view;
146
147 CCObject
148 *object;
149
150 MagickBooleanType
151 status;
152
153 register ssize_t
154 i;
155
156 ssize_t
157 y;
158
159 object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
160 if (object == (CCObject *) NULL)
161 {
162 (void) ThrowMagickException(exception,GetMagickModule(),
163 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
164 return(MagickFalse);
165 }
166 (void) ResetMagickMemory(object,0,number_objects*sizeof(*object));
167 for (i=0; i < (ssize_t) number_objects; i++)
168 {
169 object[i].id=i;
cristyc6ca5712014-10-26 22:13:07 +0000170 object[i].bounding_box.x=(ssize_t) component_image->columns;
171 object[i].bounding_box.y=(ssize_t) component_image->rows;
172 GetPixelInfo(image,&object[i].color);
cristy016b7642014-10-25 13:09:57 +0000173 }
cristy016b7642014-10-25 13:09:57 +0000174 status=MagickTrue;
175 image_view=AcquireVirtualCacheView(image,exception);
cristyc6ca5712014-10-26 22:13:07 +0000176 component_view=AcquireVirtualCacheView(component_image,exception);
cristy016b7642014-10-25 13:09:57 +0000177 for (y=0; y < (ssize_t) image->rows; y++)
178 {
179 register const Quantum
cristyc6ca5712014-10-26 22:13:07 +0000180 *restrict p,
181 *restrict q;
cristy016b7642014-10-25 13:09:57 +0000182
183 register ssize_t
184 x;
185
186 if (status == MagickFalse)
187 continue;
188 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
cristyc6ca5712014-10-26 22:13:07 +0000189 q=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,1,
190 exception);
191 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
cristy016b7642014-10-25 13:09:57 +0000192 {
193 status=MagickFalse;
194 continue;
195 }
196 for (x=0; x < (ssize_t) image->columns; x++)
197 {
cristyc6ca5712014-10-26 22:13:07 +0000198 i=(ssize_t) *q;
cristy016b7642014-10-25 13:09:57 +0000199 if (x < object[i].bounding_box.x)
200 object[i].bounding_box.x=x;
201 if (x > (ssize_t) object[i].bounding_box.width)
202 object[i].bounding_box.width=(size_t) x;
203 if (y < object[i].bounding_box.y)
204 object[i].bounding_box.y=y;
205 if (y > (ssize_t) object[i].bounding_box.height)
206 object[i].bounding_box.height=(size_t) y;
cristyc6ca5712014-10-26 22:13:07 +0000207 object[i].color.red+=GetPixelRed(image,p);
208 object[i].color.green+=GetPixelGreen(image,p);
209 object[i].color.blue+=GetPixelBlue(image,p);
210 object[i].color.alpha+=GetPixelAlpha(image,p);
211 object[i].color.black+=GetPixelBlack(image,p);
cristyc75924c2014-10-25 14:58:34 +0000212 object[i].centroid.x+=x;
213 object[i].centroid.y+=y;
cristy016b7642014-10-25 13:09:57 +0000214 object[i].area++;
215 p+=GetPixelChannels(image);
cristyc6ca5712014-10-26 22:13:07 +0000216 q+=GetPixelChannels(component_image);
cristy016b7642014-10-25 13:09:57 +0000217 }
218 }
219 for (i=0; i < (ssize_t) number_objects; i++)
220 {
221 object[i].bounding_box.width-=(object[i].bounding_box.x-1);
222 object[i].bounding_box.height-=(object[i].bounding_box.y-1);
cristyc6ca5712014-10-26 22:13:07 +0000223 object[i].color.red=object[i].color.red/object[i].area;
224 object[i].color.green=object[i].color.green/object[i].area;
225 object[i].color.blue=object[i].color.blue/object[i].area;
226 object[i].color.alpha=object[i].color.alpha/object[i].area;
227 object[i].color.black=object[i].color.black/object[i].area;
cristyc75924c2014-10-25 14:58:34 +0000228 object[i].centroid.x=object[i].centroid.x/object[i].area;
229 object[i].centroid.y=object[i].centroid.y/object[i].area;
cristy016b7642014-10-25 13:09:57 +0000230 }
cristyc6ca5712014-10-26 22:13:07 +0000231 component_view=DestroyCacheView(component_view);
cristy016b7642014-10-25 13:09:57 +0000232 image_view=DestroyCacheView(image_view);
233 qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare);
cristyc6ca5712014-10-26 22:13:07 +0000234 (void) fprintf(stdout,
235 "Objects (id: bounding-box centroid area mean-color):\n");
cristy016b7642014-10-25 13:09:57 +0000236 for (i=0; i < (ssize_t) number_objects; i++)
237 {
cristyc6ca5712014-10-26 22:13:07 +0000238 char
239 mean_color[MaxTextExtent];
240
cristy016b7642014-10-25 13:09:57 +0000241 if (status == MagickFalse)
242 break;
cristyc6ca5712014-10-26 22:13:07 +0000243 GetColorTuple(&object[i].color,MagickFalse,mean_color);
cristy016b7642014-10-25 13:09:57 +0000244 (void) fprintf(stdout,
cristyc6ca5712014-10-26 22:13:07 +0000245 " %.20g: %.20gx%.20g%+.20g%+.20g %+.1f,%+.1f %.20g %s\n",(double)
cristy016b7642014-10-25 13:09:57 +0000246 object[i].id,(double) object[i].bounding_box.width,(double)
247 object[i].bounding_box.height,(double) object[i].bounding_box.x,
248 (double) object[i].bounding_box.y,object[i].centroid.x,
cristyc6ca5712014-10-26 22:13:07 +0000249 object[i].centroid.y,(double) object[i].area,mean_color);
250 }
251 object=(CCObject *) RelinquishMagickMemory(object);
252 return(status);
253}
254
255static MagickBooleanType MergeConnectedComponents(const Image *image,
256 const Image *component_image,const size_t number_objects,
257 const double area_threshold,ExceptionInfo *exception)
258{
259 CacheView
260 *component_view,
261 *image_view;
262
263 CCObject
264 *object;
265
266 MagickBooleanType
267 status;
268
269 register ssize_t
270 i;
271
272 ssize_t
273 y;
274
275 object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
276 if (object == (CCObject *) NULL)
277 {
278 (void) ThrowMagickException(exception,GetMagickModule(),
279 ResourceLimitError,"MemoryAllocationFailed","`%s'",image->filename);
280 return(MagickFalse);
281 }
282 (void) ResetMagickMemory(object,0,number_objects*sizeof(*object));
283 for (i=0; i < (ssize_t) number_objects; i++)
284 {
285 object[i].id=i;
286 object[i].bounding_box.x=(ssize_t) component_image->columns;
287 object[i].bounding_box.y=(ssize_t) component_image->rows;
288 GetPixelInfo(image,&object[i].color);
289 }
290 status=MagickTrue;
291 image_view=AcquireVirtualCacheView(image,exception);
292 component_view=AcquireVirtualCacheView(component_image,exception);
293 for (y=0; y < (ssize_t) image->rows; y++)
294 {
295 register const Quantum
296 *restrict p,
297 *restrict q;
298
299 register ssize_t
300 x;
301
302 if (status == MagickFalse)
303 continue;
304 p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
305 q=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,1,
306 exception);
307 if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
308 {
309 status=MagickFalse;
310 continue;
311 }
312 for (x=0; x < (ssize_t) image->columns; x++)
313 {
314 i=(ssize_t) *q;
315 if (x < object[i].bounding_box.x)
316 object[i].bounding_box.x=x;
317 if (x > (ssize_t) object[i].bounding_box.width)
318 object[i].bounding_box.width=(size_t) x;
319 if (y < object[i].bounding_box.y)
320 object[i].bounding_box.y=y;
321 if (y > (ssize_t) object[i].bounding_box.height)
322 object[i].bounding_box.height=(size_t) y;
323 object[i].color.red+=GetPixelRed(image,p);
324 object[i].color.green+=GetPixelGreen(image,p);
325 object[i].color.blue+=GetPixelBlue(image,p);
326 object[i].color.alpha+=GetPixelAlpha(image,p);
327 object[i].color.black+=GetPixelBlack(image,p);
328 object[i].centroid.x+=x;
329 object[i].centroid.y+=y;
330 object[i].area++;
331 p+=GetPixelChannels(image);
332 q+=GetPixelChannels(component_image);
333 }
334 }
335 for (i=0; i < (ssize_t) number_objects; i++)
336 {
337 object[i].bounding_box.width-=(object[i].bounding_box.x-1);
338 object[i].bounding_box.height-=(object[i].bounding_box.y-1);
339 object[i].color.red=object[i].color.red/object[i].area;
340 object[i].color.green=object[i].color.green/object[i].area;
341 object[i].color.blue=object[i].color.blue/object[i].area;
342 object[i].color.alpha=object[i].color.alpha/object[i].area;
343 object[i].color.black=object[i].color.black/object[i].area;
344 object[i].centroid.x=object[i].centroid.x/object[i].area;
345 object[i].centroid.y=object[i].centroid.y/object[i].area;
346 }
347 component_view=DestroyCacheView(component_view);
348 image_view=DestroyCacheView(image_view);
349 qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare);
350 (void) fprintf(stdout,
351 "Objects (id: bounding-box centroid area mean-color):\n");
352 for (i=0; i < (ssize_t) number_objects; i++)
353 {
354 char
355 mean_color[MaxTextExtent];
356
357 if (status == MagickFalse)
358 break;
359 GetColorTuple(&object[i].color,MagickFalse,mean_color);
360 (void) fprintf(stdout,
361 " %.20g: %.20gx%.20g%+.20g%+.20g %+.1f,%+.1f %.20g %s\n",(double)
362 object[i].id,(double) object[i].bounding_box.width,(double)
363 object[i].bounding_box.height,(double) object[i].bounding_box.x,
364 (double) object[i].bounding_box.y,object[i].centroid.x,
365 object[i].centroid.y,(double) object[i].area,mean_color);
cristy016b7642014-10-25 13:09:57 +0000366 }
367 object=(CCObject *) RelinquishMagickMemory(object);
368 return(status);
369}
370
cristy6e0b3bc2014-10-19 17:51:42 +0000371MagickExport Image *ConnectedComponentsImage(const Image *image,
372 const size_t connectivity,ExceptionInfo *exception)
373{
374#define ConnectedComponentsImageTag "ConnectedComponents/Image"
375
376 CacheView
377 *image_view,
378 *component_view;
379
cristy016b7642014-10-25 13:09:57 +0000380 const char
381 *artifact;
382
cristyc6ca5712014-10-26 22:13:07 +0000383 double
384 area_threshold;
385
cristy6e0b3bc2014-10-19 17:51:42 +0000386 Image
387 *component_image;
388
389 MagickBooleanType
390 status;
391
392 MagickOffsetType
393 progress;
394
cristy016b7642014-10-25 13:09:57 +0000395 MatrixInfo
396 *equivalences;
397
398 size_t
399 size;
400
cristy6e0b3bc2014-10-19 17:51:42 +0000401 ssize_t
cristy016b7642014-10-25 13:09:57 +0000402 n,
cristy6e0b3bc2014-10-19 17:51:42 +0000403 y;
404
405 /*
406 Initialize connected components image attributes.
407 */
408 assert(image != (Image *) NULL);
409 assert(image->signature == MagickSignature);
410 if (image->debug != MagickFalse)
411 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
412 assert(exception != (ExceptionInfo *) NULL);
413 assert(exception->signature == MagickSignature);
414 component_image=CloneImage(image,image->columns,image->rows,MagickTrue,
415 exception);
416 if (component_image == (Image *) NULL)
417 return((Image *) NULL);
cristy016b7642014-10-25 13:09:57 +0000418 component_image->depth=MAGICKCORE_QUANTUM_DEPTH;
419 component_image->colorspace=GRAYColorspace;
cristy6e0b3bc2014-10-19 17:51:42 +0000420 if (SetImageStorageClass(component_image,DirectClass,exception) == MagickFalse)
421 {
422 component_image=DestroyImage(component_image);
423 return((Image *) NULL);
424 }
425 /*
cristy016b7642014-10-25 13:09:57 +0000426 Initialize connected components equivalences.
427 */
428 size=image->columns*image->rows;
429 if (image->columns != (size/image->rows))
430 {
431 component_image=DestroyImage(component_image);
432 ThrowImageException(ResourceLimitError,"MemoryAllocationFailed");
433 }
434 equivalences=AcquireMatrixInfo(size,1,sizeof(ssize_t),exception);
435 if (equivalences == (MatrixInfo *) NULL)
436 {
437 component_image=DestroyImage(component_image);
438 return((Image *) NULL);
439 }
440 for (n=0; n < (ssize_t) (image->columns*image->rows); n++)
441 status=SetMatrixElement(equivalences,n,0,&n);
442 /*
443 Find connected components.
cristy6e0b3bc2014-10-19 17:51:42 +0000444 */
445 status=MagickTrue;
446 progress=0;
447 image_view=AcquireVirtualCacheView(image,exception);
cristy016b7642014-10-25 13:09:57 +0000448 for (n=0; n < (ssize_t) (connectivity > 4 ? 4 : 2); n++)
449 {
450 ssize_t
451 connect4[2][2] = { { -1, 0 }, { 0, -1 } },
452 connect8[4][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 } },
453 dx,
454 dy;
455
456 if (status == MagickFalse)
457 continue;
458 dy=connectivity > 4 ? connect8[n][0] : connect4[n][0];
459 dx=connectivity > 4 ? connect8[n][1] : connect4[n][1];
460 for (y=0; y < (ssize_t) image->rows; y++)
461 {
462 register const Quantum
463 *restrict p;
464
465 register ssize_t
466 x;
467
468 if (status == MagickFalse)
469 continue;
470 p=GetCacheViewVirtualPixels(image_view,0,y-1,image->columns,3,exception);
471 if (p == (const Quantum *) NULL)
472 {
473 status=MagickFalse;
474 continue;
475 }
cristy00f8eb42014-10-25 13:46:10 +0000476 p+=image->columns*GetPixelChannels(image);
cristy016b7642014-10-25 13:09:57 +0000477 for (x=0; x < (ssize_t) image->columns; x++)
478 {
479 PixelInfo
480 pixel,
481 target;
482
483 ssize_t
484 neighbor_offset,
485 object,
cristy00f8eb42014-10-25 13:46:10 +0000486 offset,
cristy016b7642014-10-25 13:09:57 +0000487 ox,
488 oy,
cristy016b7642014-10-25 13:09:57 +0000489 root;
490
491 /*
492 Is neighbor an authentic pixel and a different color than the pixel?
493 */
cristy00f8eb42014-10-25 13:46:10 +0000494 GetPixelInfoPixel(image,p,&pixel);
cristy016b7642014-10-25 13:09:57 +0000495 neighbor_offset=dy*(image->columns*GetPixelChannels(image))+dx*
496 GetPixelChannels(image);
cristy016b7642014-10-25 13:09:57 +0000497 GetPixelInfoPixel(image,p+neighbor_offset,&target);
498 if (((x+dx) < 0) || ((x+dx) >= (ssize_t) image->columns) ||
499 ((y+dy) < 0) || ((y+dy) >= (ssize_t) image->rows) ||
500 (IsFuzzyEquivalencePixelInfo(&pixel,&target) == MagickFalse))
501 {
502 p+=GetPixelChannels(image);
503 continue;
504 }
505 /*
506 Resolve this equivalence.
507 */
cristy00f8eb42014-10-25 13:46:10 +0000508 offset=y*image->columns+x;
509 neighbor_offset=dy*image->columns+dx;
510 ox=offset;
cristy016b7642014-10-25 13:09:57 +0000511 status=GetMatrixElement(equivalences,ox,0,&object);
512 while (object != ox) {
513 ox=object;
514 status=GetMatrixElement(equivalences,ox,0,&object);
515 }
cristy00f8eb42014-10-25 13:46:10 +0000516 oy=offset+neighbor_offset;
cristy016b7642014-10-25 13:09:57 +0000517 status=GetMatrixElement(equivalences,oy,0,&object);
518 while (object != oy) {
519 oy=object;
520 status=GetMatrixElement(equivalences,oy,0,&object);
521 }
522 if (ox < oy)
523 {
524 status=SetMatrixElement(equivalences,oy,0,&ox);
525 root=ox;
526 }
527 else
528 {
529 status=SetMatrixElement(equivalences,ox,0,&oy);
530 root=oy;
531 }
cristy00f8eb42014-10-25 13:46:10 +0000532 ox=offset;
cristy016b7642014-10-25 13:09:57 +0000533 status=GetMatrixElement(equivalences,ox,0,&object);
534 while (object != root) {
535 status=GetMatrixElement(equivalences,ox,0,&object);
536 status=SetMatrixElement(equivalences,ox,0,&root);
537 }
cristy00f8eb42014-10-25 13:46:10 +0000538 oy=offset+neighbor_offset;
cristy016b7642014-10-25 13:09:57 +0000539 status=GetMatrixElement(equivalences,oy,0,&object);
540 while (object != root) {
541 status=GetMatrixElement(equivalences,oy,0,&object);
542 status=SetMatrixElement(equivalences,oy,0,&root);
543 }
544 status=SetMatrixElement(equivalences,y*image->columns+x,0,&root);
545 p+=GetPixelChannels(image);
546 }
547 }
548 }
549 image_view=DestroyCacheView(image_view);
550 /*
551 Label connected components.
552 */
553 n=0;
cristy6e0b3bc2014-10-19 17:51:42 +0000554 component_view=AcquireAuthenticCacheView(component_image,exception);
cristy6e0b3bc2014-10-19 17:51:42 +0000555 for (y=0; y < (ssize_t) component_image->rows; y++)
556 {
557 register Quantum
558 *restrict q;
559
560 register ssize_t
561 x;
562
563 if (status == MagickFalse)
564 continue;
565 q=QueueCacheViewAuthenticPixels(component_view,0,y,component_image->columns,
566 1,exception);
567 if (q == (Quantum *) NULL)
568 {
569 status=MagickFalse;
570 continue;
571 }
572 for (x=0; x < (ssize_t) component_image->columns; x++)
573 {
cristy016b7642014-10-25 13:09:57 +0000574 ssize_t
575 object,
576 offset;
577
578 offset=y*image->columns+x;
579 status=GetMatrixElement(equivalences,offset,0,&object);
580 if (object == offset)
581 {
582 object=n++;
583 status=SetMatrixElement(equivalences,offset,0,&object);
584 }
585 else
586 {
587 status=GetMatrixElement(equivalences,object,0,&object);
588 status=SetMatrixElement(equivalences,offset,0,&object);
589 }
590 *q=(Quantum) (object > (ssize_t) QuantumRange ? (ssize_t) QuantumRange :
591 object);
592 q+=GetPixelChannels(component_image);
cristy6e0b3bc2014-10-19 17:51:42 +0000593 }
594 if (SyncCacheViewAuthenticPixels(component_view,exception) == MagickFalse)
595 status=MagickFalse;
596 if (image->progress_monitor != (MagickProgressMonitor) NULL)
597 {
598 MagickBooleanType
599 proceed;
600
cristy6e0b3bc2014-10-19 17:51:42 +0000601 proceed=SetImageProgress(image,ConnectedComponentsImageTag,progress++,
602 image->rows);
603 if (proceed == MagickFalse)
604 status=MagickFalse;
605 }
606 }
607 component_view=DestroyCacheView(component_view);
cristy016b7642014-10-25 13:09:57 +0000608 equivalences=DestroyMatrixInfo(equivalences);
609 artifact=GetImageArtifact(image,"connected-components:verbose");
cristyc6ca5712014-10-26 22:13:07 +0000610 if (IsStringTrue(artifact) != MagickFalse)
611 status=ConnectedComponentsStatistics(image,component_image,(size_t) n,
612 exception);
613 artifact=GetImageArtifact(image,"connected-components:area-threshold");
614 if (artifact != (const char *) NULL)
615 area_threshold=StringToDouble(artifact,(char **) NULL);
616 if (area_threshold > 0.0);
617 status=MergeConnectedComponents(image,component_image,(size_t) n,
618 area_threshold,exception);
cristy6e0b3bc2014-10-19 17:51:42 +0000619 if (status == MagickFalse)
620 component_image=DestroyImage(component_image);
621 return(component_image);
622}