Mahotas - 擊中與錯過變換



擊中與錯過變換是一種二進位制形態學運算,用於檢測影像中的特定模式或形狀。

該運算將結構元素與輸入二值影像進行比較。結構元素由以特定模式排列的前景 (1) 和背景 (0) 畫素組成,這些畫素代表要檢測的所需形狀或模式。

擊中或錯過變換在結構元素和影像之間執行逐畫素邏輯與運算,然後檢查結果是否與預定義條件匹配。

該條件指定匹配模式中應該存在的特定前景和背景畫素排列。如果滿足該條件,則輸出畫素設定為 1,表示匹配;否則,設定為 0。

Mahotas 中的擊中與錯過變換

在 Mahotas 中,我們可以使用mahotas.hitmiss()函式對影像執行擊中與錯過變換。該函式使用結構元素'Bc'來確定輸入影像中是否存在特定模式。

Mahotas 中的結構元素可以取三個值:0、1 或 2。值為 1 表示結構元素的前景,而 0 表示背景。

值 2 用作“不關心”值,這意味著不應對該特定畫素執行匹配。

為了識別匹配,結構元素的值必須與輸入影像中相應的畫素值重疊。

如果重疊滿足結構元素指定的條件,則該畫素被視為匹配。

mahotas.hitmiss() 函式

mahotas.hitmiss() 以灰度影像作為輸入,並返回二值影像作為輸出。白色畫素表示結構元素與輸入影像匹配的區域,而黑色畫素表示不匹配的區域。

語法

以下是 mahotas 中 hitmiss() 函式的基本語法:

mahotas.hitmiss(input, Bc, out=np.zeros_like(input))

其中,

  • input - 輸入灰度影像。

  • Bc - 需要在輸入影像中匹配的模式。它可以是 0、1 或 2。

  • out(可選) - 定義在哪個陣列中儲存輸出影像(預設為與輸入相同大小)。

示例

以下示例演示了使用 mh.hitmiss() 函式對影像進行擊中與錯過變換。

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)
# Applying thresholding
threshold_image = mh.thresholding.bernsen(image, 5, 200)
# Creating hit & miss template
template = np.array([[1, 2, 1, 2, 1],[2, 1, 1, 1, 2],[2, 2, 1, 2, 2]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# 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 hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

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

Hit Miss Transform

透過檢測邊緣

我們還可以透過應用擊中與錯過變換來檢測影像的邊緣。邊緣表示影像中不同區域之間的邊界。

這些是相鄰畫素之間強度值差異較大的區域。

在 mahotas 中,要使用擊中與錯過變換檢測邊緣,我們首先建立一個結構元素。此結構元素將模板的邊緣與輸入影像進行匹配。

然後,我們對影像執行閾值化,然後將結構元素作為Bc引數傳遞給 hitmiss() 函式。

例如,以下結構元素可用於檢測輸入影像中的邊緣:

[[1, 2, 1]
 [2, 2, 2]
 [1, 2, 1]]

在此,1 位於結構元素的右上角、左上角、右下角和左下角位置。邊緣通常位於影像中的這些位置。

結構元素中的 1 與影像中強度值為 1 的畫素匹配,從而突出顯示邊緣作為前景。

示例

在此示例中,我們嘗試透過應用擊中與錯過變換來檢測影像的邊緣:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Applying thresholding
threshold_value = mh.thresholding.rc(image)
threshold_image = image > threshold_value
# Creating hit & miss template
template = np.array([[1, 2, 1],[2, 2, 2],[1, 2, 1]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# 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 hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Detecting Edges

透過檢測對角線

我們也可以使用擊中與錯過變換來檢測影像的對角線。對角線由連線影像相對角的線性圖案指示。

這些是畫素強度沿對角線路徑變化的區域。

在 mahotas 中,我們首先對輸入影像執行閾值化。然後,我們將結構元素作為Bc引數傳遞給 hitmiss() 函式。此結構元素將模板的對角線與輸入影像的對角線進行匹配。

例如,以下結構元素可用於檢測輸入影像中的對角線:

[[0, 2, 0]
 [2, 0, 2]
 [0, 2, 0]]

在此,0 沿從左上角到右下角的對角線路徑執行,以及從右上角到左下角的對角線路徑執行。對角線通常位於影像中的這些位置。

結構元素中的 0 與影像中強度值為 0 的畫素匹配,從而突出顯示對角線作為背景。

示例

在這裡,我們嘗試使用擊中與錯過變換來檢測影像的對角線:

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)
# Applying thresholding
threshold_image = mh.thresholding.bernsen(image, 10, 10)
# Creating hit & miss template
template = np.array([[0, 2, 0],[2, 0, 2],[0, 2, 0]])
# Applying hit & miss transformation
hit_miss = mh.hitmiss(threshold_image, template)
# 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 hit & miss transformed image
axes[1].imshow(hit_miss, cmap='gray')
axes[1].set_title('Hit & Miss Transformed Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Detecting Diagonals
廣告