簡體   English   中英

如何僅使用 NumPy 實現 tfio rgb2xyz?

[英]How can I implement tfio rgb2xyz just with NumPy?

我正在嘗試僅使用 NumPy 來執行此代碼

我用過 skimage 但它並不像 tfio rgb_to_xyz 那樣完美

所以

def rgb_to_xyz(input, name=None):
    """
    Convert a RGB image to CIE XYZ.
    Args:
      input: A 3-D (`[H, W, 3]`) or 4-D (`[N, H, W, 3]`) Tensor.
      name: A name for the operation (optional).
    Returns:
      A 3-D (`[H, W, 3]`) or 4-D (`[N, H, W, 3]`) Tensor.
    """
    input = tf.convert_to_tensor(input)
    assert input.dtype in (tf.float16, tf.float32, tf.float64)

    kernel = tf.constant(
        [
            [0.412453, 0.357580, 0.180423],
            [0.212671, 0.715160, 0.072169],
            [0.019334, 0.119193, 0.950227],
        ],
        input.dtype,
    )
    value = tf.where(
        tf.math.greater(input, 0.04045),
        tf.math.pow((input + 0.055) / 1.055, 2.4),
        input / 12.92,
    )
    return tf.tensordot(value, tf.transpose(kernel), axes=((-1,), (0,)))

這是一個使用 NumPy 的實現:

def rgb_to_xyz(rgb):
    """
    Convert a RGB image to CIE XYZ.
    Args:
      input: A 3-D (`[H, W, 3]`).
    Returns:
      A 3-D (`[H, W, 3]`).
    """
    if rgb.dtype == 'uint8':
        rgb = rgb.astype(float)/255  # Convert range from [0, 255] to [0, 1]
    # If not uint8, assume type is float64 or float32 in range [0, 1]

    # Inverse sRGB Gamma (convert to "linear RGB")
    lin_rgb = rgb / 12.92
    lin_rgb[rgb > 0.04045] = ((rgb[rgb > 0.04045] + 0.055) / 1.055) ** 2.4

    k = np.array([
                    [0.412453, 0.357580, 0.180423],
                    [0.212671, 0.715160, 0.072169],
                    [0.019334, 0.119193, 0.950227],
                ], rgb.dtype)

    # Left multiply k by lin_rgb triplets. xyz[r, c] = k * lin_rgb[r, c] (use k.T and reverse order for using matmul).
    xyz = np.matmul(lin_rgb, k.T)

    return xyz

我認為由於 NumPy 廣播規則支持 4-D 輸入。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM