Mahotas - RGB轉棕褐色



棕褐色指的是一種特殊的著色效果,使圖片看起來複古而溫暖。

當你看到一張棕褐色的照片時,它看起來會呈現出一種紅棕色調。這就像透過懷舊濾鏡觀看影像,賦予它一種復古的感覺。

要將 RGB 影像轉換為棕褐色,你需要轉換每個畫素的紅色、綠色和藍色顏色通道,以達到所需的棕褐色調。

Mahotas 中的 RGB 轉棕褐色轉換

在 Mahotas 中,我們可以使用 **colors.rgb2sepia()** 函式將 RGB 影像轉換為棕褐色調的影像。

為了理解轉換過程,讓我們從 RGB 顏色模型開始 -

  • 在 RGB 中,影像由三種原色組成 - 紅色、綠色和藍色。
  • 影像中的每個畫素都具有這三種顏色的值,這些值決定了它的整體顏色。例如,如果一個畫素具有較高的紅色值和較低的綠色和藍色值,它將顯示為紅色陰影。
  • 現在,要使用 mahotas 將 RGB 影像轉換為棕褐色,我們遵循特定的公式。該公式涉及計算每個畫素的紅色、綠色和藍色通道的新值。
  • 這些新值透過賦予影像溫暖的棕褐色調來建立棕褐色效果。

RGB 轉棕褐色轉換步驟

以下是轉換過程的簡單解釋 -

  • **以 RGB 影像開始** - RGB 影像由三個顏色通道組成 - 紅色、綠色和藍色。影像中的每個畫素都具有這三個通道的強度值,範圍從 0 到 255。

  • **計算每個畫素的強度** - 要轉換為棕褐色,我們首先需要計算每個畫素的整體強度。這可以透過取紅色、綠色和藍色顏色通道的加權平均值來完成。

    用於平均值的權重可以根據所需的棕褐色效果而有所不同。

  • **調整強度值** - 獲得強度值後,我們可以應用一些特定的變換來獲得棕褐色效果。這些變換涉及以模擬棕褐色調的方式調整紅色、綠色和藍色通道的級別。

    這可以透過增加紅色通道的強度,降低藍色通道的強度,並保持綠色通道相對不變來完成。

  • **裁剪強度值** - 調整後,某些強度值可能會超出有效範圍(對於 8 點陣圖像,範圍為 0 到 255)。為了確保值保持在此範圍內,我們需要對其進行裁剪。

    低於 0 的值設定為 0,高於 255 的值設定為 255。

  • **重建棕褐色影像** - 最後,調整後的強度值用於重建棕褐色影像。影像現在顯示出所需的棕褐色調,使其呈現復古外觀。

使用 mahotas.colors.rgb2sepia() 函式

mahotas.colors.rgb2sepia() 函式以 RGB 影像作為輸入,並返回影像的棕褐色版本。

生成的棕褐色影像保留了原始 RGB 影像的整體結構和內容,但引入了溫暖的棕褐色調。

語法

以下是 mahotas 中 rgb2sepia() 函式的基本語法 -

mahotas.colors.rgb2sepia(rgb)

其中,**rgb** 是 RGB 顏色空間中的輸入影像。

示例

在以下示例中,我們使用 mh.colors.rgb2sepia() 函式將 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 Sepia
sepia_image = mh.colors.rgb2sepia(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 sepia image
axes[1].imshow(sepia_image)
axes[1].set_title('Sepia Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

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

RGB Sepia Color Image

使用變換因子

我們可以用來將 RGB 轉換為棕褐色的另一種方法是使用預定的係數調整每個顏色通道的強度,這些係數基於每個通道對最終棕褐色影像的貢獻。

每個通道對棕褐色的貢獻計算如下 -

TR = 0.393 * R + 0.769 * G + 0.189 * B
TG = 0.349 * R + 0.686 * G + 0.168 * B
TB = 0.272 * R + 0.534 * G + 0.131 * B

其中 **TR、TG 和 TB** 分別是紅色、綠色和藍色的變換因子。

將這些變換應用於 RGB 影像中的每個畫素會導致整個棕褐色影像。

示例

以下示例顯示了使用 RGB 通道的變換因子將 RGB 影像轉換為棕褐色的過程 -

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
# Loading the image
image = mh.imread('sun.png')
# Getting the dimensions of the image
height, width, _ = image.shape
# Converting it to Sepia
# Creating an empty array for the sepia image
sepia_image = np.empty_like(image)
for y in range(height):
   for x in range(width):
      # Geting the RGB values of the pixel
      r, g, b = image[y, x]
      # Calculating tr, tg, tb
      tr = int(0.393 * r + 0.769 * g + 0.189 * b)
      tg = int(0.349 * r + 0.686 * g + 0.168 * b)
      tb = int(0.272 * r + 0.534 * g + 0.131 * b)
      # Normalizing the values if necessary
      if tr > 255:
         tr = 255
      if tg > 255:
         tg = 255
      if tb > 255:
         tb = 255
      # Setting the new RGB values in the sepia image array
      sepia_image[y, x] = [tr, tg, tb]
# Creating a figure and axes for subplots
fig, axes = plt.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 sepia image
axes[1].imshow(sepia_image)
axes[1].set_title('Sepia Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
plt.tight_layout()
# Showing the figures
plt.show()

輸出

上述程式碼的輸出如下 -

RGB Sepia Color Image1
廣告