π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
RGB <-> YCbCr (#345)
Duc Nguyencdc9a0bbfe51
diff --git a/kornia/__init__.py b/kornia/__init__.pyindex 096cda606..8474f621b 100644--- a/kornia/__init__.py+++ b/kornia/__init__.py@@ -28,6 +28,8 @@ from kornia.color import (rgb_to_yuv,yuv_to_rgb,hls_to_rgb,+ rgb_to_ycbcr,+ ycbcr_to_rgb,normalize,denormalize,adjust_brightness,diff --git a/kornia/color/__init__.py b/kornia/color/__init__.pyindex ffd232fe8..5b73db664 100644--- a/kornia/color/__init__.py+++ b/kornia/color/__init__.py@@ -6,6 +6,8 @@ from .hsv import RgbToHsv, rgb_to_hsvfrom .hsv import HsvToRgb, hsv_to_rgbfrom .hls import RgbToHls, rgb_to_hlsfrom .hls import HlsToRgb, hls_to_rgb+from .ycbcr import RgbToYcbcr, rgb_to_ycbcr+from .ycbcr import YcbcrToRgb, ycbcr_to_rgbfrom .yuv import RgbToYuv, YuvToRgb, rgb_to_yuv, yuv_to_rgbfrom .normalize import Normalize, normalize, Denormalize, denormalizefrom .core import add_weighted, AddWeighted@@ -25,6 +27,8 @@ __all__ = ["hsv_to_rgb","rgb_to_hls","hls_to_rgb",+ "rgb_to_ycbcr",+ "ycbcr_to_rgb","rgb_to_yuv","yuv_to_rgb","normalize",@@ -44,6 +48,8 @@ __all__ = ["HsvToRgb","RgbToHls","HlsToRgb",+ "RgbToYcbcr",+ "YcbcrToRgb","RgbToYuv","YuvToRgb","Normalize",diff --git a/kornia/color/gray.py b/kornia/color/gray.pyindex b84be69c6..35b655f05 100644--- a/kornia/color/gray.py+++ b/kornia/color/gray.py@@ -23,6 +23,8 @@ class RgbToGrayscale(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> gray = kornia.color.RgbToGrayscale()>>> output = gray(input) # 2x1x4x5@@ -79,6 +81,8 @@ class BgrToGrayscale(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> gray = kornia.color.BgrToGrayscale()>>> output = gray(input) # 2x1x4x5diff --git a/kornia/color/hls.py b/kornia/color/hls.pyindex 51afac908..c9a4a6153 100644--- a/kornia/color/hls.py+++ b/kornia/color/hls.py@@ -23,6 +23,8 @@ class HlsToRgb(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> rgb = kornia.color.HlsToRgb()>>> output = rgb(input) # 2x3x4x5@@ -95,9 +97,11 @@ class RgbToHls(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)- >>> hsv = kornia.color.RgbToHls()- >>> output = hsv(input) # 2x3x4x5+ >>> hls = kornia.color.RgbToHls()+ >>> output = hls(input) # 2x3x4x5"""diff --git a/kornia/color/hsv.py b/kornia/color/hsv.pyindex b06081a51..ea460f578 100644--- a/kornia/color/hsv.py+++ b/kornia/color/hsv.py@@ -19,6 +19,8 @@ class HsvToRgb(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> rgb = kornia.color.HsvToRgb()>>> output = rgb(input) # 2x3x4x5@@ -91,6 +93,8 @@ class RgbToHsv(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> hsv = kornia.color.RgbToHsv()>>> output = hsv(input) # 2x3x4x5diff --git a/kornia/color/rgb.py b/kornia/color/rgb.pyindex f6fd6bf2c..902079876 100644--- a/kornia/color/rgb.py+++ b/kornia/color/rgb.py@@ -19,6 +19,8 @@ class RgbToBgr(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> bgr = kornia.color.RgbToBgr()>>> output = bgr(input) # 2x3x4x5@@ -64,6 +66,8 @@ class BgrToRgb(nn.Module):Examples::+ >>> import torch+ >>> import kornia>>> input = torch.rand(2, 3, 4, 5)>>> rgb = kornia.color.BgrToRgb()>>> output = rgb(input) # 2x3x4x5diff --git a/kornia/color/ycbcr.py b/kornia/color/ycbcr.pynew file mode 100644index 000000000..fdec85e69--- /dev/null+++ b/kornia/color/ycbcr.py@@ -0,0 +1,127 @@+import torch+import torch.nn as nn+++class YcbcrToRgb(nn.Module):+ r"""Convert image from YCbCr to Rgb+ The image data is assumed to be in the range of (0, 1).++ args:+ image (torch.Tensor): YCbCr image to be converted to RGB.++ returns:+ torch.tensor: RGB version of the image.++ shape:+ - image: :math:`(*, 3, H, W)`+ - output: :math:`(*, 3, H, W)`++ Examples::++ >>> import torch+ >>> import kornia+ >>> input = torch.rand(2, 3, 4, 5)+ >>> rgb = kornia.color.YcbcrToRgb()+ >>> output = rgb(input) # 2x3x4x5++ """++ def __init__(self) -> None:+ super(YcbcrToRgb, self).__init__()++ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore+ return ycbcr_to_rgb(image)+++def ycbcr_to_rgb(image: torch.Tensor) -> torch.Tensor:+ r"""Convert an YCbCr image to RGB+ The image data is assumed to be in the range of (0, 1).++ Args:+ image (torch.Tensor): YCbCr Image to be converted to RGB.+++ Returns:+ torch.Tensor: RGB version of the 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 or image.shape[-3] != 3:+ raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"+ .format(image.shape))++ y: torch.Tensor = image[..., 0, :, :]+ cb: torch.Tensor = image[..., 1, :, :]+ cr: torch.Tensor = image[..., 2, :, :]++ delta: float = .5+ cb_shifted: torch.Tensor = cb - delta+ cr_shifted: torch.Tensor = cr - delta++ r: torch.Tensor = y + 1.403 * cr_shifted+ g: torch.Tensor = y - .714 * cr_shifted - .344 * cb_shifted+ b: torch.Tensor = y + 1.773 * cb_shifted+ return torch.stack((r, g, b), -3)+++class RgbToYcbcr(nn.Module):+ r"""Convert image from RGB to YCbCr+ The image data is assumed to be in the range of (0, 1).++ args:+ image (torch.Tensor): RGB image to be converted to YCbCr.++ returns:+ torch.tensor: YCbCr version of the image.++ shape:+ - image: :math:`(*, 3, H, W)`+ - output: :math:`(*, 3, H, W)`++ Examples::++ >>> import torch+ >>> import kornia+ >>> input = torch.rand(2, 3, 4, 5)+ >>> ycbcr = kornia.color.RgbToYcbcr()+ >>> output = ycbcr(input) # 2x3x4x5++ """++ def __init__(self) -> None:+ super(RgbToYcbcr, self).__init__()++ def forward(self, image: torch.Tensor) -> torch.Tensor: # type: ignore+ return rgb_to_ycbcr(image)+++def rgb_to_ycbcr(image: torch.Tensor) -> torch.Tensor:+ r"""Convert an RGB image to YCbCr.++ Args:+ image (torch.Tensor): RGB Image to be converted to YCbCr.++ Returns:+ torch.Tensor: YCbCr version of the 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 or image.shape[-3] != 3:+ raise ValueError("Input size must have a shape of (*, 3, H, W). Got {}"+ .format(image.shape))++ r: torch.Tensor = image[..., 0, :, :]+ g: torch.Tensor = image[..., 1, :, :]+ b: torch.Tensor = image[..., 2, :, :]++ delta = .5+ y: torch.Tensor = .299 * r + .587 * g + .114 * b+ cb: torch.Tensor = (b - y) * .564 + delta+ cr: torch.Tensor = (r - y) * .713 + delta+ return torch.stack((y, cb, cr), -3)diff --git a/test/color/test_ycbcr.py b/test/color/test_ycbcr.pynew file mode 100644index 000000000..d71c61540--- /dev/null+++ b/test/color/test_ycbcr.py@@ -0,0 +1,248 @@+import pytest++import kornia+import kornia.testing as utils # test utils++import cv2+import numpy as np++import torch+from torch.autograd import gradcheck+from torch.testing import assert_allclose+++class TestRgbToYcbcr:++ def test_rgb_to_ycbcr(self):+ data = torch.tensor([[[0.7925, 0.8704, 0.2771, 0.3279, 0.0193],+ [0.2483, 0.6212, 0.9859, 0.5044, 0.5621],+ [0.5762, 0.3959, 0.2931, 0.2669, 0.0243],+ [0.6989, 0.0529, 0.8344, 0.6523, 0.8980],+ [0.5181, 0.9341, 0.2172, 0.0520, 0.7266]],+ [[0.8413, 0.0284, 0.3625, 0.8864, 0.5595],+ [0.3791, 0.0235, 0.4251, 0.0619, 0.5270],+ [0.3516, 0.8005, 0.9571, 0.4113, 0.6119],+ [0.0632, 0.8836, 0.0261, 0.1550, 0.4923],+ [0.2332, 0.7044, 0.9514, 0.2443, 0.2818]],+ [[0.6899, 0.2063, 0.3179, 0.8989, 0.4378],+ [0.0384, 0.5230, 0.6416, 0.9749, 0.7863],+ [0.8577, 0.3115, 0.2375, 0.5446, 0.9837],+ [0.3213, 0.6618, 0.5977, 0.3999, 0.4962],+ [0.1385, 0.5831, 0.9756, 0.8714, 0.8017]]]) # 3x5x5++ y_expected = torch.tensor([[0.8094629, 0.30041942, 0.33188093, 0.7208117, 0.38409105],+ [0.30114722, 0.2591345, 0.6174494, 0.29830942, 0.5670594],+ [0.47643244, 0.623786, 0.6765313, 0.38332558, 0.47857922],+ [0.2826805, 0.60994685, 0.33298355, 0.3316514, 0.6140681],+ [0.30756146, 0.75926465, 0.7346588, 0.25828302, 0.4740529]])+ cr_expected = torch.tensor([[0.48791525, 0.9063825, 0.46094114, 0.21983841, 0.23989305],+ [0.46231723, 0.7581379, 0.7626976, 0.6469568, 0.496467],+ [0.5711212, 0.33752257, 0.22661471, 0.4169921, 0.17608929],+ [0.7967522, 0.10283372, 0.85753804, 0.72865105, 0.70245713],+ [0.65009415, 0.6246666, 0.13107029, 0.35291404, 0.6800583]])+ cb_expected = torch.tensor([[0.43258172, 0.4469004, 0.492101, 0.60042214, 0.53028214],+ [0.35180065, 0.64882994, 0.51362985, 0.8815981, 0.62366176],+ [0.71505237, 0.32387614, 0.2523859, 0.5909421, 0.78490996],+ [0.52178127, 0.529264, 0.64931786, 0.53848785, 0.43353173],+ [0.4046721, 0.40065458, 0.6358658, 0.8458253, 0.6847739]])++ # Kornia+ f = kornia.color.RgbToYcbcr()+ result = f(data)++ y = result[0, :, :]+ cb = result[1, :, :]+ cr = result[2, :, :]++ assert_allclose(y, y_expected)+ assert_allclose(cb, cb_expected)+ assert_allclose(cr, cr_expected)++ def test_batch_rgb_to_ycbcr(self):+ data = torch.tensor([[[0.7925, 0.8704, 0.2771, 0.3279, 0.0193],+ [0.2483, 0.6212, 0.9859, 0.5044, 0.5621],+ [0.5762, 0.3959, 0.2931, 0.2669, 0.0243],+ [0.6989, 0.0529, 0.8344, 0.6523, 0.8980],+ [0.5181, 0.9341, 0.2172, 0.0520, 0.7266]],+ [[0.8413, 0.0284, 0.3625, 0.8864, 0.5595],+ [0.3791, 0.0235, 0.4251, 0.0619, 0.5270],+ [0.3516, 0.8005, 0.9571, 0.4113, 0.6119],+ [0.0632, 0.8836, 0.0261, 0.1550, 0.4923],+ [0.2332, 0.7044, 0.9514, 0.2443, 0.2818]],+ [[0.6899, 0.2063, 0.3179, 0.8989, 0.4378],+ [0.0384, 0.5230, 0.6416, 0.9749, 0.7863],+ [0.8577, 0.3115, 0.2375, 0.5446, 0.9837],+ [0.3213, 0.6618, 0.5977, 0.3999, 0.4962],+ [0.1385, 0.5831, 0.9756, 0.8714, 0.8017]]]) # 3x5x5++ # OpenCV+ expected = torch.tensor([[[0.8094629, 0.30041942, 0.33188093, 0.7208117, 0.38409105],+ [0.30114722, 0.2591345, 0.6174494, 0.29830942, 0.5670594],+ [0.47643244, 0.623786, 0.6765313, 0.38332558, 0.47857922],+ [0.2826805, 0.60994685, 0.33298355, 0.3316514, 0.6140681],+ [0.30756146, 0.75926465, 0.7346588, 0.25828302, 0.4740529]],+ [[0.43258172, 0.4469004, 0.492101, 0.60042214, 0.53028214],+ [0.35180065, 0.64882994, 0.51362985, 0.8815981, 0.62366176],+ [0.71505237, 0.32387614, 0.2523859, 0.5909421, 0.78490996],+ [0.52178127, 0.529264, 0.64931786, 0.53848785, 0.43353173],+ [0.4046721, 0.40065458, 0.6358658, 0.8458253, 0.6847739]],+ [[0.48791525, 0.9063825, 0.46094114, 0.21983841, 0.23989305],+ [0.46231723, 0.7581379, 0.7626976, 0.6469568, 0.496467],+ [0.5711212, 0.33752257, 0.22661471, 0.4169921, 0.17608929],+ [0.7967522, 0.10283372, 0.85753804, 0.72865105, 0.70245713],+ [0.65009415, 0.6246666, 0.13107029, 0.35291404, 0.6800583]]])++ # Kornia+ f = kornia.color.RgbToYcbcr()+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5+ expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5+ assert_allclose(f(data), expected)++ def test_gradcheck(self):+ data = torch.rand(3, 5, 5) # 3x2x2++ data = utils.tensor_to_gradcheck_var(data) # to var++ assert gradcheck(kornia.color.RgbToYcbcr(), (data,),+ raise_exception=True)++ @pytest.mark.skip(reason="turn off all jit for a while")+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:+ return kornia.rgb_to_ycbcr(data)+ data = torch.tensor([[[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]]) # 3x2x2++ actual = op_script(data)+ expected = kornia.rgb_to_ycbcr(data)+ assert_allclose(actual, expected)+++class TestYcbcrToRgb:++ def test_ycbcr_to_rgb(self):+ data = torch.tensor([[[0.9892, 0.2620, 0.4184, 0.5286, 0.2793],+ [0.0722, 0.8828, 0.8714, 0.0657, 0.7798],+ [0.8118, 0.7522, 0.0260, 0.8811, 0.5226],+ [0.0644, 0.3648, 0.4448, 0.4202, 0.7316],+ [0.9138, 0.1956, 0.4257, 0.6381, 0.1353]],+ [[0.7408, 0.8529, 0.5119, 0.0220, 0.0226],+ [0.8963, 0.5652, 0.9568, 0.6977, 0.8221],+ [0.4645, 0.0478, 0.4952, 0.5492, 0.4861],+ [0.9980, 0.9978, 0.0281, 0.5283, 0.8146],+ [0.7789, 0.2663, 0.6437, 0.6926, 0.5627]],+ [[0.7377, 0.7152, 0.3080, 0.8515, 0.4841],+ [0.7192, 0.3297, 0.7337, 0.0230, 0.2464],+ [0.6399, 0.8998, 0.3838, 0.3043, 0.3774],+ [0.1281, 0.6731, 0.4218, 0.3963, 0.8541],+ [0.2245, 0.2413, 0.2351, 0.9522, 0.8158]]]) # 3x5x5++ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ data_cv[..., 1:] = data_cv[..., -1:0:-1]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_YCrCb2RGB)++ r_expected = torch.tensor([[1.3226931, 0.5639256, 0.14902398, 1.0217545, 0.2569923],+ [0.37973762, 0.64386904, 1.1992811, -0.603531, 0.4239992],+ [1.0080798, 1.3131194, -0.1370286, 0.60653293, 0.3505922],+ [-0.4573757, 0.6076593, 0.33508536, 0.27470887, 1.2284023],+ [0.52727354, -0.1673561, 0.05404532, 1.2725366, 0.5783674]])+ g_expected = torch.tensor([[0.736647, -0.01305042, 0.55139434, 0.44206098, 0.4548782],+ [-0.22063601, 0.98196536, 0.54739904, 0.33826917, 0.850068],+ [0.72412336, 0.6222996, 0.110618, 1.0039049, 0.614918],+ [0.15862459, 0.0699634, 0.66296846, 0.4845066, 0.3705502],+ [1.0145653, 0.46070462, 0.5654058, 0.24897486, -0.11174999]])+ b_expected = torch.tensor([[1.4161384, 0.88769174, 0.4394987, -0.31889397, -0.5671302],+ [0.77483994, 0.99839956, 1.6813064, 0.41622213, 1.3508832],+ [0.7488585, -0.04955059, 0.01748962, 0.9683316, 0.49795526],+ [0.9473541, 1.2473994, -0.3918787, 0.47037587, 1.2893858],+ [1.4082898, -0.21875012, 0.6804801, 0.9795798, 0.24646705]])++ # Kornia+ f = kornia.color.YcbcrToRgb()+ result = f(data)++ r = result[0, :, :]+ g = result[1, :, :]+ b = result[2, :, :]++ assert_allclose(r, r_expected)+ assert_allclose(g, g_expected)+ assert_allclose(b, b_expected)++ def test_batch_ycbcr_to_rgb(self):+ data = torch.tensor([[[0.9892, 0.2620, 0.4184, 0.5286, 0.2793],+ [0.0722, 0.8828, 0.8714, 0.0657, 0.7798],+ [0.8118, 0.7522, 0.0260, 0.8811, 0.5226],+ [0.0644, 0.3648, 0.4448, 0.4202, 0.7316],+ [0.9138, 0.1956, 0.4257, 0.6381, 0.1353]],+ [[0.7408, 0.8529, 0.5119, 0.0220, 0.0226],+ [0.8963, 0.5652, 0.9568, 0.6977, 0.8221],+ [0.4645, 0.0478, 0.4952, 0.5492, 0.4861],+ [0.9980, 0.9978, 0.0281, 0.5283, 0.8146],+ [0.7789, 0.2663, 0.6437, 0.6926, 0.5627]],+ [[0.7377, 0.7152, 0.3080, 0.8515, 0.4841],+ [0.7192, 0.3297, 0.7337, 0.0230, 0.2464],+ [0.6399, 0.8998, 0.3838, 0.3043, 0.3774],+ [0.1281, 0.6731, 0.4218, 0.3963, 0.8541],+ [0.2245, 0.2413, 0.2351, 0.9522, 0.8158]]]) # 3x5x5++ # OpenCV+ data_cv = data.numpy().transpose(1, 2, 0).copy()+ data_cv[..., 1:] = data_cv[..., -1:0:-1]+ expected = cv2.cvtColor(data_cv, cv2.COLOR_YCrCb2RGB)++ expected = torch.tensor([[[1.3226931, 0.5639256, 0.14902398, 1.0217545, 0.2569923],+ [0.37973762, 0.64386904, 1.1992811, -0.603531, 0.4239992],+ [1.0080798, 1.3131194, -0.1370286, 0.60653293, 0.3505922],+ [-0.4573757, 0.6076593, 0.33508536, 0.27470887, 1.2284023],+ [0.52727354, -0.1673561, 0.05404532, 1.2725366, 0.5783674]],+ [[0.736647, -0.01305042, 0.55139434, 0.44206098, 0.4548782],+ [-0.22063601, 0.98196536, 0.54739904, 0.33826917, 0.850068],+ [0.72412336, 0.6222996, 0.110618, 1.0039049, 0.614918],+ [0.15862459, 0.0699634, 0.66296846, 0.4845066, 0.3705502],+ [1.0145653, 0.46070462, 0.5654058, 0.24897486, -0.11174999]],+ [[1.4161384, 0.88769174, 0.4394987, -0.31889397, -0.5671302],+ [0.77483994, 0.99839956, 1.6813064, 0.41622213, 1.3508832],+ [0.7488585, -0.04955059, 0.01748962, 0.9683316, 0.49795526],+ [0.9473541, 1.2473994, -0.3918787, 0.47037587, 1.2893858],+ [1.4082898, -0.21875012, 0.6804801, 0.9795798, 0.24646705]]])++ # Kornia+ f = kornia.color.YcbcrToRgb()+ data = data.repeat(2, 1, 1, 1) # 2x3x5x5+ expected = expected.repeat(2, 1, 1, 1) # 2x3x5x5+ assert_allclose(f(data), expected)++ def test_gradcheck(self):+ data = torch.rand(3, 5, 5) # 3x5x5+ data = utils.tensor_to_gradcheck_var(data) # to var++ assert gradcheck(kornia.color.YcbcrToRgb(), (data,),+ raise_exception=True)++ @pytest.mark.skip(reason="turn off all jit for a while")+ def test_jit(self):+ @torch.jit.script+ def op_script(data: torch.Tensor) -> torch.Tensor:+ return kornia.ycbcr_to_rgb(data)++ data = torch.tensor([[[[21., 22.],+ [22., 22.]],++ [[13., 14.],+ [14., 14.]],++ [[8., 8.],+ [8., 8.]]]]) # 3x2x2++ actual = op_script(data)+ expected = kornia.ycbcr_to_rgb(data)+ assert_allclose(actual, expected)