roboflow/supervision · error · ValueError

Incorrect connectivity value. Possible connectivity values:

Error message

Incorrect connectivity value. Possible connectivity values: 4 or 8.

What it means

contains_multiple_segments delegates to cv2.connectedComponents, which only supports 4-connected or 8-connected neighborhoods; any other connectivity integer is rejected before the OpenCV call. 4-connectivity counts only edge-adjacent pixels as one segment; 8-connectivity also counts diagonal neighbors.

Source

Thrown at src/supervision/detection/utils/masks.py:283

        >>> sv.contains_multiple_segments(mask=mask, connectivity=4)
        True
        >>> mask = np.array([
        ...     [0, 0, 0, 0, 0, 0],
        ...     [0, 1, 1, 1, 1, 1],
        ...     [0, 1, 1, 1, 1, 1],
        ...     [0, 1, 1, 1, 1, 1],
        ...     [0, 1, 1, 1, 1, 1],
        ...     [0, 0, 0, 0, 0, 0]
        ... ]).astype(bool)
        >>> sv.contains_multiple_segments(mask=mask, connectivity=4)
        False

        ```

    ![contains_multiple_segments](https://media.roboflow.com/supervision-docs/contains-multiple-segments.png){ align=center width="800" }
    """  # noqa E501 // docs
    if connectivity != 4 and connectivity != 8:
        raise ValueError(
            "Incorrect connectivity value. Possible connectivity values: 4 or 8."
        )
    mask_uint8 = mask.astype(np.uint8)
    labels = np.zeros_like(mask_uint8, dtype=np.int32)
    number_of_labels, _ = cv2.connectedComponents(
        mask_uint8, labels, connectivity=connectivity
    )
    return bool(number_of_labels > 2)


def resize_masks(
    masks: npt.NDArray[np.bool_], max_dimension: int = 640
) -> npt.NDArray[np.bool_]:
    """
    Resize all masks in the array to have a maximum dimension of max_dimension,
    maintaining aspect ratio.

    Args:

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Use 4 or 8 as an int; choose 4 for strict edge-adjacency, 8 to merge diagonal neighbors.
  2. Coerce config/CLI inputs with int() before passing.
  3. Validate at the config boundary: if connectivity not in (4, 8): raise early with your own message.

Example fix

# before
 sv.contains_multiple_segments(mask=mask, connectivity=int(cfg["connectivity"]) if cfg else 6)

# after
 connectivity = int(cfg["connectivity"]) if cfg else 4
 assert connectivity in (4, 8)
 sv.contains_multiple_segments(mask=mask, connectivity=connectivity)
Defensive patterns

Strategy: validation

Validate before calling

connectivity = int(cfg["connectivity"])
if connectivity not in (4, 8):
    raise ValueError(f"connectivity must be 4 or 8, got {connectivity}")
sv.contains_multiple_segments(mask=mask, connectivity=connectivity)

Type guard

def is_valid_connectivity(value: int) -> bool:
    return value in (4, 8)

Prevention

When it happens

Trigger: Calling sv.contains_multiple_segments(mask, connectivity=6) or connectivity=2, or passing a value read from a config/CLI as a string ('4') so the != comparisons always hold.

Common situations: Config value parsed as string instead of int; copying a connectivity number from skimage (which uses 1/2) rather than OpenCV semantics; typo or auto-complete picking an invalid value.

Related errors


AI-assisted analysis of roboflow/supervision@7f254d9784 (2026-08-15). Data as JSON: /api/errors/ab0373d3c3553b6a. Report an issue: GitHub.