Mahotas - RGB 到 XYZ 顏色空間轉換



XYZ 顏色空間是一個三維顏色模型,它基於人類感知,表示顏色的亮度、色度和強度。

在 XYZ 顏色空間中:

  • Y 分量表示顏色的亮度。
  • X 和 Z 分量確定色度座標或顏色在光譜中的位置。
  • 透過組合不同的 X、Y 和 Z 值,可以表示 XYZ 顏色空間內的任何可見顏色。

當我們從 RGB 轉換為 XYZ 時,我們取顏色的紅、綠、藍值,並將它們轉換為稱為 XYZ 的不同值。這有助於我們將顏色資訊與它在特定顯示器或裝置上的顯示方式的細節分開。

Mahotas 中的 RGB 到 XYZ 轉換

在 Mahotas 中,我們可以使用 **colors.rgb2xyz()** 函式將 RGB 影像轉換為 XYZ 影像。

Mahotas 中的 RGB 到 XYZ 轉換包含以下步驟:

  • **歸一化 RGB 值** - 將畫素的 RGB 值(通常表示為 0 到 255 之間的整數)歸一化到 0 到 1 之間的歸一化範圍。

    此步驟確保 RGB 值一致且可比較。

  • **伽馬校正** - 在從 RGB 轉換為 XYZ 之前,Mahotas 會對 RGB 值應用伽馬校正。

    伽馬校正調整影像的亮度級別,確保生成的 XYZ 值更準確地表示原始顏色。

  • **線性化 RGB 值** - 伽馬校正後,RGB 值將轉換為線性顏色空間。在這個線性 RGB 顏色空間中,強度值與實際物理光強度成比例。

    這種線性變換允許更準確的顏色計算。

  • **轉換矩陣** - Mahotas 使用轉換矩陣將線性 RGB 值轉換為 XYZ 值。轉換矩陣表示 RGB 和 XYZ 顏色空間之間的關係。

    它包含確定每個顏色通道對生成的 XYZ 值貢獻多少的係數。

  • **輸出** - 應用轉換矩陣後,Mahotas 提供 XYZ 值作為輸出。這些 XYZ 值以更感知均勻且更接近人類視覺系統感知顏色的顏色空間表示輸入 RGB 影像的顏色。

使用 mahotas.colors.rgb2xyz() 函式

mahotas.colors.rgb2xyz() 函式以 RGB 影像作為輸入,並返回影像的 XYZ 顏色空間版本。

生成的 XYZ 影像保留了原始 RGB 影像的結構和內容。

語法

以下是 mahotas 中 rgb2xyz() 函式的基本語法:

mahotas.colors.rgb2xyz(rgb, dtype={float})

其中:

  • **rgb** - 它是 RGB 顏色空間中的輸入影像。

  • **dtype(可選)** - 它是返回影像的資料型別(預設為 float)

示例

在下面的示例中,我們使用 mh.colors.rgb2xyz() 函式將 RGB 影像轉換為 XYZ 影像:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Create a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the XYZ image
axes[1].imshow(xyz_image)
axes[1].set_title('XYZ Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

以下是上述程式碼的輸出:

RGB XYZ Image

使用轉換矩陣

我們可以用來將 RGB 影像轉換為 XYZ 影像的另一種方法是使用轉換矩陣。轉換矩陣包含將畫素的 RGB 分量與 XYZ 分量相關的係數。

可以按如下方式計算每個畫素的 XYZ 分量的值:

X = 0.412456 * r + 0.357576 * g + 0.180437 * b
Y = 0.212672 * r + 0.715152 * g + 0.072175 * b
Z = 0.019334 * r + 0.119193 * g + 0.950471 * b

其中 **X、Y 和 Z** 值表示 XYZ 顏色空間中的對應值。

示例

以下示例顯示了使用 RGB 通道的轉換矩陣值將 RGB 影像轉換為 XYZ 影像:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Function to convert RGB to XYZ
def rgb_to_xyz(rgb):
   height, width, _ = rgb.shape
   xyz_image = np.zeros((height, width, 3))
   for i in range(height):
      for j in range(width):
         # Separating the RGB image into individual channels
         r, g, b = rgb[i, j]
         x = 0.412456 * r + 0.357576 * g + 0.180437 * b
         y = 0.212672 * r + 0.715152 * g + 0.072175 * b
         z = 0.019334 * r + 0.119193 * g + 0.950471 * b
         xyz_image[i, j] = [x, y, z]
   return xyz_image
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = rgb_to_xyz(image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('RGB Image')
axes[0].set_axis_off()
# Displaying the XYZ image
axes[1].imshow(xyz_image)
axes[1].set_title('XYZ Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

RGB XYZ Image1
廣告