Implementation:Kornia Kornia Integral Image
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Enhancement |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module provides functions and nn.Module classes for computing integral images (summed-area tables) over arbitrary dimensions of a PyTorch tensor.
Description
integral.py is part of the kornia.enhance module in the Kornia computer vision library. It implements integral image (summed-area table) computation, a classic technique that allows O(1) computation of the sum of values in any rectangular subregion of an image. The module contains:
- integral_tensor -- computes the cumulative sum over specified dimensions of an arbitrary tensor. Dimensions must be specified as a tuple of unique, sorted integers. Each specified dimension is independently summed via torch.cumsum.
- integral_image -- a convenience function that computes the integral image by summing over the last two dimensions (-2, -1), which represent height and width.
- IntegralTensor -- an nn.Module wrapper for integral_tensor that stores the target dimensions.
- IntegralImage -- an nn.Module wrapper for integral_image that sums over the spatial dimensions.
The integral image at position (x, y) contains the sum of all pixels above and to the left of (x, y), inclusive. This enables fast computation of box filter responses, mean values over rectangular regions, and is a core building block for algorithms like Haar feature computation and SURF.
Usage
Users should import from this module when they need fast summed-area tables for box filtering, feature computation (e.g., Haar-like features), or any application requiring rapid summation over rectangular image regions.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/enhance/integral.py
- Lines: 1-152
Signature
def integral_tensor(
input: torch.Tensor,
dim: Optional[Tuple[int, ...]] = None,
) -> torch.Tensor
def integral_image(image: torch.Tensor) -> torch.Tensor
class IntegralTensor(nn.Module):
def __init__(self, dim: Optional[Tuple[int, ...]] = None) -> None
def forward(self, input: torch.Tensor) -> torch.Tensor
class IntegralImage(nn.Module):
def forward(self, input: torch.Tensor) -> torch.Tensor
Import
from kornia.enhance import integral_image, integral_tensor
from kornia.enhance.integral import IntegralImage, IntegralTensor
I/O Contract
Inputs (integral_tensor)
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor | Yes | Input tensor with shape (*, D) where D is arbitrary dimensionality |
| dim | Optional[Tuple[int, ...]] | No | Dimensions to sum over. Must be unique, sorted in ascending order. Default: (-1,) |
Inputs (integral_image)
| Name | Type | Required | Description |
|---|---|---|---|
| image | torch.Tensor | Yes | Input image tensor with shape (*, H, W) |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Integral tensor/image with the same shape as input |
Usage Examples
import torch
from kornia.enhance import integral_image, integral_tensor
from kornia.enhance.integral import IntegralImage, IntegralTensor
# Compute integral image for a batch
input = torch.ones(1, 5, 5)
output = integral_image(input)
# output[0, 2, 3] == 12.0 (sum of 3x4 = 12 ones)
# Compute integral tensor over specific dimensions
input = torch.ones(3, 5)
output = integral_tensor(input, (-2, -1))
# output == tensor([[ 1., 2., 3., 4., 5.],
# [ 2., 4., 6., 8., 10.],
# [ 3., 6., 9., 12., 15.]])
# Using IntegralImage module
integral_mod = IntegralImage()
input = torch.ones(2, 3, 10, 10)
output = integral_mod(input)
# output.shape == torch.Size([2, 3, 10, 10])
# Using IntegralTensor module with custom dimensions
integral_mod = IntegralTensor(dim=(-2, -1))
output = integral_mod(torch.ones(3, 5))