Mahotas - RGB 轉灰度



影像處理中的 RGB 到灰度轉換將 RGB 顏色空間中的彩色影像轉換為灰度影像。

  • RGB 影像由三個顏色通道組成——紅色、綠色和藍色。RGB 影像中的每個畫素都由這三個通道的強度值的組合表示,從而產生廣泛的顏色範圍。
  • 另一方面,灰度影像是單通道影像(僅包含(灰色)的陰影),其中每個畫素表示原始影像中相應位置的強度。
  • 強度值範圍從黑色(0)到白色(255),中間是灰色的陰影。

Mahotas 中的 RGB 轉灰度轉換

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

該函式根據其 RGB 值的加權平均值計算每個畫素的灰度強度。

權重反映了人類對顏色的感知,紅色權重最高,其次是綠色,然後是藍色。

mahotas.colors.rgb2gray() 函式

mahotas.colors.rgb2gray() 函式以 RGB 影像作為輸入,並返回影像的灰度版本。

生成的灰度影像保留了原始 RGB 影像的結構和整體內容,但缺少顏色資訊。

語法

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

mahotas.colors.rgb2gray(rgb_image, dtype=float)

其中,

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

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

示例

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

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

rgb2gray Color

使用 RGB 通道的平均值

我們還可以使用 RGB 通道的平均值將 RGB 影像轉換為灰度影像。

將每個畫素的紅色、綠色和藍色的強度相加併除以三,即可獲得平均強度值。我們可以使用 numpy 庫的 mean() 函式實現這一點。

生成的影像將是灰度影像,具有單個通道,其中每個畫素表示跨 RGB 通道的平均強度,其中每個顏色通道對整體灰度強度都有同等貢獻。

示例

以下示例顯示了使用 RGB 通道平均值將 RGB 影像轉換為灰度的過程:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
gray_image = np.mean(image, axis=2).astype(np.uint8)
# 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 grayscale image
axes[1].imshow(gray_image, cmap='gray')
axes[1].set_title('Grayscale Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Average RGB Channels

使用亮度

亮度是指物件或影像的感知亮度或亮度。

在影像處理中,亮度為每個畫素的紅色、綠色和藍色顏色通道分配特定的權重,並將它們組合起來計算灰度強度值。

RGB 顏色空間中的亮度可以使用以下公式計算:

Luminosity = 0.2989 * R + 0.5870 * G + 0.1140 * B

其中,值 **0.2989、0.5870** 和 **0.1140** 分別是分配給紅色、綠色和藍色通道的權重。這些權重來自用於數字顯示的標準 Rec.709 顏色空間。

與簡單地平均 RGB 通道相比,此方法可產生更好的結果,因為它可以更好地捕捉原始影像的視覺感知。

示例

在這裡,我們定義了 RGB 的亮度,以將彩色影像轉換為其等效的灰度影像:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to gray
gray_image = np.dot(image[..., :3], [0.2989, 0.5870, 0.1140]).astype(np.uint8)
# 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 grayscale image
axes[1].imshow(gray_image, cmap='gray')
axes[1].set_title('Grayscale Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

執行上述程式碼後,我們將獲得以下輸出:

Luminosity Image
廣告