Mahotas - XYZ 到 RGB 顏色空間轉換



在上一個教程中,我們學習了 XYZ 顏色空間、RGB 顏色空間以及 RGB 到 XYZ 的轉換。現在讓我們討論一下 XYZ 顏色空間到 RGB 顏色空間的轉換。

當我們將 XYZ 轉換為 RGB 時,我們採用顏色的 XYZ 值(表示其感知特性),並將它們轉換為紅色、綠色和藍色值。

這種轉換允許我們將顏色表示為適合在特定裝置或螢幕上顯示的格式。

Mahotas 中的 XYZ 到 RGB 轉換

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

Mahotas 中的 XYZ 到 RGB 轉換涉及以下步驟:

  • **歸一化 XYZ 值** - 歸一化 X、Y 和 Z 值,使其範圍在 0 到 1 之間。此步驟確保 XYZ 值相對於參考白點,並允許進行一致的顏色計算。

  • **將歸一化的 XYZ 轉換為線性 RGB** - 接下來,使用轉換矩陣將歸一化的 XYZ 值轉換為線性 RGB 值。轉換矩陣指定 XYZ 座標如何影響結果顏色的紅色、綠色和藍色分量。

    進行矩陣乘法運算以獲得線性 RGB 值。

  • **應用伽馬校正** - 伽馬校正調整 RGB 值的亮度,以匹配人類視覺系統的響應。

  • **縮放 RGB 值** - 伽馬校正後,RGB 值通常在 0 到 1 的範圍內。為了在 8 位範圍內 (0-255) 表示顏色,需要縮放 RGB 值。

    將每個伽馬校正後的 RGB 值乘以 255 以將其縮放至適當的範圍。

  • **結果** - 應用縮放後,即可獲得 RGB 顏色值。這些值表示結果顏色的紅色、綠色和藍色通道的強度。

使用 mahotas.colors.xyz2rgb() 函式

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

生成的 RGB 影像保留了原始 XYZ 影像的結構和內容,但是會丟失一些顏色細節。

語法

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

mahotas.colors.xyz2rgb(xyz, dtype={float})

其中:

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

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

示例

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

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

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

XYZ RGB Conversion

使用轉換矩陣

我們可以使用轉換矩陣將 XYZ 影像轉換為 RGB 影像。轉換矩陣具有一組用於將 XYZ 畫素轉換為 RGB 畫素的值。

  • 透過進行轉換矩陣與 XYZ 影像之間的矩陣乘法,將 XYZ 畫素轉換為 RGB 畫素。
  • 我們透過使用 numpy 庫中的 dot() 函式來實現這一點。
  • 然後,透過將每個畫素的值乘以 255,再除以該畫素的最大強度,將每個畫素的值從 0 到 1 的範圍(XYZ 顏色的強度範圍)歸一化到 0 到 255 的範圍(RGB 顏色的強度範圍),以獲得 RGB 影像。

示例

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

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Function to convert XYZ to RGB
def xyz_to_rgb(xyz_image):
   # XYZ to RGB conversion matrix
   xyz_to_rgb_matrix = np.array([[3.2406, -1.5372, -0.4986],
   [-0.9689, 1.8758, 0.0415],[0.0557, -0.2040, 1.0570]])
   # Perform the XYZ to RGB conversion using matrix multiplication
   rgb_image = np.dot(xyz_image, xyz_to_rgb_matrix.T)
   # Scale the RGB values from the range [0, 1] to [0, 255]
   rgb_image = (rgb_image * 255.0 / np.max(rgb_image)).astype(np.uint8)
   return rgb_image
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to XYZ
xyz_image = mh.colors.rgb2xyz(image)
# Converting back to RGB (lossy)
rgb_image = xyz_to_rgb(xyz_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original XYZ image
axes[0].imshow(xyz_image)
axes[0].set_title('XYZ Image')
axes[0].set_axis_off()
# Displaying the RGB image
axes[1].imshow(rgb_image)
axes[1].set_title('RGB Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

XYZ RGB Conversion1
廣告