shift_rgb.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from kornia.core import Tensor, stack
  18. from kornia.core.check import KORNIA_CHECK_IS_COLOR, KORNIA_CHECK_IS_TENSOR
  19. def shift_rgb(image: Tensor, r_shift: Tensor, g_shift: Tensor, b_shift: Tensor) -> Tensor:
  20. """Shift rgb channels.
  21. Shift each image's channel by either r_shift for red, g_shift for green and b_shift for blue channels.
  22. """
  23. KORNIA_CHECK_IS_TENSOR(image)
  24. KORNIA_CHECK_IS_COLOR(image, f"with shape {image.shape}")
  25. shifts = [r_shift, g_shift, b_shift]
  26. shifted = (image + stack(shifts, dim=1).view(-1, 3, 1, 1).to(image)).clamp_(min=0, max=1)
  27. return shifted