Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Kornia Kornia Add Weighted

From Leeroopedia


Knowledge Sources
Domains Vision, Image_Enhancement
Last Updated 2026-02-09 15:00 GMT

Overview

This module provides a function and nn.Module class for computing the weighted sum of two tensors, analogous to OpenCV's addWeighted operation.

Description

core.py is part of the kornia.enhance module in the Kornia computer vision library. It implements the weighted addition of two tensors following the formula:

out = src1 * alpha + src2 * beta + gamma

The module contains:

  • add_weighted -- a functional API that takes two source tensors of the same shape, their respective weights (alpha, beta), and a scalar offset (gamma). Weights can be floats or tensors with shapes broadcastable to the source tensors.
  • AddWeighted -- an nn.Module wrapper that stores alpha, beta, and gamma as parameters and applies the weighted sum in the forward pass.

Both source tensors must have identical shapes. When alpha, beta, or gamma are provided as tensors, they must have shapes that match the source tensors for element-wise operation.

Usage

Users should import from this module when they need to blend two images or tensors with specified weights, such as for image overlaying, alpha blending, or creating weighted combinations of feature maps.

Code Reference

Source Location

Signature

def add_weighted(
    src1: torch.Tensor,
    alpha: Union[float, torch.Tensor],
    src2: torch.Tensor,
    beta: Union[float, torch.Tensor],
    gamma: Union[float, torch.Tensor],
) -> torch.Tensor

class AddWeighted(nn.Module):
    def __init__(
        self,
        alpha: Union[float, torch.Tensor],
        beta: Union[float, torch.Tensor],
        gamma: Union[float, torch.Tensor],
    ) -> None
    def forward(self, src1: torch.Tensor, src2: torch.Tensor) -> torch.Tensor

Import

from kornia.enhance import add_weighted, AddWeighted

I/O Contract

Inputs

Name Type Required Description
src1 torch.Tensor Yes First source tensor with arbitrary shape
alpha Union[float, torch.Tensor] Yes Weight for the first source tensor
src2 torch.Tensor Yes Second source tensor, must have the same shape as src1
beta Union[float, torch.Tensor] Yes Weight for the second source tensor
gamma Union[float, torch.Tensor] Yes Scalar offset added to the weighted sum

Outputs

Name Type Description
output torch.Tensor Weighted sum tensor with the same shape as src1 and src2

Usage Examples

import torch
from kornia.enhance import add_weighted, AddWeighted

# Functional API: blend two images equally with an offset of 1.0
input1 = torch.rand(1, 1, 5, 5)
input2 = torch.rand(1, 1, 5, 5)
output = add_weighted(input1, 0.5, input2, 0.5, 1.0)
# output.shape == torch.Size([1, 1, 5, 5])

# Module API
blender = AddWeighted(alpha=0.7, beta=0.3, gamma=0.0)
output = blender(input1, input2)
# output.shape == torch.Size([1, 1, 5, 5])

# Using tensor weights for per-element weighting
alpha = torch.rand(1, 1, 5, 5)
beta = torch.rand(1, 1, 5, 5)
gamma = torch.zeros(1, 1, 5, 5)
output = add_weighted(input1, alpha, input2, beta, gamma)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment