Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Modify AdjustBrightness to accept a factor per element in the batch

Pau Riba36d29435a8ec
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.py
index 28c4bd1fc..64ba83034 100644
--- a/kornia/color/adjust.py
+++ b/kornia/color/adjust.py
@@ -9,9 +9,10 @@ class AdjustBrightness(nn.Module):
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.
+ brightness_factor (torch.Tensor): Brightness adjust factor per element
+ in the batch. 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.
@@ -35,9 +36,10 @@ def adjust_brightness(image: torch.Tensor,
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.
+ brightness_factor (torch.Tensor): Brightness adjust factor per element
+ in the batch. 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.
@@ -51,10 +53,15 @@ def adjust_brightness(image: torch.Tensor,
raise ValueError("Input size must have a shape of (*, H, W). Got {}"
.format(image.shape))
- if brightness_factor < 0.0:
+
+ if (brightness_factor < torch.zeros(1)).any():
raise ValueError("Brightness factor must be non-negative. Got {}"
.format(brightness_factor))
+ if torch.is_tensor(brightness_factor):
+ for _ in image.shape[1:]:
+ brightness_factor = brightness_factor.unsqueeze(-1)
+
# Apply brightness factor to each channel
adjust_image: torch.Tensor = image * brightness_factor