π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(color): reject unsupported CFA in rgb_to_raw (#5340)
NiteeshGowda72c996899a6f01
diff --git a/changelog.d/5340.breaking.md b/changelog.d/5340.breaking.mdnew file mode 100644index 000000000..a6d42a257--- /dev/null+++ b/changelog.d/5340.breaking.md@@ -0,0 +1,3 @@+`rgb_to_raw` and `RgbToRaw` now raise `ValueError` for a `cfa` that is not a `CFA` member, as+`raw_to_rgb` and `raw_to_rgb_2x2_downscaled` already did. Previously any other value, such as a+string, an integer or `None`, silently returned the green channel as the mosaic.diff --git a/kornia/color/raw.py b/kornia/color/raw.pyindex 6fdfbf2c9..a0413edb1 100644--- a/kornia/color/raw.py+++ b/kornia/color/raw.py@@ -229,6 +229,8 @@ def rgb_to_raw(image: torch.Tensor, cfa: CFA) -> torch.Tensor:elif cfa == CFA.GR:output[..., :, 1::2, ::2] = image[..., 0:1, 1::2, ::2] # redoutput[..., :, ::2, 1::2] = image[..., 2:3, ::2, 1::2] # blue+ else:+ raise ValueError(f"Unsupported CFA value. Got {cfa}")return outputdiff --git a/tests/color/test_raw.py b/tests/color/test_raw.pyindex ca8b9d572..001d52011 100644--- a/tests/color/test_raw.py+++ b/tests/color/test_raw.py@@ -175,6 +175,10 @@ class TestRgbToRaw(BaseTester):with pytest.raises(ValueError):assert kornia.color.rgb_to_raw(img, kornia.color.raw.CFA.BG)+ img = torch.rand(1, 3, 2, 3, device=device, dtype=dtype)+ with pytest.raises(ValueError, match="Unsupported CFA value"):+ kornia.color.rgb_to_raw(img, "bad")+# Reverse test in rawtorgb is sufficient functional testdef test_gradcheck(self, device, dtype):