{"record":{"id":"01cf494c13ac8d23","repo":"roboflow/supervision","slug":"bgr2gray-conversion-requires-a-three-channel-image","errorCode":null,"errorMessage":"BGR2GRAY conversion requires a three-channel image","messagePattern":"BGR2GRAY conversion requires a three-channel image","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/_cv2/_color.py","lineNumber":35,"sourceCode":"    _COLOR_RGB2BGR,\n)\n\n\ndef _cvt_color(image: npt.NDArray[Any], code: int) -> npt.NDArray[Any]:\n    \"\"\"Convert the BGR, RGB, grayscale, and 8-bit HSV formats used by Supervision.\"\"\"\n    if code in (_COLOR_BGR2RGB, _COLOR_RGB2BGR):\n        if image.ndim != 3 or image.shape[2] != 3:\n            raise ValueError(\"BGR/RGB conversion requires a three-channel image\")\n        return np.ascontiguousarray(image[..., ::-1])\n\n    if code == _COLOR_GRAY2BGR:\n        if image.ndim != 2:\n            raise ValueError(\"GRAY2BGR conversion requires a two-dimensional image\")\n        return np.repeat(image[..., np.newaxis], 3, axis=2)\n\n    if code == _COLOR_BGR2GRAY:\n        if image.ndim != 3 or image.shape[2] != 3:\n            raise ValueError(\"BGR2GRAY conversion requires a three-channel image\")\n        if image.dtype == np.uint8:\n            values = image.astype(np.uint32)\n            weighted = (\n                values[..., 0] * 3735\n                + values[..., 1] * 19235\n                + values[..., 2] * 9798\n                + (1 << 14)\n            ) >> 15\n            return weighted.astype(np.uint8)\n        float_values = (\n            image[..., 0].astype(np.float64) * 0.114\n            + image[..., 1].astype(np.float64) * 0.587\n            + image[..., 2].astype(np.float64) * 0.299\n        )\n        return _cast_array_like_opencv(float_values, image.dtype)\n\n    if code == _COLOR_HSV2BGR:\n        if image.ndim != 3 or image.shape[2] != 3:","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/_cv2/_color.py#L17-L53","documentation":"BGR2GRAY computes a weighted sum of exactly three channels (BT.601 weights 0.114/0.587/0.299, with a fixed-point fast path for uint8). The fallback at src/supervision/_cv2/_color.py:35 requires a 3-channel 3-D image; RGBA (4-channel), grayscale (2-D), or batched input raises.","triggerScenarios":"`cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)` on an RGBA frame, a mask already grayscale, or a (N, H, W, 3) batched array.","commonSituations":"Images loaded with IMREAD_UNCHANGED keeping alpha; video sources yielding BGRA; downstream code assuming 3-channel frames receiving 4.","solutions":["Drop the alpha channel first: `frame = frame[..., :3]`","Skip the conversion if input is already 2-D grayscale","Index batched arrays per-image: `frames[i]`"],"exampleFix":"// before\ngray = cv2.cvtColor(bgra_frame, cv2.COLOR_BGR2GRAY)\n\n// after\ngray = cv2.cvtColor(bgra_frame[..., :3], cv2.COLOR_BGR2GRAY)","handlingStrategy":"type-guard","validationCode":"import numpy as np\n\ndef as_bgr_three_channel(image):\n    \"\"\"Return a (H, W, 3) BGR array for BGR2GRAY.\"\"\"\n    arr = np.asarray(image)\n    if arr.ndim == 2:\n        return arr  # already grayscale; caller can skip the conversion\n    if arr.ndim == 3 and arr.shape[2] > 3:\n        arr = arr[..., :3]\n    if arr.ndim != 3 or arr.shape[2] != 3:\n        raise ValueError(f\"expected (H, W, 3), got {arr.shape}\")\n    return arr","typeGuard":"def is_bgr_frame(image) -> bool:\n    \"\"\"BGR2GRAY requires exactly (H, W, 3).\"\"\"\n    arr = np.asarray(image)\n    return arr.ndim == 3 and arr.shape[2] == 3","tryCatchPattern":null,"preventionTips":["Drop alpha channels immediately after loading BGRA/RGBA sources","Skip BGR2GRAY when input is already 2-D"],"tags":["cv2-fallback","color-conversion","grayscale","channels"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}