Contrast-limited adaptive histogram equalization (CLAHE)

I wanna do a Contrast-limited adaptive histogram equalization (CLAHE) as one of my custom transforms, how should i go about adding this in my transforms?

You can define a function that performs the sequence of operations for CLAHE on a single image in NumPy array or Torch tensor format. Then, write a Dataset class, and in your __getitem__ function call that function for CLAHE and pass the image tensor to do the job.

2 Likes

did you find the answer for this question ?
if so can you please share it

Since I needed it too, I wrote mine. Here’s my basic solution (no randomization since I need to apply it every time with the same parameters, but you can add it easily).

import torch
import kornia
from typing import Tuple


class Clahe(torch.nn.Module):

    def __init__(self, clip_limit: int | float = 40, grid_size: Tuple[int, int] = (8, 8)) -> None:
        super().__init__()
        self.clip_limit, self.grid_size = float(clip_limit), grid_size

    def forward(self, img: torch.Tensor) -> torch.Tensor:
        return kornia.enhance.equalize_clahe(img, self.clip_limit, self.grid_size)

    def __repr__(self) -> str:
        return "{}(clip_limit={}, tile_grid_size={})".format(
            self.__class__.__name__,
            self.clip_limit,
            self.grid_size
        )
    

I hope you’ll find it useful :slight_smile: