Mahotas 简明教程

Mahotas - Increase Gamma Correction

在我们了解增加伽玛校正之前,让我们首先了解伽玛校正是什么。

Let us first learn about what is gamma correction before understanding about increase gamma correction.

伽玛校正调整图像的亮度,以匹配我们的眼睛如何感知光线。我们的眼睛不会线性地看到光线,因此如果不进行校正,图像可能显得太暗或太亮。

Gamma correction adjusts the brightness of images to match how our eyes perceive light. Our eyes don’t see light in a linear way, so without correction, images may appear too dark or bright.

伽玛校正将数学变换应用于亮度值,通过调整其亮度级别使图像看起来更加自然。

Gamma correction applies a mathematical transformation to the brightness values, making the image look more natural by adjusting its brightness levels.

现在, increasing gamma correction 指的是调整伽玛值以使整体图像更亮。增加伽玛值时,暗区会变得更亮并增强图像的整体对比度。

Now, increasing gamma correction refers to adjusting the gamma value to make the overall image brighter. When the gamma value is increased, the dark areas appear brighter and enhance the overall contrast of the image.

Increasing Gamma Correction in Mahotas

在 Mahotas 中,增加伽玛校正指的是在改变像素亮度时调整伽玛值。

In Mahotas, increasing gamma correction refers to adjusting the gamma value when changing the brightness of the pixels.

伽玛是一个正值,其中:

The gamma is a positive value, where −

  1. A gamma value less than 1 will brighten the image.

  2. A gamma value greater than 1 will darken the image.

  3. A gamma value of 1 represents no correction and indicates a linear relationship between the pixel values and luminance.

Mahotas 中的伽马校正是指对图像的强度值应用幂律变换。幂律变换的定义如下:

Gamma correction in Mahotas involves applying a power−law transformation to the intensity values of the image. The power−law transformation is defined as follows −

new_intensity = old_intensity^gamma

在此,

Here,

  1. old_intensity − It is the original intensity value of a pixel

  2. new_intensity − It is the transformed intensity value after gamma correction.

  3. The gamma determines the degree of correction applied to the image.

在 Mahotas 中增加伽马值,即表示强度值提升到了更高的幂次方。此调整会影响图像的整体亮度。

When gamma is increased in Mahotas, it means that the intensity values are raised to a higher power. This adjustment affects the overall brightness of the image.

Example

在以下示例中,我们将通过减小伽马值来加深灰度图像:

In the following example, we are darkening a grayscale image by decreasing the gamma value −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to grayscale
gray_image = mh.colors.rgb2gray(image)
# Decreasing gamma value
corrected_gamma = 1.5
# Updating the image to use the corrected gamma value
gamma_correction = np.power(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 the gamma corrected image
axes[1].imshow(gamma_correction, cmap='gray')
axes[1].set_title('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 −

gamma correction

Using Interactive Gamma Correction Slider

交互式伽马校正滑块是一种允许用户动态调整伽马值的 GUI 元素。用户可以通过拖动滑块来增加或减小伽马值,从而对显示提供实时反馈。

An interactive gamma correction slider is a GUI element that allows users to adjust the gamma value dynamically. Users can increase or decrease the gamma value by dragging the slider, providing real−time feedback on the display.

我们可以通过先确定所需的伽马值来使用 mahotas 中的交互式伽马校正滑块增加伽马校正,从而增加校正。

We can increase gamma correction using interactive gamma correction slider in mahotas, by first determining the desired gamma value to increase the correction.

然后,通过将像素值提升到伽马值的逆幂次方来对图像应用幂律变换。

Then, apply the power−law transformation to the image by raising the pixel values to the power of the inverse gamma value.

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

在此,我们尝试使用交互式伽马校正滑块增加伽马校正:

Here, we are trying to increase gamma correction using interactive gamma correction 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')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Creating a figure and axes for the plot
fig, axis = mtplt.subplots()
# Displaying the original image
axis.imshow(image, cmap='gray')
axis.set_title('Gamma Correction')
axis.set_axis_off()
# Creating a slider for gamma adjustment
slider_axis = mtplt.axes([0.2, 0.05, 0.6, 0.03])
gamma_slider = Slider(slider_axis, 'Gamma', 0.1, 5.0, valinit=1.0)
# Updating the gamma correction and plot on change of slider value
def update_gamma(val):
   gamma = gamma_slider.val
   corrected_image = np.power(image, gamma)
   axis.imshow(corrected_image, cmap='gray')
   fig.canvas.draw_idle()
gamma_slider.on_changed(update_gamma)
# Showing the figure
mtplt.show()

以上代码的输出如下。我们首先尝试使用滑块增加伽马校正,如下所示:

Output of the above code is as follows. First we are trying to increase the gamma correction using the slider as shown below −

gamma correction slider

现在,使用滑块减小伽马校正:

Now, decreasing gamma correction using the slider −

gamma correction slider1

Using Batch Gamma correction

批处理伽马校正将多个伽马值应用于单一图像。这有助于比较原始图像在不同伽马值下的并排情况,以查看增加伽马校正的影响。

The batch gamma correction applies multiple gamma values to a single image. This helps in comparing the original image side−by−side at different gamma values to see the impact of increasing gamma correction.

在 Mahotas 中,我们可以通过先遍历已预定伽马值列表,使用批处理伽马校正来调整图像的亮度。然后对输入图像应用具有不同伽马值的幂律变换。

In Mahotas, we can adjust the brightness of an image using batch gamma correction by first iterating over a list of predetermined gamma values. Then applying the power−law transformation on the input image with different gamma values.

Example

现在,我们尝试使用批处理伽马校正增加伽马值:

Now, we are trying to increase gamma value using batch gamma correction −

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
from matplotlib.widgets import Slider
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Defining a list of gamma values
gamma_values = [0.21, 0.82, 2, 5]
# Creating subplots to display images for each gamma value
fig, axes = mtplt.subplots(1, len(gamma_values) + 1)
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Applying gamma correction for each gamma value
for i, gamma in enumerate(gamma_values):
   corrected_image = np.power(image, gamma)
   axes[i + 1].imshow(corrected_image, cmap='gray')
   axes[i + 1].set_title(f'Gamma={gamma}')
   axes[i + 1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Following is the output of the above code −

batch gamma correction