Mahotas 简明教程

Mahotas - RGB to Sepia

焦茶色是指一种特殊的着色效果,可以让图片看起来古色古香和温暖。

当你看到一张焦茶色照片时,它看起来会带有红褐色调。这就像通过一种怀旧的滤镜观察图像,赋予它一种复古的感觉。

要将 RGB 图像转换为焦茶色,你需要转换每个像素的红色、绿色和蓝色通道以达到所需的焦茶色调。

RGB to Sepia Conversion in Mahotas

在 Mahotas 中,我们可以使用 colors.rgb2sepia() 函数将 RGB 图像转换为焦茶色图像。

为了理解转换,让我们从 RGB 颜色模型开始 −

  1. 在 RGB 中,图像由三种原色组成——红色、绿色和蓝色。

  2. 图像中的每个像素都对这三种颜色有一个值,它决定了它的整体颜色。例如,如果一个像素有较高的红色和较低的绿色和蓝色值,它会显示为一种红色阴影。

  3. 现在,要使用 mahotas 将 RGB 图像转换为焦茶色,我们遵循一个特定的公式。该公式涉及计算每个像素的红色、绿色和蓝色通道的新值。

  4. 这些新值将图像赋予一种温暖的褐色色调,从而创造出复古色效果。

RGB to Sepia Conversion Steps

以下是转换过程的简单说明:

  1. @ {s0} − 一幅 RGB 图像由三个颜色通道组成:红色、绿色和蓝色。图像中的每个像素都有这三个通道的强度值,范围从 0 到 255。

  2. @ {s1} − 要转换为复古色,我们首先需要计算每个像素的整体强度。这可以通过取红色、绿色和蓝色颜色通道的加权平均值来完成。平均值中使用的权重可以根据所需的复古色效果而不同。

  3. @ {s2} − 获得强度值后,我们可以应用一些特定的变换来获得复古色效果。这些变换涉及以模拟复古色调的方式调整红色、绿色和蓝色通道的水平。这可以通过提高红色通道的强度、降低蓝色通道的强度并保持绿色通道基本不变来完成。

  4. @ {s3} − 经过调整后,一些强度值可能会超出有效范围(对于 8 位图像,范围为 0 到 255)。为了确保值保持在此范围内,我们需要对其进行裁剪。低于 0 的值设置为 0,高于 255 的值设置为 255。

  5. @ {s4} − 最后,使用调整后的强度值来重建复古色图像。图像现在以所需的复古色调出现,使其呈现出一种复古外观。

Using the mahotas.colors.rgb2sepia() Function

mahotas.colors.rgb2sepia() 函数将 RGB 图像作为输入,并返回图像的复古色版本。

生成的复古色图像保留了原始 RGB 图像的总体结构和内容,但引入了一种温暖的褐色色调。

以下是 mahotas 中 rgb2sepia() 函数的基本语法:

mahotas.colors.rgb2sepia(rgb)

其中,@ {s5} 是 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

Using Transformation Factor

我们可以用于将 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

其中,@ {s6} 分别是红色、绿色和蓝色的转换因子。

将这些变换应用到 RGB 图像中的每个像素上,便会得到完整的复古色调图像。

Example

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