Mahotas - 突出影像最大值



突出影像最大值是指顯示影像中最亮區域。影像最大值,也稱為區域最大值,是指在影像所有其他區域中具有最高畫素強度值的區域。

影像最大值在搜尋最亮區域時會考慮整幅影像。影像可以有多個區域最大值,但它們都具有相同的亮度級別。這是因為只有最亮的值被認為是影像最大值。

在 Mahotas 中突出影像最大值

在 Mahotas 中,我們可以使用 mahotas.regmax() 函式來突出影像中的最大值。影像最大值代表高強度區域;因此,它們是透過檢視影像的強度峰值來識別的。以下是該函式突出影像最大值的基本方法:

  • 首先,它將每個區域性最大值區域的強度值與其相鄰畫素進行比較。

  • 如果找到更亮的相鄰畫素,則該函式將其設定為新的影像最大值。

此過程持續進行,直到所有區域都已與影像最大值進行比較。

mahotas.regmax() 函式

mahotas.regmax() 函式接受灰度影像作為輸入。它返回一個影像,其中 1 代表影像最大值點,而 0 代表普通點。

語法

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

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

其中,

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

  • Bc (可選) - 它是用於連線的結構元素。

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

示例

在下面的示例中,我們使用 mh.regmax() 函式突出影像最大值。

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 regional maxima
regional_maxima = mh.regmax(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 highlighted image maxima
axes[1].imshow(regional_maxima, cmap='gray')
axes[1].set_title('Regional Maxima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

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

Image Maxima

使用自定義結構元素突出最大值

我們還可以使用自定義結構元素來突出影像最大值。結構元素是一個僅包含 1 和 0 的陣列。它定義了鄰域畫素的連線模式。

值為 1 的畫素包含在連線分析中,而值為 0 的畫素被排除在外。

在 Mahotas 中,我們使用 mh.disk() 函式建立一個自定義結構元素。然後,我們將此自定義結構元素設定為 regmax() 函式中的 Bc 引數以突出影像最大值。

示例

在這個例子中,我們使用自定義結構元素來突出影像最大值。

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)
# Creating a custom structuring element
se = np.array([[0, 1, 0],[1, 1, 1],[0, 1, 0]])
# Getting the regional maxima
regional_maxima = mh.regmax(image, Bc=se)
# 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 highlighted image maxima
axes[1].imshow(regional_maxima, cmap='gray')
axes[1].set_title('Regional Maxima')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Image Maxima1
廣告