Mahotas 简明教程

Mahotas - RGB to Gray Conversion

在图像处理中 RGB 转灰度转换将 RGB 色彩空间中的彩色图像转换成灰度图像。

  1. RGB 图像由三个色彩通道构成− 红色、绿色、蓝色。RGB 图像中的每个像素都由三个通道的强度值的组合表示,从而产生各种各样的颜色。

  2. 而灰度图像则属于单通道图像(仅包含(灰色)阴影,其中每个像素都表示原始图像相应位置的强度。

  3. 强度值范围从黑色(0)到白色(255),并具有灰度的中间色调。

RGB to Gray Conversion in Mahotas

在 Mahotas 中,我们可以使用 colors.rgb2gray() 函数将 RGB 图像转换为灰度图像。

此函数根据其 RGB 值的加权平均值计算每个像素的灰度强度。

权重反映了人的色彩感知,其中红色有最高的权重,其次是绿色,然后是蓝色。

The mahotas.colors.rgb2gray() Function

mahotas.colors.rgb2gray() 函数获取 RGB 图像作为输入,并返回图像的灰度版本。

得到的灰度图像保留了原始 RGB 图像的结构和总体内容,但没有颜色信息。

以下是 mahotas 中 rgb2gray() 函数的基本语法 −

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

其中,

  1. rgb_image − 是 RGB 色彩空间中的输入图像。

  2. dtype (optional) − 是返回图像的数据类型(默认为 float)。

在以下示例中,我们正使用 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

Using Average of RGB Channels

我们还可以使用 RGB 通道的平均值将 RGB 图像转换为灰度图像。

对每个像素的红色、绿色和蓝色颜色强度求和并除以 3 以获得平均强度值。我们可以使用 numpy 库的 mean() 函数来实现这一点。

生成的图像将是一个灰度图像,具有单个通道,其中每个像素表示 RGB 通道的平均强度,其中每个颜色通道对整体灰度强度贡献相等。

Example

以下示例显示了使用 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

Using Luminosity

亮度是一个用于描述对象或图像的感知亮度或明暗的术语。

在图像处理中,亮度为每个像素的红色、绿色和蓝色颜色通道指定特定的权重,并将它们组合起来计算灰度强度值。

RGB 颜色空间中的亮度可以使用以下公式计算 -

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

其中,值 0.2989, 0.5870,0.1140 分别是分配给红色、绿色和蓝色通道的权重。这些权重来自用于数字显示的标准 Rec.709 色彩空间。

与简单地对 RGB 通道求平均值相比,这种方法产生了更好的结果,因为它更好地捕捉了原始图像的视觉感知。

Example

在这里,我们定义了 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