Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Create functional and class Adjust Brightness

Pau Ribad267c2db8a4d
diff --git a/kornia/__init__.py b/kornia/__init__.py
index d3c1bdf84..f605ab666 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -23,6 +23,7 @@ from kornia.color import (
rgb_to_hsv,
hsv_to_rgb,
normalize,
+ adjust_brightness,
)
from kornia.contrib import (
spatial_soft_argmax2d,
diff --git a/kornia/color/__init__.py b/kornia/color/__init__.py
index 4821361f0..3e9248bca 100644
--- a/kornia/color/__init__.py
+++ b/kornia/color/__init__.py
@@ -4,6 +4,7 @@ from .rgb import RgbToBgr, rgb_to_bgr
from .hsv import RgbToHsv, rgb_to_hsv
from .hsv import HsvToRgb, hsv_to_rgb
from .normalize import Normalize, normalize
+from .adjust import AdjustBrightness, adjust_brightness
__all__ = [
"rgb_to_grayscale",
@@ -11,12 +12,14 @@ __all__ = [
"rgb_to_bgr",
"rgb_to_hsv",
"hsv_to_rgb",
- "normalize"
+ "normalize",
+ "adjust_brightness",
"RgbToGrayscale",
"BgrToRgb",
"RgbToBgr",
"RgbToHsv",
"HsvToRgb",
"Normalize",
+ "AdjustBrightness",
]
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.py
new file mode 100644
index 000000000..28c4bd1fc
--- /dev/null
+++ b/kornia/color/adjust.py
@@ -0,0 +1,64 @@
+import torch
+import torch.nn as nn
+
+
+class AdjustBrightness(nn.Module):
+ r"""Adjust Brightness of an Image
+
+ See :class:`~kornia.color.AdjustBrightness` for details.
+
+ Args:
+ image (torch.Tensor): Image to be adjusted.
+ brightness_factor (torch.Tensor): Brightness adjust factor. 0 generates
+ a compleatly black image, 1 does not modify the input image while any
+ other non-negative number modify the brightness by this factor.
+
+ Returns:
+ torch.Tensor: Adjusted image.
+ """
+
+ def __init__(self) -> None:
+ super(AdjustBrightness, self).__init__()
+
+ def forward(self, # type: ignore
+ image: torch.Tensor, # type: ignore
+ brightness_factor: torch.Tensor # type: ignore
+ ) -> torch.Tensor: # type: ignore
+ return adjust_brightness(image, brightness_factor)
+
+
+def adjust_brightness(image: torch.Tensor,
+ brightness_factor: torch.Tensor) -> torch.Tensor:
+ r"""Adjust Brightness of an Image
+
+ See :class:`~kornia.color.AdjustBrightness` for details.
+
+ Args:
+ image (torch.Tensor): Image to be adjusted.
+ brightness_factor (torch.Tensor): Brightness adjust factor. 0 generates
+ a compleatly black image, 1 does not modify the input image while any
+ other non-negative number modify the brightness by this factor.
+
+ Returns:
+ torch.Tensor: Adjusted image.
+ """
+
+ if not torch.is_tensor(image):
+ raise TypeError("Input type is not a torch.Tensor. Got {}".format(
+ type(image)))
+
+ if len(image.shape) < 3:
+ raise ValueError("Input size must have a shape of (*, H, W). Got {}"
+ .format(image.shape))
+
+ if brightness_factor < 0.0:
+ raise ValueError("Brightness factor must be non-negative. Got {}"
+ .format(brightness_factor))
+
+ # Apply brightness factor to each channel
+ adjust_image: torch.Tensor = image * brightness_factor
+
+ # Truncate between pixel values
+ out: torch.Tensor = torch.clamp(adjust_image, 0.0, 1.0)
+
+ return out