Mahotas - 伽馬校正拉伸



伽馬校正拉伸是指增強影像的整體對比度。這是透過增加伽馬值來實現的,伽馬值增加會擴充套件影像畫素的強度級別範圍。

伽馬校正拉伸的過程涉及將原始輸入值拉伸到新的更寬的取值範圍。

在Mahotas中進行伽馬校正拉伸

在Mahotas中,我們可以使用mahotas.stretch()函式進行影像的伽馬校正拉伸。

在伽馬校正中,大於1的伽馬值會增加影像的對比度,而小於1的伽馬值會降低對比度。

因此,透過拉伸伽馬值,影像的暗區會變得更暗,亮區會變得更亮,從而在不同的色調和細節之間產生更明顯的區別。

使用mahotas.stretch()函式

mahotas.stretch()函式接收影像作為輸入,並返回影像的銳化版本作為輸出。生成的影像具有增強的對比度和改進的細節可見性。

stretch()函式確定影像中的最小和最大強度值,並將它們轉換為全範圍的畫素值(對於8點陣圖像為0-255)。

語法

以下是Mahotas中mh.stretch()函式的基本語法:

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

其中,

  • image - 輸入影像。

  • arg0 (可選) - 輸出的最小值(預設值為0)。

  • arg1 (可選) - 輸出的最大值(預設值為255)。

  • dtype (可選) - 輸出影像的資料型別(預設為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

RGB影像的伽馬校正拉伸

我們也可以使用stretch()函式在mahotas中對RGB影像執行伽馬校正拉伸。stretch函式中使用的伽馬值決定了對比度增強的程度。

然後,我們可以透過將其乘以255(RGB影像的最大強度)將其拉伸的影像轉換回RGB顏色空間。

示例

以下示例顯示了增加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

互動式伽馬校正拉伸滑塊

互動式伽馬校正拉伸滑塊是一個GUI元素,允許使用者透過拖動滑塊來動態調整伽馬值以更改對比度。

  • 要在Mahotas中使用互動式滑塊進行伽馬校正拉伸,首先建立一個用於調整伽馬值的滑塊。
  • 然後,實現一個函式,在移動滑塊時檢索新的伽馬值並將其應用於影像的伽馬校正拉伸。
  • 最後,將函式連線到滑塊的值更改事件,以便在移動滑塊時自動呼叫它。

語法

以下是建立互動式滑塊的基本語法:

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

其中,

  • slider_axis - 定義滑塊位置和尺寸的列表。

  • name - 滑塊的名稱。

  • mini_value - 滑塊可以到達的最小值。

  • max_value - 滑塊可以到達的最大值。

  • valint - 滑塊的起始值。

示例

在這個例子中,我們使用互動式滑塊來增加對比度:

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
廣告
© . All rights reserved.