Mahotas 简明教程

Mahotas - Image Stretch RGB

图像拉伸是一种通过将原始像素值映射到新的值范围来增强图像对比度的技术。这通常是为了提高细节的可见性和增强图像的整体外观。

对于 RGB 图像,每个像素有三个颜色通道——红色、绿色和蓝色。拉伸 RGB 图像涉及独立拉伸每个颜色通道的值以扩展强度范围。

Image Stretch RGB in Mahotas

在 Mahotas 中,通过拉伸图像的单独颜色通道(红、绿和蓝)来执行 RGB 图像拉伸。拉伸过程涉及将原始像素强度映射到新的范围以增加对比度。

拉伸各个 RGB 通道后,需要将它们重新组合以创建最终的拉伸图像。

我们可以使用 stretch() 函数和 stretch_rgb() 函数在 mahotas 中执行图像拉伸 RGB。

Using the stretch() and the stretch_rgb() Functions

mahotas.stretch() 和 mahotas.stretch_rgb() 函数将 RGB 图像中每个通道中的像素值拉伸到指定的 min_value 和 max_value 之间。

值低于 min_value 的像素将设置为 min_value,值高于 max_value 的像素将设置为 max_value。拉伸在每个通道上独立执行。

以下是 mahotas.stretch() 函数的基本语法 −

mahotas.stretch(img,arg0=None, arg1=None, dtype=<type 'numpy.uint8'>)

以下是 mahotas.stretch_rgb() 函数的基本语法 −

mahotas.stretch(img,arg0=None, arg1=None, dtype=<type 'numpy.uint8'>)

以下参数传递给 mahotas.stretch() 函数 −

img − 它是输入图像。

arg0 and arg1 (optional) − 这些参数指定像素值应该拉伸的范围。这些参数的解释取决于它们的值−

  1. 如果提供了 arg0arg1 ,并且它们不是 None ,它们表示像素值应该拉伸到的最小值和最大值。低于 arg0 的像素值将设置为 arg0 ,高于 arg1 的像素值将设置为 arg1

  2. 如果仅提供了 arg0 ,并且 arg1Nonearg0 应该是一个表示百分比值的长度为 2 的元组或列表。在 arg0 的第一个元素的值以下的像素值将被设置为该值,在 arg0 的第二个元素的值以上的像素值将被设置为该值。

  3. 如果 arg0arg1 都是 None ,则像素值将被拉伸到由 dtype 参数指定的数据类型的整个范围(默认值为 numpy.uint8)。

dtype − 它指定输出图像的数据类型。默认值是 numpy.uint8,它表示无符号 8 位整数。

在以下示例中,我们正在使用 mahotas.stretch() 函数来拉伸图像−

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('sun.png')
# Stretch image
stretched = mh.stretch(image, arg0=100, arg1=280)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the stretched grayscale image
axes[1].imshow(stretched, cmap='gray')
axes[1].set_title('Stretched Grayscale Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

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

image stretch rgb

在此示例中,我们将看到如何使用 mahotas.stretch_rgb 函数拉伸图像−

import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('nature.jpeg')
# Stretch image
stretched = mh.stretch_rgb(image, arg0=100, arg1=280)
# Create a figure with subplots
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
# Display the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].axis('off')
# Display the stretched grayscale image
axes[1].imshow(stretched, cmap='gray')
axes[1].set_title('Stretched Grayscale Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()

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

image stretch rgb1

Stretching Image with Percentile Value

要在 Mahotas 中用百分比值拉伸 RGB 图像,我们可以手动计算百分比值并在每个颜色通道上单独使用拉伸函数−

  1. 首先,将 RGB 图像拆分为各个颜色通道(红色、绿色和蓝色)。

  2. 这可以通过使用 NumPy 库中的 np.split() 函数来实现。

  3. 一旦拆分图像,使用 NumPy 的 np.percentile() 函数计算每个颜色通道的所需百分比值。

  4. 在获得百分比值后,使用 Mahotas 的 stretch() 函数独立于每个通道,使用计算出的最小值和最大值。

  5. 这将在每个通道的所需百分比范围内拉伸像素值。

最后,通过沿着颜色通道轴连接拉伸通道,将拉伸通道合并回 RGB 图像中。结果图像将是基于指定百分比值的拉伸 RGB 图像。

Example

在这里,我们尝试用百分比值拉伸图像−

import mahotas as mh
import numpy as np
from pylab import imshow, show
image = mh.imread('nature.jpeg')
# Splitting the RGB image into individual channels
r, g, b = np.split(image, 3, axis=2)
# Calculating percentiles for each channel
r_min, r_max = np.percentile(r, [17, 90])
g_min, g_max = np.percentile(g, [25, 75])
b_min, b_max = np.percentile(b, [50, 99])
# Stretching each channel independently
stretched_r = mh.stretch(r, r_min, r_max)
stretched_g = mh.stretch(g, g_min, g_max)
stretched_b = mh.stretch(b, b_min, b_max)
# Merging the stretched channels back into an RGB image
stretched_image = np.concatenate((stretched_r, stretched_g, stretched_b),
axis=2)
imshow(stretched_image)
show()

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

image stretch rgb2