bwconncomp

CC = bwconncomp(BW; conn=8)

Find and count connected components in binary image.

Description

Finds and counts the connected components in the binary image BW. The function returns a structure containing the total number of connected components, such as regions of interest (ROIs), in the image and the pixel indices assigned to each component.

Parameters

  • BW: Binary 2D image. For numeric input, any nonzero pixels are considered to be 1 (true).

    • Type: Array{<:Real} or GMTgrid

Keyword Arguments

  • conn: Connectivity for connected components

    • Type: Integer or Array

    • Default: 8

    • Options:

      • For 2D images:

        • 4 — 4-connected neighborhood (edge connectivity)

        • 8 — 8-connected neighborhood (edge and corner connectivity)

      • Alternatively, can be a connectivity array of 0s and 1s with the same dimensionality as BW. The 1-valued elements define neighborhood locations relative to the center element. The center element must be 1. The connectivity array size must be odd along each dimension.

Returns

Connected components, specified as a structure with the following fields:

FieldDescription
connectivityConnectivity of the connected components
image_sizeSize of the binary image
num_objectsNumber of connected components in the binary image
rangeRange of the image coordinates
incImage's increment (!= 1 whem image is referenced)
registrationRegistration of the image
xX coordinates of the image
yY coordinates of the image
layoutMemory layout of the image
proj4Projection definition (optional)
wktWell-known text definition (optional)
epsgEPSG code of the image
bboxThe bounding boxes as a vector of GMTdataset
pixel_listVector where each element contains the linear indices of the pixels in each object
centroidA Float64 Matrix with the x,y coordinates of the centroids for each component
areaA vector of Float64 with the areas of each component
  • To compute a label matrix with a memory-efficient data type (for instance, UInt8 versus Float64), use the labelmatrix function on the output of bwconncomp:

CC = bwconncomp(BW)
L = labelmatrix(CC)

Examples

Find Connected Components in Binary Image

using GMT

# Create a binary image
BW = [1 1 0 0 0 0 0 0
      1 1 0 1 1 0 0 0
      0 0 0 1 1 0 0 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 0 0 0]

# Find connected components
CC = bwconncomp(BW)

println("Number of connected components: ", CC.num_objects)

Specify Connectivity

Find connected components using 4-connectivity instead of the default 8-connectivity:

using GMT

BW = [1 1 0 0 0 0 0 0
      1 1 0 1 1 0 0 0
      0 0 0 1 1 0 0 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 0 0 0]

# 4-connectivity
CC4 = bwconncomp(BW, conn=4)
println("Number of components (4-conn): ", CC4.num_objects)

# 8-connectivity
CC8 = bwconncomp(BW, conn=8)
println("Number of components (8-conn): ", CC8.num_objects)

Extract Properties of Connected Components

Use the output structure to analyze individual components:

using GMT

BW = [1 1 0 0 0 0 0 0
      1 1 0 1 1 0 0 0
      0 0 0 1 1 0 0 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 1 1 0
      0 0 0 0 0 0 0 0]

CC = bwconncomp(BW)

# Number of pixels in each component
numPixels = [length(CC.pixel_list[i]) for i in 1:CC.num_objects]
println("Pixels per component: ", numPixels)

# Find the largest component
largest = argmax(numPixels)
println("Largest component is #", largest, " with ", numPixels[largest], " pixels")

See Also

cc2bw