Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Correct adjust brightness and contrast

Pau Riba431d00424b19
diff --git a/kornia/color/adjust.py b/kornia/color/adjust.py
index 009977d23..bfc140523 100644
--- a/kornia/color/adjust.py
+++ b/kornia/color/adjust.py
@@ -116,8 +116,8 @@ def adjust_contrast(input: torch.Tensor,
for _ in input.shape[1:]:
contrast_factor = torch.unsqueeze(contrast_factor, dim=-1)
- # Apply brightness factor to each channel
- x_adjust: torch.Tensor = input + contrast_factor
+ # Apply contrast factor to each channel
+ x_adjust: torch.Tensor = input * contrast_factor
# Truncate between pixel values
out: torch.Tensor = torch.clamp(x_adjust, 0.0, 1.0)
@@ -144,14 +144,11 @@ def adjust_brightness(input: torch.Tensor,
brightness_factor = brightness_factor.to(input.device).to(input.dtype)
- if (brightness_factor < 0).any():
- raise ValueError(f"Brightness factor must be non-negative. Got {brightness_factor}")
-
for _ in input.shape[1:]:
brightness_factor = torch.unsqueeze(brightness_factor, dim=-1)
# Apply brightness factor to each channel
- x_adjust: torch.Tensor = input * brightness_factor
+ x_adjust: torch.Tensor = input + brightness_factor
# Truncate between pixel values
out: torch.Tensor = torch.clamp(x_adjust, 0.0, 1.0)
@@ -263,9 +260,8 @@ class AdjustBrightness(nn.Module):
Args:
input (torch.Tensor): Image/Input to be adjusted in the shape of (\*, N).
brightness_factor (Union[float, 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.
+ in the batch. 0 does not modify the input image while any other number modify the
+ brightness.
Returns:
torch.Tensor: Adjusted image.
diff --git a/test/color/test_adjust.py b/test/color/test_adjust.py
index 4e40ef591..42118ff6c 100644
--- a/test/color/test_adjust.py
+++ b/test/color/test_adjust.py
@@ -134,9 +134,10 @@ class TestAdjustContrast:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ expected = torch.zeros_like(data)
f = kornia.color.AdjustContrast(0.)
+
assert_allclose(f(data), expected)
def test_factor_one(self):
@@ -150,9 +151,34 @@ class TestAdjustContrast:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = torch.ones_like(data)
+ expected = data
f = kornia.color.AdjustContrast(1.)
+
+ assert_allclose(f(data), expected)
+
+ def test_factor_two(self):
+ # prepare input data
+ data = torch.tensor([[[1., 1.],
+ [1., 1.]],
+
+ [[.5, .5],
+ [.5, .5]],
+
+ [[.25, .25],
+ [.25, .25]]]) # 3x2x2
+
+ expected = torch.tensor([[[1., 1.],
+ [1., 1.]],
+
+ [[1., 1.],
+ [1., 1.]],
+
+ [[.5, .5],
+ [.5, .5]]]) # 3x2x2
+
+ f = kornia.color.AdjustContrast(2.)
+
assert_allclose(f(data), expected)
def test_factor_tensor(self):
@@ -169,9 +195,19 @@ class TestAdjustContrast:
[[.5, .5],
[.5, .5]]]) # 4x2x2
- expected = torch.ones_like(data)
+ expected = torch.tensor([[[0., 0.],
+ [0., 0.]],
- factor = torch.tensor([0, 0.5, 0.75, 2])
+ [[.5, .5],
+ [.5, .5]],
+
+ [[.375, .375],
+ [.375, .375]],
+
+ [[1., 1.],
+ [1., 1.]]]) # 4x2x2
+
+ factor = torch.tensor([0, 1, 1.5, 2])
f = kornia.color.AdjustContrast(factor)
assert_allclose(f(data), expected)
@@ -198,22 +234,64 @@ class TestAdjustContrast:
expected = torch.tensor([[[[1., 1.],
[1., 1.]],
- [[.75, .75],
- [.75, .75]],
-
[[.5, .5],
- [.5, .5]]],
+ [.5, .5]],
- [[[.1, .1],
- [.1, .1]],
+ [[.25, .25],
+ [.25, .25]]],
- [[.4, .4],
- [.4, .4]],
+ [[[0., 0.],
+ [0., 0.]],
- [[.7, .7],
- [.7, .7]]]]) # 2x3x2x2
+ [[.6, .6],
+ [.6, .6]],
- factor = torch.tensor([0.25, 0.1])
+ [[1., 1.],
+ [1., 1.]]]]) # 2x3x2x2
+
+ factor = torch.tensor([1, 2])
+ f = kornia.color.AdjustContrast(factor)
+ assert_allclose(f(data), expected)
+
+ def test_factor_tensor_shape(self):
+ # prepare input data
+ data = torch.tensor([[[[1., 1., .5],
+ [1., 1., .5]],
+
+ [[.5, .5, .25],
+ [.5, .5, .25]],
+
+ [[.25, .25, .25],
+ [.6, .6, .3]]],
+
+ [[[0., 0., 1.],
+ [0., 0., .25]],
+
+ [[.3, .3, .4],
+ [.3, .3, .4]],
+
+ [[.6, .6, 0.],
+ [.3, .2, .1]]]]) # 2x3x2x3
+
+ expected = torch.tensor([[[[1., 1., .75],
+ [1., 1., .75]],
+
+ [[.75, .75, .375],
+ [.75, .75, .375]],
+
+ [[.375, .375, .375],
+ [.9, .9, .45]]],
+
+ [[[0., 0., 1.],
+ [0., 0., .5]],
+
+ [[.6, .6, .8],
+ [.6, .6, .8]],
+
+ [[1., 1., 0.],
+ [.6, .4, .2]]]]) # 2x3x2x3
+
+ factor = torch.tensor([1.5, 2.])
f = kornia.color.AdjustContrast(factor)
assert_allclose(f(data), expected)
@@ -237,17 +315,9 @@ class TestAdjustBrightness:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = torch.tensor([[[0., 0.],
- [0., 0.]],
-
- [[0., 0.],
- [0., 0.]],
-
- [[0., 0.],
- [0., 0.]]]) # 3x2x2
+ expected = data
f = kornia.color.AdjustBrightness(0.)
-
assert_allclose(f(data), expected)
def test_factor_one(self):
@@ -261,34 +331,32 @@ class TestAdjustBrightness:
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = data
+ expected = torch.ones_like(data)
f = kornia.color.AdjustBrightness(1.)
-
assert_allclose(f(data), expected)
- def test_factor_two(self):
+ def test_factor_minus(self):
# prepare input data
data = torch.tensor([[[1., 1.],
[1., 1.]],
- [[.5, .5],
- [.5, .5]],
+ [[.75, .75],
+ [.75, .75]],
[[.25, .25],
[.25, .25]]]) # 3x2x2
- expected = torch.tensor([[[1., 1.],
- [1., 1.]],
-
- [[1., 1.],
- [1., 1.]],
+ expected = torch.tensor([[[.5, .5],
+ [.5, .5]],
- [[.5, .5],
- [.5, .5]]]) # 3x2x2
+ [[.25, .25],
+ [.25, .25]],
- f = kornia.color.AdjustBrightness(2.)
+ [[0., 0.],
+ [0., 0.]]]) # 3x2x2
+ f = kornia.color.AdjustBrightness(-.5)
assert_allclose(f(data), expected)
def test_factor_tensor(self):
@@ -305,19 +373,9 @@ class TestAdjustBrightness:
[[.5, .5],
[.5, .5]]]) # 4x2x2
- expected = torch.tensor([[[0., 0.],
- [0., 0.]],
-
- [[.5, .5],
- [.5, .5]],
-
- [[.375, .375],
- [.375, .375]],
-
- [[1., 1.],
- [1., 1.]]]) # 4x2x2
+ expected = torch.ones_like(data)
- factor = torch.tensor([0, 1, 1.5, 2])
+ factor = torch.tensor([0, 0.5, 0.75, 2])
f = kornia.color.AdjustBrightness(factor)
assert_allclose(f(data), expected)
@@ -344,64 +402,22 @@ class TestAdjustBrightness:
expected = torch.tensor([[[[1., 1.],
[1., 1.]],
- [[.5, .5],
- [.5, .5]],
-
- [[.25, .25],
- [.25, .25]]],
-
- [[[0., 0.],
- [0., 0.]],
-
- [[.6, .6],
- [.6, .6]],
-
- [[1., 1.],
- [1., 1.]]]]) # 2x3x2x2
-
- factor = torch.tensor([1, 2])
- f = kornia.color.AdjustBrightness(factor)
- assert_allclose(f(data), expected)
-
- def test_factor_tensor_shape(self):
- # prepare input data
- data = torch.tensor([[[[1., 1., .5],
- [1., 1., .5]],
-
- [[.5, .5, .25],
- [.5, .5, .25]],
-
- [[.25, .25, .25],
- [.6, .6, .3]]],
-
- [[[0., 0., 1.],
- [0., 0., .25]],
-
- [[.3, .3, .4],
- [.3, .3, .4]],
-
- [[.6, .6, 0.],
- [.3, .2, .1]]]]) # 2x3x2x3
-
- expected = torch.tensor([[[[1., 1., .75],
- [1., 1., .75]],
-
- [[.75, .75, .375],
- [.75, .75, .375]],
+ [[.75, .75],
+ [.75, .75]],
- [[.375, .375, .375],
- [.9, .9, .45]]],
+ [[.5, .5],
+ [.5, .5]]],
- [[[0., 0., 1.],
- [0., 0., .5]],
+ [[[.1, .1],
+ [.1, .1]],
- [[.6, .6, .8],
- [.6, .6, .8]],
+ [[.4, .4],
+ [.4, .4]],
- [[1., 1., 0.],
- [.6, .4, .2]]]]) # 2x3x2x3
+ [[.7, .7],
+ [.7, .7]]]]) # 2x3x2x2
- factor = torch.tensor([1.5, 2.])
+ factor = torch.tensor([0.25, 0.1])
f = kornia.color.AdjustBrightness(factor)
assert_allclose(f(data), expected)