Implementation:Kornia Kornia CLAHE
| Knowledge Sources | |
|---|---|
| Domains | Vision, Image_Enhancement |
| Last Updated | 2026-02-09 15:00 GMT |
Overview
This module implements Contrast Limited Adaptive Histogram Equalization (CLAHE) for PyTorch tensors, providing both standard and differentiable histogram equalization with bilinear interpolation across tiles.
Description
equalization.py is part of the kornia.enhance module in the Kornia computer vision library. It exposes the equalize_clahe function which performs CLAHE equalization on batches of images. The implementation follows the OpenCV approach: the image is divided into a grid of tiles, per-tile histograms are computed, contrast is limited by clipping and redistributing histogram counts, cumulative distribution functions (LUTs) are built, and the final equalized image is reconstructed via bilinear interpolation across tile boundaries. Key internal helpers include:
- _compute_tiles -- divides the image into a grid of tiles with optional padding
- _compute_interpolation_tiles -- creates half-size tiles for bilinear interpolation
- _compute_luts -- computes the look-up tables from clipped histograms
- _map_luts -- assigns the correct LUTs to each interpolation tile (corners, borders, interior)
- _compute_equalized_tiles -- applies LUTs with bilinear interpolation weights
The module supports a slow_and_differentiable flag that uses kernel density estimation (from the histogram module) instead of hard binning, enabling gradient flow through the equalization pipeline.
Usage
Users should import equalize_clahe when they need adaptive histogram equalization for improving local contrast in images, particularly useful for medical imaging, satellite imagery, or low-contrast photographs processed in differentiable pipelines.
Code Reference
Source Location
- Repository: Kornia
- File: kornia/enhance/equalization.py
- Lines: 1-404
Signature
def equalize_clahe(
input: torch.Tensor,
clip_limit: float = 40.0,
grid_size: Tuple[int, int] = (8, 8),
slow_and_differentiable: bool = False,
) -> torch.Tensor
# Internal helpers
def _compute_tiles(
imgs: torch.Tensor, grid_size: Tuple[int, int], even_tile_size: bool = False
) -> Tuple[torch.Tensor, torch.Tensor]
def _compute_interpolation_tiles(
padded_imgs: torch.Tensor, tile_size: Tuple[int, int]
) -> torch.Tensor
def _compute_luts(
tiles_x_im: torch.Tensor, num_bins: int = 256, clip: float = 40.0, diff: bool = False
) -> torch.Tensor
def _map_luts(interp_tiles: torch.Tensor, luts: torch.Tensor) -> torch.Tensor
def _compute_equalized_tiles(interp_tiles: torch.Tensor, luts: torch.Tensor) -> torch.Tensor
Import
from kornia.enhance import equalize_clahe
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| input | torch.Tensor | Yes | Images tensor with values in [0, 1] and shape (*, C, H, W) |
| clip_limit | float | No | Threshold for contrast limiting. 0 disables clipping. Default: 40.0 |
| grid_size | Tuple[int, int] | No | Number of tiles in each direction (GH, GW). Default: (8, 8) |
| slow_and_differentiable | bool | No | If True, uses differentiable histogram computation (KDE). Default: False |
Outputs
| Name | Type | Description |
|---|---|---|
| output | torch.Tensor | Equalized image with the same shape as the input |
Usage Examples
import torch
from kornia.enhance import equalize_clahe
# Single-channel image
img = torch.rand(1, 10, 20)
result = equalize_clahe(img)
# result.shape == torch.Size([1, 10, 20])
# Batch of RGB images
imgs = torch.rand(2, 3, 10, 20)
result = equalize_clahe(imgs, clip_limit=40.0, grid_size=(8, 8))
# result.shape == torch.Size([2, 3, 10, 20])
# Differentiable version for training pipelines
imgs = torch.rand(2, 3, 64, 64, requires_grad=True)
result = equalize_clahe(imgs, slow_and_differentiable=True)
result.sum().backward() # Gradients flow through