Mahotas 简明教程

Mahotas - Stretching Gamma Correction

伽马校正拉伸指的是增强图像的整体对比度。这可以通过增加伽马值来完成,这会扩展图像像素的强度等级范围。

伽马校正拉伸过程涉及将原始输入值拉伸到一个新的更宽的取值范围。

Stretching Gamma Correction in Mahotas

在 Mahotas 中,我们可以使用 mahotas.stretch() 函数实现图像的拉伸伽马校正。

在伽马校正中,大于 1 的伽马值会增加图像的对比度,而小于 1 的伽马值会降低对比度。

因此,通过拉伸伽马,图像的暗区会变得更暗,亮区会变得更亮,从而导致不同阴影和细节得到更显著的区别。

Using the mahotas.stretch() Function

mahotas.stretch() 函数以图像作为输入,并返回锐化后的图像作为输出。生成的图像具有增强的对比度和改善的细节可见性。

stretch() 函数会确定图像中的最小和最大强度值,并将它们转换为像素值的全范围(对于 8 位图像而言为 0-255)。

以下是 mahotas 中 mh.stretch() 函数的基本语法:

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

其中,

  1. image - 它是输入图像。

  2. arg0 (optional) - 它是输出的最小值(默认值为 0)。

  3. arg1 (optional) - 它是输出的最大值(默认值为 255)。

  4. dtype (optional) - 它是输出图像的数据类型(默认值为 uint8)。

在以下示例中,我们使用 mh.stretch() 函数增加了灰度图像的对比度:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting to grayscale
gray_image = mh.colors.rgb2gray(image)
# Decreasing gamma value
corrected_gamma = 3.2
# Applying stretch gamma correction image
stretch_gamma_corrected = mh.stretch(gray_image, corrected_gamma)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(gray_image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying Stretched gamma corrected image
axes[1].imshow(stretch_gamma_corrected, cmap='gray')
axes[1].set_title('Stretched Gamma Corrected Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

stretching gamma correction

Stretching Gamma Correction of RGB Image

我们还可以使用 mahotas 中的 stretch() 函数为 RGB 图像执行拉伸伽马校正。stretch 函数中使用的伽马值决定了对比度增强的程度。

然后,我们可以通过乘以 255(RGB 图像的最大强度)将拉伸图像转换回 RGB 色彩空间。

Example

以下示例展示了如何增加 RGB 图像的对比度:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Applying stretched gamma correction
stretched_gamma_corrected = mh.stretch(image, 3)
# Converting the image back to RGB
stretched_gamma_corrected = stretched_gamma_corrected * 255
# Creating subplots to display images
fig, axes = mtplt.subplots(1, 2)
# Displaying the original RGB image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the stretched image
axes[1].imshow(stretched_gamma_corrected)
axes[1].set_title('Stretched Gamma Corrected Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

stretching gamma rgb image

Interactive Stretching Gamma Correction Slider

交互式拉伸伽马校正滑块是一个 GUI 元素,它允许用户通过拖动滑块来调整伽马值以动态更改对比度。

  1. 要在 Mahotas 中使用交互式滑块拉伸伽马校正,首先为调整伽马值创建一个滑块。

  2. 然后,实现一个函数,当滑块移动时检索新的伽马值,并将拉伸伽马校正应用到图像。

  3. 最后,将函数连接到滑块的值更改事件,以便在滑块移动时自动调用它。

Syntax

以下是创建交互式滑块的基本语法:

from matplotlib.widgets import Slider
Slider(slider_axis, name, min_value, max_value, valint)

其中,

  1. slider_axis − 是一个定义滑块位置和大小的列表。

  2. name − 是滑块的名称。

  3. mini_value − 是滑块能到达的最小值。

  4. max_value − 是滑块能到达的最大值。

  5. valint − 是滑块的开始值。

Example

在示例中,我们正使用交互式滑块来增加对比度 −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
from matplotlib.widgets import Slider
# Loading the image
image = mh.imread('tree.tiff')
# Creating a figure and axes for the plot
fig, axis = mtplt.subplots()
# Displaying the original image
axis.imshow(image)
axis.set_title('Stretching Gamma Correction')
axis.set_axis_off()
# Creating a slider for stretched gamma adjustment
slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03])
stretched_gamma_slider = Slider(slider_axis, 'Stretched Gamma', 0.1, 5.0,
valinit=1.0)
# Updating the stretched gamma correction and plot on change of slider value
def update_stretched_gamma(val):
   stretched_gamma = stretched_gamma_slider.val
   corrected_image = mh.stretch(image, stretched_gamma)
   corrected_image = corrected_image * 255
   axis.imshow(corrected_image)
   fig.canvas.draw_idle()
stretched_gamma_slider.on_changed(update_stretched_gamma)
# Showing the figure
mtplt.show()

输出结果如下:

stretching gamma slider