Mahotas - 影像區域性最小值



影像中的區域性最小值指的是在區域性鄰域內畫素強度最低的區域。區域性鄰域僅包含畫素的直接鄰居;因此,它在識別區域性最小值時僅使用影像的一部分。

一張影像可以包含多個區域性最小值,每個區域性最小值具有不同的強度。發生這種情況是因為一個區域的強度可能低於其相鄰區域,但它可能不是整個影像中強度最低的區域。

Mahotas 中的影像區域性最小值

在 Mahotas 中,我們可以使用 **mahotas.locmin()** 函式找到影像的區域性最小值。區域性最小值區域是使用區域最小值找到的,區域最小值指的是強度值低於影像中所有相鄰畫素的區域。

mahotas.locmin() 函式

“mahotas.locmin()”函式以影像作為輸入並找到區域性最小值區域。它返回一個二值影像,其中每個區域性最小值區域由 1 表示。該函式以以下方式工作以查詢影像中的區域性最小值:

  • 它首先對輸入影像應用形態學腐蝕以找到區域最小值點。

  • 接下來,它將腐蝕後的影像與原始影像進行比較。如果原始影像中的畫素強度較低,則該畫素表示區域最小值。

  • 最後,生成一個二值陣列,其中 1 對應於區域性最小值的存在,其他地方為 0。

語法

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

mahotas.locmin(f, Bc={3x3 cross}, out={np.empty(f.shape, bool)})

其中,

  • **f** - 它是要輸入的灰度影像。

  • **Bc(可選)** - 它是在用於連通性的結構元素。

  • **out(可選)** - 它是由布林資料型別組成的輸出陣列(預設為與 f 大小相同的新的陣列)。

示例

在以下示例中,我們使用 mh.locmin() 函式獲取影像的區域性最小值。

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)
# Getting the local minima
local_minima = mh.locmin(image)
# 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 local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Local Minima Image

使用自定義結構元素

我們可以使用自定義結構元素來獲取影像的區域性最小值。結構元素是一個奇數維度的二值陣列,由 1 和 0 組成,定義了鄰域畫素的連通性模式。

1 表示包含在連通性分析中的鄰域畫素,而 0 表示排除或忽略的鄰居。

在 mahotas 中,我們可以使用自定義結構元素來定義區域性最小值提取期間影像的鄰域畫素。它允許我們根據我們的要求找到區域性最小值區域。

要使用結構元素,我們需要將其傳遞給 locmin() 函式的 **Bc** 引數。

例如,讓我們考慮自定義結構元素:**[[1, 0, 0, 0, 1], [0, 1, 0, 1,0], [0, 0, 1, 0, 0], [0, 1, 0, 1, 1], [1, 0, 0, 1, 0]]**。此結構元素表示垂直、水平和對角線連通性。

這意味著對於影像中的每個畫素,在區域性最小值提取期間,只有垂直、水平或對角線位於其上方和下方的畫素被視為其鄰居。

示例

在下面的示例中,我們定義了一個自定義結構元素來獲取影像的區域性最小值。

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)
# Setting custom structuring element
struct_element = np.array([[1, 0, 0, 0, 1],
[0, 1, 0, 1, 0],
[0, 0, 1, 0, 0],
[0, 1, 0, 1, 1],
[1, 0, 0, 1, 0]])
# Getting the local minima
local_minima = mh.locmin(image, Bc=struct_element)
# 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 local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

上述程式碼的輸出如下:

Local Minima Image1

使用二值影像

我們還可以找到二值影像中的區域性最小值。二值影像是一幅影像,其中每個畫素由 1 或 0 表示,分別表示前景或背景。可以使用 numpy 庫中的 array() 函式建立二值影像。

在 mahotas 中,我們可以使用 locmin() 函式查詢二值影像的區域性最小值區域。由於二值影像僅包含 1 和 0,因此具有值 1 的畫素將被視為區域性最小值,因為前景畫素的強度小於背景畫素。

例如,假設從以下陣列建立了一個二值影像:**[[0, 1, 0, 0], [1, 0, 1, 0], [0, 1, 0, 0], [1, 0, 1, 0]]**。然後陣列中 1 的數量將決定區域性最小值區域的數量。因此,第一個陣列將有 1 個區域性最小值區域,第二個陣列將有 2 個區域性最小值區域,依此類推。

示例

在這裡,我們獲取二值影像中的區域性最小值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Creating a binary image
binary_image = np.array([[0, 1, 0, 0],
[1, 0, 1, 0],
[0, 1, 0, 0],
[1, 0, 1, 0]], dtype=np.uint8)
# Getting the local minima
local_minima = mh.locmin(binary_image)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the binary image
axes[0].imshow(binary_image, cmap='gray')
axes[0].set_title('Binary Image')
axes[0].set_axis_off()
# Displaying the local minima
axes[1].imshow(local_minima, cmap='gray')
axes[1].set_title('Local Minima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Binary Image Mahotas
廣告