cc2bw

Convert connected components to binary image


Description

BW = cc2bw(CC)

Converts a connected components structure CC returned by bwconncomp into a binary image BW.

BW = cc2bw(CC, obj2keep=indices)

Creates a binary image containing only the connected components specified by indices.


Input Arguments

CC — Connected components (objects)

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

obj2keep — Objects to keep

Objects to keep, specified as one of these values:

  • Positive integer or vector of positive integers — Keep the object or objects whose index is included in obj2keep. The length of obj2keep is less than or equal to CC.num_objects.

  • Logical vector — Keep the objects whose corresponding element in obj2keep is true. The length of obj2keep must be equal to CC.num_objects.


Output Arguments

BW — Binary image

A new GMTimage, returned as a logical array of the same size as CC.image_size.


Examples

Keep Largest Connected Components

Read a binary image and detect the connected components:

BW = gmtread(TESTSDIR * "assets/text.png");
CC = bwconncomp(BW);

Create a binary image that contains only the 2nd through 10th largest connected components:

using GMT

# Sort by area in descending order
idx = sortperm(CC.area, rev=true);
Ifilt = cc2bw(CC, obj2keep=idx[2:10])
grdimage(I, figsize=6)
grdimage!(Ifilt, figsize=6, xshift=6.2, show=true)

Select Connected Components Based on Multiple Criteria

Read a grayscale image of grains of rice, then convert the image to binary:

I = gmtread(TESTSDIR * "assets/rice.png");
BW = binarize(I);
imshow(BW)

Select regions for whom these conditions apply:

  • The area is greater than 50 pixels

  • The bounding box is less than 15 pixels wide and is greater than or equal to 20 pixels tall

This example uses the filter function to get the indices of the connected components that satisfy these criteria. The ind2bool function is used to convert the vector of indices returned by filter into into a vector of Boolean that can be combined with the area andition and te result passed to cc2bw.

This example also shows a problem with the bwconncomp function when the BoundingBoxes of components overlap. In such cases it may happen that parts of one component are included in the another component. This can lead to wrong results in some areas (as can be seen with the help of the red rectangles drawn around each component).

CC = bwconncomp(BW);
selection = (CC.area .> 50) .&& ind2bool(filter(CC.bbox, true, _aspect=15/20), CC.num_objects);
BW2 = cc2bw(CC, obj2keep=selection)

# Display the original and filtered images
grdimage(BW, figsize=6, plot=(data=CC.bbox, lc=:red))
grdimage!(BW2, figsize=6, xshift=6.2, show=true)

// Image matching '/assets/documentation/utilities/cc2bw/code/example_13604133945599631932.png' not found. //


See Also

bwconncomp, binarize