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 In Range

From Leeroopedia


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

Overview

Creates a binary mask or filtered image indicating which pixels fall within specified lower and upper bounds across all channels.

Description

This module is part of the Kornia library's filters subpackage. It provides the in_range function and InRange nn.Module class for range-based pixel filtering. The function checks whether each pixel in every channel of the input tensor falls within the inclusive bounds defined by lower and upper. For multi-channel images, a pixel is considered in-range only if all of its channel values satisfy the bounds (logical AND across channels). The bounds can be specified as tuples (uniform across batches) or as torch.Tensors of shape (B, C, 1, 1) for per-batch bounds. The function can return either a binary mask of shape (*, 1, H, W) or the input image masked by the range. The function is decorated with @perform_keep_shape_image for shape compatibility.

Usage

Import this function or class when you need to threshold or mask images based on pixel intensity or color ranges, such as for color segmentation, chroma keying, or selecting regions of interest by intensity.

Code Reference

Source Location

Signature

@perform_keep_shape_image
def in_range(
    input: torch.Tensor,
    lower: Union[tuple[Any, ...], torch.Tensor],
    upper: Union[tuple[Any, ...], torch.Tensor],
    return_mask: bool = False,
) -> torch.Tensor: ...

class InRange(nn.Module):
    def __init__(self, lower: Union[tuple[Any, ...], torch.Tensor],
                 upper: Union[tuple[Any, ...], torch.Tensor],
                 return_mask: bool = False) -> None: ...
    def forward(self, input: torch.Tensor) -> torch.Tensor: ...

Import

from kornia.filters import in_range, InRange

I/O Contract

Inputs

Name Type Required Description
input torch.Tensor (*, C, H, W) Yes The input image tensor.
lower tuple or torch.Tensor (B, C, 1, 1) Yes Inclusive lower bound per channel. Tuple length must match channel count.
upper tuple or torch.Tensor (B, C, 1, 1) Yes Inclusive upper bound per channel. Tuple length must match channel count.
return_mask bool No (default False) If True, returns a binary mask (*, 1, H, W). If False, returns the filtered input image.

Outputs

Name Type Description
output torch.Tensor Either a binary mask of shape (*, 1, H, W) when return_mask=True, or the input image multiplied by the mask (*, C, H, W) when return_mask=False.

Usage Examples

import torch
from kornia.filters import in_range, InRange

# Get a binary mask for pixels within range
rng = torch.manual_seed(1)
input = torch.rand(1, 3, 3, 3)
lower = (0.2, 0.3, 0.4)
upper = (0.8, 0.9, 1.0)
mask = in_range(input, lower, upper, return_mask=True)
print(mask.shape)  # torch.Size([1, 1, 3, 3])

# Per-batch bounds using tensors
input_tensor = torch.rand(2, 3, 3, 3)
lower_t = torch.tensor([[0.2, 0.2, 0.2], [0.3, 0.3, 0.3]]).reshape(2, 3, 1, 1)
upper_t = torch.tensor([[0.6, 0.6, 0.6], [0.8, 0.8, 0.8]]).reshape(2, 3, 1, 1)
mask = in_range(input_tensor, lower_t, upper_t, return_mask=True)

# Module-based usage
filter_mod = InRange((0.2, 0.3, 0.4), (0.8, 0.9, 1.0), return_mask=True)
mask = filter_mod(input)

Related Pages

Page Connections

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