def adjust_saturation(input: torch.Tensor, saturation_factor: float) -> torch.Tensor: r"""Adjust color saturation of an image. See :class:`~kornia.color.AdjustSaturation` for details. """ if not torch.is_tensor(input): raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}") if not isinstance(saturation_factor, float): raise TypeError(f"The saturation_factor should be a float number.") # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # unpack the hsv values h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3) # transform the hue value and appl module s_out: torch.Tensor = torch.clamp(s * saturation_factor, min=0, max=1) # pack back back the corrected hue x_adjusted: torch.Tensor = torch.cat([h, s_out, v], dim=-3) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_hue(input: torch.Tensor, hue_factor: float) -> torch.Tensor: r"""Adjust hue of an image. See :class:`~kornia.color.AdjustHue` for details. """ if not torch.is_tensor(input): raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}") if not isinstance(hue_factor, float) and -0.5 < hue_factor < 0.5: raise TypeError( f"The hue_factor should be a float number in the range between" f" [-0.5, 0.5]. Got {type(hue_factor)}") # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # unpack the hsv values h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3) # transform the hue value and appl module h_out: torch.Tensor = torch.frac(h * hue_factor) # pack back back the corrected hue x_adjusted: torch.Tensor = torch.cat([h_out, s, v], dim=-3) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def transform(self, x): """ Returns jittered images. Args: x (torch.Tensor): observation tensor. Returns: torch.Tensor: processed observation tensor. """ # check if channel can be devided by three if x.shape[1] % 3 > 0: raise ValueError('color jitter is used with stacked RGB images') # flag for transformation order is_transforming_rgb_first = np.random.randint(2) # (batch, C, W, H) -> (batch, stack, 3, W, H) flat_rgb = x.view(x.shape[0], -1, 3, x.shape[2], x.shape[3]) if is_transforming_rgb_first: # transform contrast flat_rgb = self._transform_contrast(flat_rgb) # (batch, stack, 3, W, H) -> (batch * stack, 3, W, H) rgb_images = flat_rgb.view(-1, 3, x.shape[2], x.shape[3]) # RGB -> HSV hsv_images = rgb_to_hsv(rgb_images) # apply same transformation within the stacked images # (batch * stack, 3, W, H) -> (batch, stack, 3, W, H) flat_hsv = hsv_images.view(x.shape[0], -1, 3, x.shape[2], x.shape[3]) # transform hue flat_hsv = self._transform_hue(flat_hsv) # transform saturate flat_hsv = self._transform_saturate(flat_hsv) # transform brightness flat_hsv = self._transform_brightness(flat_hsv) # (batch, stack, 3, W, H) -> (batch * stack, 3, W, H) hsv_images = flat_hsv.view(-1, 3, x.shape[2], x.shape[3]) # HSV -> RGB rgb_images = hsv_to_rgb(hsv_images) # (batch * stack, 3, W, H) -> (batch, stack, 3, W, H) flat_rgb = rgb_images.view(x.shape[0], -1, 3, x.shape[2], x.shape[3]) if not is_transforming_rgb_first: # transform contrast flat_rgb = self._transform_contrast(flat_rgb) return flat_rgb.view(*x.shape)
def adjust_saturation( input: torch.Tensor, saturation_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust color saturation of an image. The input image is expected to be an RGB image in the range of [0, 1]. Args: input (torch.Tensor): Image/Tensor to be adjusted in the shape of :math:`(*, 3, H, W)`. saturation_factor (Union[float, torch.Tensor]): How much to adjust the saturation. 0 will give a black and white image, 1 will give the original image while 2 will enhance the saturation by a factor of 2. Return: torch.Tensor: Adjusted image in the shape of :math:`(*, 3, H, W)`. Example: >>> x = torch.ones(1, 3, 3, 3) >>> adjust_saturation(x, 2.) tensor([[[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], <BLANKLINE> [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], <BLANKLINE> [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]]]) >>> x = torch.ones(2, 3, 3, 3) >>> y = torch.ones(2) >>> out = adjust_saturation(x, y) >>> torch.nn.functional.mse_loss(x, out) tensor(0.) """ # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # perform the conversion x_adjusted: torch.Tensor = adjust_saturation_raw(x_hsv, saturation_factor) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_saturation(input: torch.Tensor, saturation_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust color saturation of an image. See :class:`~kornia.color.AdjustSaturation` for details. """ # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # perform the conversion x_adjusted: torch.Tensor = adjust_saturation_raw(x_hsv, saturation_factor) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_hue(input: torch.Tensor, hue_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust hue of an image. The input image is expected to be an RGB image in the range of [0, 1]. Args: input (torch.Tensor): Image to be adjusted in the shape of :math:`(*, 3, H, W)`. hue_factor (Union[float, torch.Tensor]): How much to shift the hue channel. Should be in [-PI, PI]. PI and -PI give complete reversal of hue channel in HSV space in positive and negative direction respectively. 0 means no shift. Therefore, both -PI and PI will give an image with complementary colors while 0 gives the original image. Return: torch.Tensor: Adjusted image in the shape of :math:`(*, 3, H, W)`. Example: >>> x = torch.ones(1, 3, 3, 3) >>> adjust_hue(x, 3.141516) tensor([[[[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], <BLANKLINE> [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], <BLANKLINE> [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]]]) >>> x = torch.ones(2, 3, 3, 3) >>> y = torch.ones(2) * 3.141516 >>> adjust_hue(x, y).shape torch.Size([2, 3, 3, 3]) """ # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # perform the conversion x_adjusted: torch.Tensor = adjust_hue_raw(x_hsv, hue_factor) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_saturation( input: torch.Tensor, saturation_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust color saturation of an image. See :class:`~kornia.color.AdjustSaturation` for details. """ if not torch.is_tensor(input): raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}") if not isinstance(saturation_factor, ( float, torch.Tensor, )): raise TypeError( f"The saturation_factor should be a float number or torch.Tensor." f"Got {type(saturation_factor)}") if isinstance(saturation_factor, float): saturation_factor = torch.tensor([saturation_factor]) saturation_factor = saturation_factor.to(input.device).to(input.dtype) if (saturation_factor < 0).any(): raise ValueError( f"Saturation factor must be non-negative. Got {saturation_factor}") for _ in input.shape[1:]: saturation_factor = torch.unsqueeze(saturation_factor, dim=-1) # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # unpack the hsv values h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3) # transform the hue value and appl module s_out: torch.Tensor = torch.clamp(s * saturation_factor, min=0, max=1) # pack back back the corrected hue x_adjusted: torch.Tensor = torch.cat([h, s_out, v], dim=-3) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_hue(input: torch.Tensor, hue_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust hue of an image. See :class:`~kornia.color.AdjustHue` for details. """ if not torch.is_tensor(input): raise TypeError(f"Input type is not a torch.Tensor. Got {type(input)}") if not isinstance(hue_factor, (float, torch.Tensor)): raise TypeError( f"The hue_factor should be a float number or torch.Tensor in the range between" f" [-PI, PI]. Got {type(hue_factor)}") if isinstance(hue_factor, float): hue_factor = torch.tensor([hue_factor]) hue_factor = hue_factor.to(input.device).to(input.dtype) if ((hue_factor < -pi) | (hue_factor > pi)).any(): raise ValueError( f"Hue-factor must be in the range [-PI, PI]. Got {hue_factor}") for _ in input.shape[1:]: hue_factor = torch.unsqueeze(hue_factor, dim=-1) # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # unpack the hsv values h, s, v = torch.chunk(x_hsv, chunks=3, dim=-3) # transform the hue value and appl module divisor: float = 2 * pi.item() h_out: torch.Tensor = torch.fmod(h + hue_factor, divisor) # pack back back the corrected hue x_adjusted: torch.Tensor = torch.cat([h_out, s, v], dim=-3) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_hue(input: torch.Tensor, hue_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust hue of an image. .. image:: _static/img/adjust_hue.png The input image is expected to be an RGB image in the range of [0, 1]. Args: input: Image to be adjusted in the shape of :math:`(*, 3, H, W)`. hue_factor: How much to shift the hue channel. Should be in [-PI, PI]. PI and -PI give complete reversal of hue channel in HSV space in positive and negative direction respectively. 0 means no shift. Therefore, both -PI and PI will give an image with complementary colors while 0 gives the original image. Return: Adjusted image in the shape of :math:`(*, 3, H, W)`. .. note:: See a working example `here <https://kornia-tutorials.readthedocs.io/en/latest/ image_enhancement.html>`__. Example: >>> x = torch.ones(1, 3, 2, 2) >>> adjust_hue(x, 3.141516).shape torch.Size([1, 3, 2, 2]) >>> x = torch.ones(2, 3, 3, 3) >>> y = torch.ones(2) * 3.141516 >>> adjust_hue(x, y).shape torch.Size([2, 3, 3, 3]) """ # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # perform the conversion x_adjusted: torch.Tensor = adjust_hue_raw(x_hsv, hue_factor) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out
def adjust_saturation( input: torch.Tensor, saturation_factor: Union[float, torch.Tensor]) -> torch.Tensor: r"""Adjust color saturation of an image. .. image:: _static/img/adjust_saturation.png The input image is expected to be an RGB image in the range of [0, 1]. Args: input: Image/Tensor to be adjusted in the shape of :math:`(*, 3, H, W)`. saturation_factor: How much to adjust the saturation. 0 will give a black and white image, 1 will give the original image while 2 will enhance the saturation by a factor of 2. Return: Adjusted image in the shape of :math:`(*, 3, H, W)`. .. note:: See a working example `here <https://kornia-tutorials.readthedocs.io/en/latest/ image_enhancement.html>`__. Example: >>> x = torch.ones(1, 3, 3, 3) >>> adjust_saturation(x, 2.).shape torch.Size([1, 3, 3, 3]) >>> x = torch.ones(2, 3, 3, 3) >>> y = torch.tensor([1., 2.]) >>> adjust_saturation(x, y).shape torch.Size([2, 3, 3, 3]) """ # convert the rgb image to hsv x_hsv: torch.Tensor = rgb_to_hsv(input) # perform the conversion x_adjusted: torch.Tensor = adjust_saturation_raw(x_hsv, saturation_factor) # convert back to rgb out: torch.Tensor = hsv_to_rgb(x_adjusted) return out