Mahotas - 軟閾值



軟閾值是指降低影像的噪聲(降噪)以提高其質量。

它根據畫素與閾值的接近程度,為畫素分配一個連續的值範圍。這導致前景和背景區域之間逐漸過渡。

在軟閾值中,閾值決定了降噪和影像儲存之間的平衡。較高的閾值會導致更強的降噪,但會導致資訊丟失。

相反,較低的閾值保留更多資訊,但會導致不需要的噪聲。

Mahotas中的軟閾值

在Mahotas中,我們可以使用thresholding.soft_threshold()函式對影像應用軟閾值。它根據相鄰畫素動態調整閾值,以增強具有非均勻噪聲水平的影像。

透過使用動態調整,該函式按比例降低那些強度超過閾值的畫素的強度,並將它們分配給前景。

另一方面,如果畫素的強度低於閾值,則將其分配給背景。

mahotas.thresholding.soft_threshold() 函式

mahotas.thresholding.soft_threshold() 函式接收灰度影像作為輸入,並返回已應用軟閾值的影像。它的工作原理是將畫素強度與提供的閾值進行比較。

語法

以下是Mahotas中soft_threshold()函式的基本語法:

mahotas.thresholding.soft_threshold(f, tval)

其中,

  • f - 輸入灰度影像。

  • tval - 閾值。

示例

在下面的示例中,我們使用mh.thresholding.soft_threshold()函式對灰度影像應用軟閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting threshold value
tval = 150
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Soft Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

以下是上述程式碼的輸出:

Soft Threshold Mahotas

使用均值進行軟閾值

我們可以使用影像上畫素強度的均值來應用軟閾值。均值是指影像的平均強度。

它是透過將所有畫素的強度值相加,然後除以畫素總數來計算的。

在Mahotas中,我們可以使用numpy.mean()函式找到影像所有畫素的平均畫素強度。然後,可以將均值傳遞給mahotas.thresholding.soft_threshold()函式的tval引數以生成軟閾值影像。

這種應用軟閾值的方法在降噪和影像質量之間保持了良好的平衡,因為閾值既不太高也不太低。

示例

下面的示例顯示了當閾值為畫素強度均值時,對灰度影像應用軟閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting mean threshold value
tval = np.mean(image)
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Soft Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Soft Threshold Mean Value

使用百分位數進行軟閾值

除了均值,我們還可以使用影像畫素強度的百分位數來應用軟閾值。百分位數是指低於該值的資料所佔的百分比;在影像處理中,它指的是影像中畫素強度的分佈。

例如,讓我們將閾值百分位數設定為85。這意味著只有強度大於影像中其他畫素的85%的畫素才會被分類為前景,而其餘畫素會被分類為背景。

在Mahotas中,我們可以使用numpy.percentile()函式根據畫素強度的百分位數設定閾值。然後,此值用於soft_thresholding()函式對影像應用軟閾值。

示例

在這個例子中,我們展示了當使用百分位數查詢閾值時如何應用軟閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting percentile threshold value
tval = np.percentile(image, 85)
# Applying soft threshold on the image
threshold_image = mh.thresholding.soft_threshold(image, tval)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the threshold image
axes[1].imshow(threshold_image, cmap='gray')
axes[1].set_title('Soft Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

執行上述程式碼後,我們將得到以下輸出:

Soft Threshold Percentile Value
廣告