{"record":{"id":"ab0373d3c3553b6a","repo":"roboflow/supervision","slug":"incorrect-connectivity-value-possible-connectivit","errorCode":null,"errorMessage":"Incorrect connectivity value. Possible connectivity values: 4 or 8.","messagePattern":"Incorrect connectivity value\\. Possible connectivity values: 4 or 8\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/detection/utils/masks.py","lineNumber":283,"sourceCode":"        >>> sv.contains_multiple_segments(mask=mask, connectivity=4)\n        True\n        >>> mask = np.array([\n        ...     [0, 0, 0, 0, 0, 0],\n        ...     [0, 1, 1, 1, 1, 1],\n        ...     [0, 1, 1, 1, 1, 1],\n        ...     [0, 1, 1, 1, 1, 1],\n        ...     [0, 1, 1, 1, 1, 1],\n        ...     [0, 0, 0, 0, 0, 0]\n        ... ]).astype(bool)\n        >>> sv.contains_multiple_segments(mask=mask, connectivity=4)\n        False\n\n        ```\n\n    ![contains_multiple_segments](https://media.roboflow.com/supervision-docs/contains-multiple-segments.png){ align=center width=\"800\" }\n    \"\"\"  # noqa E501 // docs\n    if connectivity != 4 and connectivity != 8:\n        raise ValueError(\n            \"Incorrect connectivity value. Possible connectivity values: 4 or 8.\"\n        )\n    mask_uint8 = mask.astype(np.uint8)\n    labels = np.zeros_like(mask_uint8, dtype=np.int32)\n    number_of_labels, _ = cv2.connectedComponents(\n        mask_uint8, labels, connectivity=connectivity\n    )\n    return bool(number_of_labels > 2)\n\n\ndef resize_masks(\n    masks: npt.NDArray[np.bool_], max_dimension: int = 640\n) -> npt.NDArray[np.bool_]:\n    \"\"\"\n    Resize all masks in the array to have a maximum dimension of max_dimension,\n    maintaining aspect ratio.\n\n    Args:","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/detection/utils/masks.py#L265-L301","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use 4 or 8 as an int; choose 4 for strict edge-adjacency, 8 to merge diagonal neighbors.","Coerce config/CLI inputs with int() before passing.","Validate at the config boundary: if connectivity not in (4, 8): raise early with your own message."],"exampleFix":"# before\n sv.contains_multiple_segments(mask=mask, connectivity=int(cfg[\"connectivity\"]) if cfg else 6)\n\n# after\n connectivity = int(cfg[\"connectivity\"]) if cfg else 4\n assert connectivity in (4, 8)\n sv.contains_multiple_segments(mask=mask, connectivity=connectivity)","handlingStrategy":"validation","validationCode":"connectivity = int(cfg[\"connectivity\"])\nif connectivity not in (4, 8):\n    raise ValueError(f\"connectivity must be 4 or 8, got {connectivity}\")\nsv.contains_multiple_segments(mask=mask, connectivity=connectivity)","typeGuard":"def is_valid_connectivity(value: int) -> bool:\n    return value in (4, 8)","tryCatchPattern":null,"preventionTips":["Coerce config/CLI values to int.","Remember OpenCV semantics: 4 or 8, not skimage's 1/2.","Pick 8 if diagonal bridges should keep a segment connected."],"tags":["masks","connectivity","opencv","validation"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}