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 Image Adjust

From Leeroopedia
Revision as of 15:21, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/Kornia_Kornia_Image_Adjust.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

Overview

This module provides a comprehensive collection of differentiable image adjustment functions for brightness, contrast, saturation, hue, gamma, sigmoid, log corrections, solarization, posterization, sharpness, equalization, and inversion in the Kornia library.

Description

adjust.py is part of the kornia.enhance module in the Kornia computer vision library. It contains both functional APIs (pure functions) and their corresponding nn.Module class wrappers for performing pixel-level image adjustments on PyTorch tensors. The functions operate on images in the range [0, 1] and support batched operations with per-element factors. Key operations include:

  • adjust_brightness / adjust_brightness_accumulative -- additive and multiplicative brightness control
  • adjust_contrast / adjust_contrast_with_mean_subtraction -- multiplicative contrast (OpenCV-style) and mean-subtraction contrast (PIL-style)
  • adjust_saturation / adjust_saturation_with_gray_subtraction -- HSV-based and grayscale-blending saturation control
  • adjust_hue -- hue shifting in HSV space
  • adjust_gamma -- gamma correction with gain
  • adjust_sigmoid / adjust_log -- sigmoid and logarithmic intensity corrections
  • solarize / posterize -- solarization and bit-depth reduction
  • sharpness -- PIL-compatible sharpness enhancement via convolution blending
  • equalize / equalize3d -- histogram equalization for 2D and 3D volumes
  • invert -- pixel value inversion

Usage

Users should import from this module when they need differentiable, GPU-accelerated image enhancement operations that integrate with PyTorch autograd. It is suitable for data augmentation pipelines, image preprocessing, and learnable enhancement networks.

Code Reference

Source Location

Signature

# Functional APIs
def adjust_saturation_raw(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_saturation_with_gray_subtraction(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_saturation(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_hue_raw(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_hue(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_gamma(input: torch.Tensor, gamma: Union[float, torch.Tensor], gain: Union[float, torch.Tensor] = 1.0) -> torch.Tensor
def adjust_contrast(image: torch.Tensor, factor: Union[float, torch.Tensor], clip_output: bool = True) -> torch.Tensor
def adjust_contrast_with_mean_subtraction(image: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def adjust_brightness(image: torch.Tensor, factor: Union[float, torch.Tensor], clip_output: bool = True) -> torch.Tensor
def adjust_brightness_accumulative(image: torch.Tensor, factor: Union[float, torch.Tensor], clip_output: bool = True) -> torch.Tensor
def adjust_sigmoid(image: torch.Tensor, cutoff: float = 0.5, gain: float = 10, inv: bool = False) -> torch.Tensor
def adjust_log(image: torch.Tensor, gain: float = 1, inv: bool = False, clip_output: bool = True) -> torch.Tensor
def solarize(input: torch.Tensor, thresholds: Union[float, torch.Tensor] = 0.5, additions: Optional[Union[float, torch.Tensor]] = None) -> torch.Tensor
def posterize(input: torch.Tensor, bits: Union[int, torch.Tensor]) -> torch.Tensor
def sharpness(input: torch.Tensor, factor: Union[float, torch.Tensor]) -> torch.Tensor
def equalize(input: torch.Tensor) -> torch.Tensor
def equalize3d(input: torch.Tensor) -> torch.Tensor
def invert(image: torch.Tensor, max_val: Optional[torch.Tensor] = None) -> torch.Tensor

# nn.Module class wrappers
class AdjustSaturation(nn.Module)
class AdjustSaturationWithGraySubtraction(nn.Module)
class AdjustHue(nn.Module)
class AdjustGamma(nn.Module)
class AdjustContrast(nn.Module)
class AdjustContrastWithMeanSubtraction(nn.Module)
class AdjustBrightness(nn.Module)
class AdjustBrightnessAccumulative(nn.Module)
class AdjustSigmoid(nn.Module)
class AdjustLog(nn.Module)
class Invert(nn.Module)

Import

from kornia.enhance import adjust_brightness, adjust_contrast, adjust_saturation, adjust_hue
from kornia.enhance import adjust_gamma, adjust_sigmoid, adjust_log
from kornia.enhance import solarize, posterize, sharpness, equalize, invert
from kornia.enhance import AdjustBrightness, AdjustContrast, AdjustSaturation, AdjustHue

I/O Contract

Inputs (common pattern)

Name Type Required Description
image / input torch.Tensor Yes Input image tensor with shape (*, C, H, W) in range [0, 1]
factor Union[float, torch.Tensor] Yes (varies) Adjustment factor; can be scalar or per-batch 1-d tensor
clip_output bool No Whether to clamp the result to [0, 1]. Default: True
gamma Union[float, torch.Tensor] Yes (adjust_gamma) Non-negative gamma exponent for gamma correction
gain Union[float, torch.Tensor] No Multiplicative gain for gamma/log corrections. Default: 1.0
cutoff float No Cutoff for sigmoid correction. Default: 0.5
inv bool No Whether to apply inverse correction (sigmoid/log). Default: False
thresholds Union[float, torch.Tensor] No Solarize threshold. Default: 0.5
additions Optional[Union[float, torch.Tensor]] No Solarize addition in [-0.5, 0.5]. Default: None
bits Union[int, torch.Tensor] Yes (posterize) Number of high bits to keep, range [0, 8]
max_val Optional[torch.Tensor] No Maximum value for inversion. Default: 1.0

Outputs

Name Type Description
output torch.Tensor Adjusted image tensor with the same shape as input

Usage Examples

import torch
from kornia.enhance import (
    adjust_brightness,
    adjust_contrast,
    adjust_saturation,
    adjust_gamma,
    solarize,
    posterize,
    sharpness,
    equalize,
    invert,
)

# Create a sample batch of RGB images (B=2, C=3, H=64, W=64)
images = torch.rand(2, 3, 64, 64)

# Adjust brightness by adding 0.2 to all pixels
bright = adjust_brightness(images, factor=0.2)

# Adjust contrast with a per-batch factor
contrast_factors = torch.tensor([0.5, 1.5])
contrasted = adjust_contrast(images, factor=contrast_factors)

# Adjust saturation (expects RGB input)
saturated = adjust_saturation(images, factor=1.5)

# Gamma correction
gamma_corrected = adjust_gamma(images, gamma=0.5, gain=1.0)

# Solarize pixels above threshold 0.5
solarized = solarize(images, thresholds=0.5, additions=0.0)

# Reduce color depth to 4 bits
posterized = posterize(images, bits=4)

# Apply sharpness enhancement
sharpened = sharpness(images, factor=2.0)

# Histogram equalization
equalized = equalize(images)

# Invert the image
inverted = invert(images)

Related Pages

Page Connections

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