Mahotas 简明教程

Mahotas - RGB to Gray Conversion

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

RGB to grayscale conversion in image processing transforms a colored image in the RGB color space to a grayscale image.

  1. RGB images are composed of three color channels− red, green, and blue. Each pixel in an RGB image is represented by a combination of intensity values for these three channels, resulting in a wide range of colors.

  2. On the other hand, grayscale images are single−channel images (contain only shades of (gray) where each pixel represents the intensity of the corresponding location in the original image.

  3. The intensity values range from black (0) to white (255), with intermediate shades of gray.

RGB to Gray Conversion in Mahotas

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

In Mahotas, we can convert an RGB image to grayscale image using the colors.rgb2gray() function.

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

The function calculates the grayscale intensity of each pixel based on the weighted average of its RGB values.

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

The weights reflect the human perception of colors, with red having the highest weight, followed by green, and then blue.

The mahotas.colors.rgb2gray() Function

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

The mahotas.colors.rgb2gray() function takes an RGB image as input and returns the grayscale version of the image.

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

The resulting grayscale image retains the structure and overall content of the original RGB image but lacks color information.

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

Following is the basic syntax of the rgb2gray() function in mahotas −

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

其中,

where,

  1. rgb_image − It is the input image in RGB color space.

  2. dtype (optional) − It is the data type of the returned image (default is float).

在以下示例中,我们正使用 mh.colors.rgb2gray() 函数将 RGB 图像转换为灰度图像 −

In the following example, we are converting an RGB image to a grayscale image using the mh.colors.rgb2gray() function −

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()

以下是上面代码的输出: -

Following is the output of the above code −

rgb2gray color

Using Average of RGB Channels

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

We can also convert an RGB image to grayscale image using average of RGB channels.

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

The intensity of red, green, and blue color of each pixel is summed and divided by three to obtain the average intensity value. We can achieve this using the mean() function of the numpy library.

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

The resulting image will be a grayscale image having a single channel where each pixel represents the average intensity across the RGB channels, where each color channel contributes equally to the overall grayscale intensity.

Example

以下示例显示了使用 RGB 通道的平均值将 RGB 图像转换为灰度的过程 -

The following example shows conversion of an RGB image to grayscale using average of RGB channels −

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()

上述代码的输出如下:

Output of the above code is as follows −

average rgb channels

Using Luminosity

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

Luminosity is a term used to describe the perceived brightness or lightness of an object or image.

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

In image processing, luminosity assigns specific weights to the red, green, and blue color channels of each pixel and combines them to calculate the grayscale intensity value.

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

The luminosity in the RGB color space can be calculated using the following formula −

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

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

Where, the values 0.2989, 0.5870, and 0.1140 are the weights assigned to the red, green, and blue channels, respectively. These weights are derived from the standard Rec.709 color space used for digital displays.

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

This method produces better results compared to simply averaging the RGB channels, as it better captures the visual perception of the original image.

Example

在这里,我们定义了 RGB 的亮度以将彩色图像转换为其等效的灰度图像 -

Here, we have defined the luminosity of RGB to convert a colored image to its equivalent grayscale image −

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()

执行上面的代码后,我们得到以下输出: -

After executing the above code, we get the following output −

luminosity image