{"record":{"id":"8642742d250d56c2","repo":"roboflow/supervision","slug":"addweighted-inputs-must-have-equal-shapes","errorCode":null,"errorMessage":"addWeighted inputs must have equal shapes","messagePattern":"addWeighted inputs must have equal shapes","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/supervision/_cv2/_image.py","lineNumber":85,"sourceCode":"\n\ndef _add_weighted(\n    src1: npt.NDArray[Any],\n    alpha: float,\n    src2: npt.NDArray[Any],\n    beta: float,\n    gamma: float,\n    dst: npt.NDArray[Any] | None = None,\n    dtype: int | None = None,\n) -> npt.NDArray[Any]:\n    \"\"\"Blend two arrays with OpenCV-compatible saturation and optional mutation.\"\"\"\n    if dtype is not None and dtype != -1:\n        raise ValueError(\n            \"addWeighted fallback only supports the default output depth; \"\n            f\"unsupported dtype: {dtype}\"\n        )\n    if src1.shape != src2.shape:\n        raise ValueError(\"addWeighted inputs must have equal shapes\")\n    result = _cast_array_like_opencv(\n        src1.astype(np.float64) * alpha + src2.astype(np.float64) * beta + gamma,\n        src1.dtype,\n    )\n    if dst is not None:\n        dst[...] = result\n        return dst\n    return result\n\n\ndef _convert_scale_abs(\n    image: npt.NDArray[Any], alpha: float = 1, beta: float = 0\n) -> npt.NDArray[np.uint8]:\n    \"\"\"Scale, offset, take the absolute value, and saturate to uint8.\"\"\"\n    values = np.abs(image.astype(np.float64) * alpha + beta)\n    return _cast_array_like_opencv(values, np.dtype(np.uint8))\n\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/roboflow/supervision/blob/7f254d9784d4c37e0f03cd89ddee164c8db099c0/src/supervision/_cv2/_image.py#L67-L103","documentation":"Thrown by Supervision's OpenCV-free fallback for cv2.addWeighted, which blends two images with alpha/beta/gamma. The implementation performs element-wise arithmetic on src1 and src2, so it requires both arrays to have identical shapes; unlike OpenCV, it does not broadcast. Any shape mismatch (even channel or batch differences) raises this ValueError.","triggerScenarios":"Calling cv2.addWeighted(src1, alpha, src2, beta, gamma) through the fallback with src1.shape != src2.shape, e.g. blending a 1080p frame with a 720p overlay, or a 2D grayscale mask with a 3-channel BGR image.","commonSituations":"Compositing annotated frames of different resolutions (e.g. after a resize of only one input), blending a mask (H,W) with a color image (H,W,3), or mixing images loaded with different imread flags. Surfaces only when opencv-python is absent and Supervision uses its internal NumPy fallback.","solutions":["Make the inputs the same shape before blending: resize or pad the smaller array so src1.shape == src2.shape, including channels.","If blending a grayscale mask with a color image, convert the mask first (cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)) or blend per-channel.","Add an assert or explicit check src1.shape == src2.shape before the call to fail with a clearer message.","Install opencv-python if you rely on looser cv2 behavior in your pipeline."],"exampleFix":"// before\nblended = cv2.addWeighted(frame, 0.7, overlay, 0.3, 0)  # frame=(1080,1920,3), overlay=(720,1280,3)\n\n# after\noverlay = cv2.resize(overlay, (frame.shape[1], frame.shape[0]))\nblended = cv2.addWeighted(frame, 0.7, overlay, 0.3, 0)","handlingStrategy":"validation","validationCode":"assert src1.shape == src2.shape, f'shape mismatch: {src1.shape} vs {src2.shape}'\nblended = cv2.addWeighted(src1, alpha, src2, beta, gamma)","typeGuard":null,"tryCatchPattern":"try:\n    blended = cv2.addWeighted(src1, a, src2, b, g)\nexcept ValueError as e:\n    if 'equal shapes' in str(e):\n        src2 = cv2.resize(src2, (src1.shape[1], src1.shape[0]))\n        blended = cv2.addWeighted(src1, a, src2, b, g)\n    else:\n        raise","preventionTips":["Assert equal shapes before blending","Resize/pad inputs to a common shape at pipeline entry","Keep both inputs 3-channel BGR"],"tags":["opencv-fallback","numpy","shape-mismatch","image-blending"],"backgroundTag":null,"analyzedSha":"7f254d9784d4c37e0f03cd89ddee164c8db099c0","analyzedAt":"2026-08-15T05:13:01.950Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}