Mahotas 简明教程

Mahotas - Stretching Gamma Correction

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

Stretching gamma correction refers to enhancing the overall contrast of an image. This is done by increasing the gamma value, which expands the range of intensity levels of the pixels of the image.

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

The process of stretching gamma correction involves stretching the original input values to a new broader range of values.

Stretching Gamma Correction in Mahotas

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

In Mahotas, we can do stretching gamma correction of an image by using the mahotas.stretch() function.

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

In gamma correction, a gamma value greater than 1 increases the contrast of the image, while a gamma value less than 1 decreases the contrast.

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

Hence, by stretching the gamma, dark areas of the image become darker, and bright areas become brighter, resulting in a more pronounced distinction between different shades and details.

Using the mahotas.stretch() Function

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

The mahotas.stretch() function takes an image as input and returns a sharpened version of the image as output. The resulting image has enhanced contrast and improved visibility of details.

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

The stretch() function determines the minimum and maximum intensity values in the image and transforms them to full range of pixel values (0−255 for 8−bit images).

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

Following is the basic syntax for mh.stretch() function in mahotas −

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

其中,

where,

  1. image − It is the input image.

  2. arg0 (optional) − It is the minimum value for output (default is 0).

  3. arg1 (optional) − It is the maximum value for output (default is 255).

  4. dtype (optional) − It is the data type of output image (default is uint8).

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

In the following example, we are increasing the contrast of a grayscale image by using mh.stretch() function −

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

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

Following is the output of the above code −

stretching gamma correction

Stretching Gamma Correction of RGB Image

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

We can also perform stretching gamma correction for an RGB image in mahotas using the stretch() function. The gamma value used in the stretch function determines the extent of contrast enhancement.

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

We can then convert the stretched image back to the RGB color space, by multiplying it by 255 (maximum intensity of an RGB image).

Example

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

The following example shows increasing of the contrast of an RGB image −

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

上述代码的输出如下:

Output of the above code is as follows −

stretching gamma rgb image

Interactive Stretching Gamma Correction Slider

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

An interactive stretching gamma correction slider is a GUI element that allows users to adjust the gamma value to change the contrast dynamically by dragging the slider.

  1. To stretch gamma correction using an interactive slider in Mahotas, first create a slider for adjusting the gamma value.

  2. Then, implement a function to retrieve the new gamma value when the slider is moved and apply stretching gamma correction to the image.

  3. Finally, connect the function to the slider’s value change event, so it is called automatically when the slider is moved.

Syntax

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

Following is the basic syntax to create an interactive slider −

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

其中,

where,

  1. slider_axis − It is a list that defines the position and dimensions of the slider.

  2. name − It is the name of the slider.

  3. mini_value − It is the minimum value that the slider can go to.

  4. max_value − It is the maximum value that the slider can go to.

  5. valint − It is the starting value of the slider.

Example

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

In this example we are increasing the contrast using an interactive slider −

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

输出结果如下:

The output produced is as follows −

stretching gamma slider