Connected-component labeling (alternatively connected-component analysis, blob extraction, region labeling, blob discovery, or region extraction) uniquely labels connected components in an image. The labeling process scans the image, pixel-by-pixel from top-left to bottom-right, in order to identify connected pixel regions, i.e. regions of adjacent pixels which share the same set of intensity values. For example, let's find the objects in this image:

    purse

To identify the objects in this image, use this command:

convert objects.gif -connected-components 4 -auto-level -depth 8 objects.png

The detected objects are uniquely labeled starting with 1. Use auto leveling to visualize the detected objects:

    Objects

Note, the -connected-components option requires ImageMagick version 6.8.9-10 or later.

Object statistics is useful to review. To display them, use this command:

convert objects.gif -define connected-components:verbose=true -connected-components 4 objects.png

Five objects were detected with these statistics:

Objects (id: bounding-box centroid area mean-color):
  0: 256x171+0+0 +119.2,+80.8 33117 gray(0)
  2: 120x135+104+18 +159.5,+106.5 8690 gray(255)
  3: 50x36+129+44 +154.2,+63.4 1529 gray(0)
  4: 21x23+0+45 +8.8,+55.9 409 gray(255)
  1: 4x10+252+0 +253.9,+4.1 31 gray(255)

Use -connected-components 8 to visit 8 neighbors rather than 4. By default, neighbor colors must be exact to be part of a unique object. Use -fuzz to include pixels that are close in color.

You might want to eliminate small objects by merging them with their larger neighbors. If so, use this command:

convert objects.gif -define connected-components:area-threshold=410 -connected-components 4 \
  -auto-level objects.jpg

Here are the expected results. Notice, how the small objects are now merged with the background.

Objects

Use the -fuzz option to identify objects that are made up of pixels similiar in color (e.g. -fuzz 3%).