Shortcuts

Source code for caer.color._bgr

#    _____           ______  _____
#  / ____/    /\    |  ____ |  __ \
# | |        /  \   | |__   | |__) | Caer - Modern Computer Vision
# | |       / /\ \  |  __|  |  _  /  Languages: Python, C, C++, Cuda
# | |___   / ____ \ | |____ | | \ \  http://github.com/jasmcaus/caer
#  \_____\/_/    \_ \______ |_|  \_\

# Licensed under the MIT License <http://opensource.org/licenses/MIT>
# SPDX-License-Identifier: MIT
# Copyright (c) 2020-2021 The Caer Authors <http://github.com/jasmcaus>


import cv2 as cv

from ..coreten import Tensor, to_tensor
from ._constants import BGR2RGB, BGR2GRAY, BGR2HSV, BGR2LAB, BGR2HLS, BGR2YUV, BGR2LUV

__all__ = [
    "bgr2gray",
    "bgr2hsv",
    "bgr2lab",
    "bgr2rgb",
    "bgr2hls",
    "bgr2yuv",
    "bgr2luv",
]


def _is_bgr_image(tens):
    # tens = to_tensor(tens)
    # return tens.is_bgr()
    return len(tens.shape) == 3 and tens.shape[-1] == 3


[docs]def bgr2rgb(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its RGB version. Args: tens (Tensor): Valid BGR Tensor Returns: RGB Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its RGB counterpart" ) img = cv.cvtColor(tens, BGR2RGB) return to_tensor(img, cspace="rgb")
[docs]def bgr2gray(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its Grayscale version. Args: tens (Tensor): Valid BGR Tensor Returns: Grayscale Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its Grayscale counterpart" ) tens = to_tensor(tens) tens._nullprt() # raises a ValueError if we're dealing with a Foreign Tensor with illegal `.cspace` value img = cv.cvtColor(tens, BGR2GRAY) return to_tensor(img, cspace="gray")
[docs]def bgr2hsv(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its HSV version. Args: tens (Tensor): Valid BGR Tensor Returns: HSV Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its HSV counterpart" ) img = cv.cvtColor(tens, BGR2HSV) return to_tensor(img, cspace="hsv")
[docs]def bgr2lab(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its LAB version. Args: tens (Tensor): Valid BGR Tensor Returns: LAB Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its LAB counterpart" ) img = cv.cvtColor(tens, BGR2LAB) return to_tensor(img, cspace="lab")
def bgr2hls(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its HLS version. Args: tens (Tensor): Valid BGR Tensor Returns: HLS Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its HLS counterpart" ) img = cv.cvtColor(tens, BGR2HLS) return to_tensor(img, cspace="hls") def bgr2yuv(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its YUV version. Args: tens (Tensor): Valid BGR Tensor Returns: YUV Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its YUV counterpart" ) img = cv.cvtColor(tens, BGR2YUV) return to_tensor(img, cspace="yuv") def bgr2luv(tens: Tensor) -> Tensor: r""" Converts a BGR Tensor to its LUV version. Args: tens (Tensor): Valid BGR Tensor Returns: YUV Tensor of shape ``(height, width, channels)`` Raises: ValueError: If `tens` is not of shape 3 """ if not _is_bgr_image(tens): raise ValueError( f"Tensor of shape 3 expected. Found shape {len(tens.shape)}. " "This function converts a BGR Tensor to its LUV counterpart" ) img = cv.cvtColor(tens, BGR2LUV) return to_tensor(img, cspace="luv")
Read the Docs v: latest
Versions
latest
stable
Downloads
pdf
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.