Skip to content

Commit 7f4f7c6

Browse files
author
cristy
committed
1 parent 4089840 commit 7f4f7c6

File tree

4 files changed

+25
-55
lines changed

4 files changed

+25
-55
lines changed

β€ŽMagickCore/vision.c

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
156156
ssize_t
157157
y;
158158

159+
/*
160+
Collect statistics on unique objects.
161+
*/
159162
object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
160163
if (object == (CCObject *) NULL)
161164
{
@@ -235,6 +238,9 @@ static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
235238
}
236239
component_view=DestroyCacheView(component_view);
237240
image_view=DestroyCacheView(image_view);
241+
/*
242+
Report statistics on unique objects.
243+
*/
238244
qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare);
239245
(void) fprintf(stdout,
240246
"Objects (id: bounding-box centroid area mean-color):\n");
@@ -247,7 +253,7 @@ static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
247253
break;
248254
GetColorTuple(&object[i].color,MagickFalse,mean_color);
249255
(void) fprintf(stdout,
250-
" %.20g: %.20gx%.20g%+.20g%+.20g %+.1f,%+.1f %.20g %s\n",(double)
256+
" %.20g: %.20gx%.20g%+.20g%+.20g %.1f,%.1f %.20g %s\n",(double)
251257
object[i].id,(double) object[i].bounding_box.width,(double)
252258
object[i].bounding_box.height,(double) object[i].bounding_box.x,
253259
(double) object[i].bounding_box.y,object[i].centroid.x,
@@ -257,12 +263,11 @@ static MagickBooleanType ConnectedComponentsStatistics(const Image *image,
257263
return(status);
258264
}
259265

260-
static MagickBooleanType MergeConnectedComponents(const Image *image,
261-
const Image *component_image,const size_t number_objects,
262-
const double area_threshold,ExceptionInfo *exception)
266+
static MagickBooleanType MergeConnectedComponents(Image *image,
267+
const size_t number_objects,const double area_threshold,
268+
ExceptionInfo *exception)
263269
{
264270
CacheView
265-
*component_view,
266271
*image_view;
267272

268273
CCObject
@@ -277,6 +282,9 @@ static MagickBooleanType MergeConnectedComponents(const Image *image,
277282
ssize_t
278283
y;
279284

285+
/*
286+
Collect statistics on unique objects.
287+
*/
280288
object=(CCObject *) AcquireQuantumMemory(number_objects,sizeof(*object));
281289
if (object == (CCObject *) NULL)
282290
{
@@ -288,35 +296,30 @@ static MagickBooleanType MergeConnectedComponents(const Image *image,
288296
for (i=0; i < (ssize_t) number_objects; i++)
289297
{
290298
object[i].id=i;
291-
object[i].bounding_box.x=(ssize_t) component_image->columns;
292-
object[i].bounding_box.y=(ssize_t) component_image->rows;
293-
GetPixelInfo(image,&object[i].color);
299+
object[i].bounding_box.x=(ssize_t) image->columns;
300+
object[i].bounding_box.y=(ssize_t) image->rows;
294301
}
295302
status=MagickTrue;
296303
image_view=AcquireVirtualCacheView(image,exception);
297-
component_view=AcquireVirtualCacheView(component_image,exception);
298304
for (y=0; y < (ssize_t) image->rows; y++)
299305
{
300306
register const Quantum
301-
*restrict p,
302-
*restrict q;
307+
*restrict p;
303308

304309
register ssize_t
305310
x;
306311

307312
if (status == MagickFalse)
308313
continue;
309314
p=GetCacheViewVirtualPixels(image_view,0,y,image->columns,1,exception);
310-
q=GetCacheViewVirtualPixels(component_view,0,y,component_image->columns,1,
311-
exception);
312-
if ((p == (const Quantum *) NULL) || (q == (const Quantum *) NULL))
315+
if (p == (const Quantum *) NULL)
313316
{
314317
status=MagickFalse;
315318
continue;
316319
}
317320
for (x=0; x < (ssize_t) image->columns; x++)
318321
{
319-
i=(ssize_t) *q;
322+
i=(ssize_t) *p;
320323
if (x < object[i].bounding_box.x)
321324
object[i].bounding_box.x=x;
322325
if (x > (ssize_t) object[i].bounding_box.width)
@@ -325,55 +328,21 @@ static MagickBooleanType MergeConnectedComponents(const Image *image,
325328
object[i].bounding_box.y=y;
326329
if (y > (ssize_t) object[i].bounding_box.height)
327330
object[i].bounding_box.height=(size_t) y;
328-
object[i].color.red+=GetPixelRed(image,p);
329-
object[i].color.green+=GetPixelGreen(image,p);
330-
object[i].color.blue+=GetPixelBlue(image,p);
331-
object[i].color.alpha+=GetPixelAlpha(image,p);
332-
object[i].color.black+=GetPixelBlack(image,p);
333-
object[i].centroid.x+=x;
334-
object[i].centroid.y+=y;
335-
object[i].area++;
336331
p+=GetPixelChannels(image);
337-
q+=GetPixelChannels(component_image);
338332
}
339333
}
340334
for (i=0; i < (ssize_t) number_objects; i++)
341335
{
342336
object[i].bounding_box.width-=(object[i].bounding_box.x-1);
343337
object[i].bounding_box.height-=(object[i].bounding_box.y-1);
344-
object[i].color.red=(MagickRealType) (object[i].color.red/
345-
object[i].area);
346-
object[i].color.green=(MagickRealType) (object[i].color.green/
347-
object[i].area);
348-
object[i].color.blue=(MagickRealType) (object[i].color.blue/
349-
object[i].area);
350-
object[i].color.alpha=(MagickRealType) (object[i].color.alpha/
351-
object[i].area);
352-
object[i].color.black=(MagickRealType) (object[i].color.black/
353-
object[i].area);
354-
object[i].centroid.x=object[i].centroid.x/object[i].area;
355-
object[i].centroid.y=object[i].centroid.y/object[i].area;
356338
}
357-
component_view=DestroyCacheView(component_view);
358-
image_view=DestroyCacheView(image_view);
359-
qsort((void *) object,number_objects,sizeof(*object),CCObjectCompare);
360-
(void) fprintf(stdout,
361-
"Objects (id: bounding-box centroid area mean-color):\n");
339+
/*
340+
Merge objects below area threshold.
341+
*/
362342
for (i=0; i < (ssize_t) number_objects; i++)
363343
{
364-
char
365-
mean_color[MaxTextExtent];
366-
367-
if (status == MagickFalse)
368-
break;
369-
GetColorTuple(&object[i].color,MagickFalse,mean_color);
370-
(void) fprintf(stdout,
371-
" %.20g: %.20gx%.20g%+.20g%+.20g %+.1f,%+.1f %.20g %s\n",(double)
372-
object[i].id,(double) object[i].bounding_box.width,(double)
373-
object[i].bounding_box.height,(double) object[i].bounding_box.x,
374-
(double) object[i].bounding_box.y,object[i].centroid.x,
375-
object[i].centroid.y,(double) object[i].area,mean_color);
376344
}
345+
image_view=DestroyCacheView(image_view);
377346
object=(CCObject *) RelinquishMagickMemory(object);
378347
return(status);
379348
}
@@ -621,11 +590,12 @@ MagickExport Image *ConnectedComponentsImage(const Image *image,
621590
status=ConnectedComponentsStatistics(image,component_image,(size_t) n,
622591
exception);
623592
artifact=GetImageArtifact(image,"connected-components:area-threshold");
593+
area_threshold=0.0;
624594
if (artifact != (const char *) NULL)
625595
area_threshold=StringToDouble(artifact,(char **) NULL);
626596
if (area_threshold > 0.0)
627-
status=MergeConnectedComponents(image,component_image,(size_t) n,
628-
area_threshold,exception);
597+
status=MergeConnectedComponents(component_image,(size_t) n,area_threshold,
598+
exception);
629599
if (status == MagickFalse)
630600
component_image=DestroyImage(component_image);
631601
return(component_image);

β€Žimages/objects.gif

1013 Bytes
Loading
File renamed without changes.

β€Žimages/purse.png

-640 Bytes
Binary file not shown.

0 commit comments

Comments
Β (0)