Mahotas - 設定閾值



設定閾值是指為影像定義一個閾值,以執行影像閾值化。影像閾值化是將灰度影像轉換為二值影像的過程,其中畫素被分為兩類:前景或背景。

強度值高於閾值的畫素被分配到前景,而低於閾值的畫素被分配到背景類。

閾值範圍從 0 到 255,其中值為 0 產生僅包含前景(白色)的影像,值為 255 產生僅包含背景(黑色)的影像。

在 Mahotas 中設定閾值

在 Mahotas 中,我們可以使用 **numpy.mean()** 函式設定影像的閾值。

此函式將灰度影像作為輸入,並計算其畫素的平均強度值。

然後將平均值設定為閾值。任何強度超過閾值的畫素都被分類為前景,而強度低於閾值的畫素則被分類為背景。

**注意** - Mahotas 沒有提供直接設定閾值的方法,但是可以透過將 mahotas 與 numpy 結合使用來實現。

語法

以下是 numpy 中 mean() 函式的基本語法:

numpy.mean(image)

其中,

  • **image** - 輸入灰度影像。

示例

在下面的示例中,我們使用 np.mean() 函式設定灰度影像的閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Calculating mean intensity value
mean_value = np.mean(image)
# Creating threshold image
threshold_image = image > mean_value
# 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('Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Setting Threshold

設定反向閾值

我們還可以為影像設定反向閾值。在反向閾值中,強度大於閾值的畫素被分類為背景,而強度小於閾值的畫素被分類為前景。

在 mahotas 中,反向閾值化可以分兩步完成。第一步是使用 numpy.mean() 函式計算影像的閾值。

第二步是使用小於運算子 (<) 而不是正常閾值化中使用的大於運算子 (>),將影像的畫素強度與平均閾值進行比較。

示例

以下示例顯示瞭如何從灰度影像建立反向閾值影像。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Calculating mean intensity value
mean_value = np.mean(image)
# Creating inverse threshold image
threshold_image = image < mean_value
# 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('Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Inverse Threshold

設定自定義閾值

設定閾值的另一種方法是為影像設定自定義閾值。它是一個完全基於影像決定的隨機數。

自定義閾值是一個任意數字,不是使用任何數學公式計算的。這就是為什麼不應將自定義值用作閾值的原因。

另一個原因是,自定義值可能會產生噪聲明顯更大的影像。

在 mahotas 中,我們可以分配一個任意數字來設定自定義閾值。然後,我們可以使用此值並將影像的畫素與其進行比較以生成閾值影像。

**注意** - 將閾值設定為 0 或 255 將導致最終影像僅包含前景畫素或背景畫素。

示例

在這裡,我們設定了一個任意數字作為閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Setting threshold value
threshold_value = 200
# Creating threshold image
threshold_image = image > threshold_value
# 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('Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Custom Threshold Value
廣告