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
广告