roboflow/supervision · error · ValueError

addWeighted inputs must have equal shapes

Error message

addWeighted inputs must have equal shapes

What it means

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.

Source

Thrown at src/supervision/_cv2/_image.py:85


def _add_weighted(
    src1: npt.NDArray[Any],
    alpha: float,
    src2: npt.NDArray[Any],
    beta: float,
    gamma: float,
    dst: npt.NDArray[Any] | None = None,
    dtype: int | None = None,
) -> npt.NDArray[Any]:
    """Blend two arrays with OpenCV-compatible saturation and optional mutation."""
    if dtype is not None and dtype != -1:
        raise ValueError(
            "addWeighted fallback only supports the default output depth; "
            f"unsupported dtype: {dtype}"
        )
    if src1.shape != src2.shape:
        raise ValueError("addWeighted inputs must have equal shapes")
    result = _cast_array_like_opencv(
        src1.astype(np.float64) * alpha + src2.astype(np.float64) * beta + gamma,
        src1.dtype,
    )
    if dst is not None:
        dst[...] = result
        return dst
    return result


def _convert_scale_abs(
    image: npt.NDArray[Any], alpha: float = 1, beta: float = 0
) -> npt.NDArray[np.uint8]:
    """Scale, offset, take the absolute value, and saturate to uint8."""
    values = np.abs(image.astype(np.float64) * alpha + beta)
    return _cast_array_like_opencv(values, np.dtype(np.uint8))

View on GitHub (pinned to 7f254d9784)

Solutions

  1. Make the inputs the same shape before blending: resize or pad the smaller array so src1.shape == src2.shape, including channels.
  2. If blending a grayscale mask with a color image, convert the mask first (cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)) or blend per-channel.
  3. Add an assert or explicit check src1.shape == src2.shape before the call to fail with a clearer message.
  4. Install opencv-python if you rely on looser cv2 behavior in your pipeline.

Example fix

// before
blended = cv2.addWeighted(frame, 0.7, overlay, 0.3, 0)  # frame=(1080,1920,3), overlay=(720,1280,3)

# after
overlay = cv2.resize(overlay, (frame.shape[1], frame.shape[0]))
blended = cv2.addWeighted(frame, 0.7, overlay, 0.3, 0)
Defensive patterns

Strategy: validation

Validate before calling

assert src1.shape == src2.shape, f'shape mismatch: {src1.shape} vs {src2.shape}'
blended = cv2.addWeighted(src1, alpha, src2, beta, gamma)

Try / catch

try:
    blended = cv2.addWeighted(src1, a, src2, b, g)
except ValueError as e:
    if 'equal shapes' in str(e):
        src2 = cv2.resize(src2, (src1.shape[1], src1.shape[0]))
        blended = cv2.addWeighted(src1, a, src2, b, g)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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