Mahotas - 影像區域最大值



區域最大值指的是影像中畫素強度值最高的點。在一幅影像中,構成區域最大值的區域是所有其他區域中最亮的。區域最大值也稱為全域性最大值。

區域最大值考慮整幅影像,而區域性最大值只考慮區域性鄰域,以查詢強度值最高的畫素。

區域最大值是區域性最大值的一個子集,因此所有區域最大值都是區域性最大值,但並非所有區域性最大值都是區域最大值。

一幅影像可以包含多個區域最大值,但所有區域最大值的強度都相同。這是因為區域最大值只考慮最高的強度值。

Mahotas中的影像區域最大值

在Mahotas中,我們可以使用mahotas.regmax()函式查詢影像中的區域最大值。區域最大值透過影像中的強度峰值來識別,因為它們代表高強度區域。

區域最大值點突出顯示為白色,而其他點則為黑色。

mahotas.regmax()函式

mahotas.regmax()函式從輸入的灰度影像中提取區域最大值。

它輸出一個影像,其中1代表區域最大值點的存在,0代表普通點。

regmax()函式使用基於形態學重建的方法來查詢區域最大值。在這種方法中,每個區域性最大值區域的強度值與其鄰居進行比較。

如果發現鄰居具有更高的強度,則它成為新的區域最大值。此過程持續進行,直到沒有更高強度區域剩餘,表明已達到區域最大值。

語法

以下是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('tree.tiff')
# 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 regional 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()

輸出

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

Regional Maxima Image Mahotas

使用自定義結構元素

我們還可以使用自定義結構元素來從影像中獲取區域最大值。結構元素是一個奇數維度的二元陣列,由1和0組成,它定義了影像標記期間鄰域畫素的連線模式。

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

在Mahotas中,提取區域最大值區域時,我們可以使用自定義結構元素來定義相鄰畫素的連線性。我們首先使用numpy.array()函式建立一個奇數維度的結構元素。

然後,我們將此自定義結構元素輸入到regmax()函式的Bc引數中。

例如,讓我們考慮自定義結構元素:[[0, 0, 0, 0, 1], [0, 0, 1, 0, 0], [1, 0, 0, 0, 0], [0, 0, 0, 1, 0], [0, 1, 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)
# Setting custom structuring element
struct_element = np.array([[0, 0, 0, 0, 1],
[0, 0, 1, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 1, 0],
[0, 1, 0, 0, 0]])
# Getting the regional maxima
regional_maxima = mh.regmax(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 regional 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()

輸出

上述程式碼的輸出如下:

Custom Structuring Element

使用影像的特定區域

我們還可以找到影像特定區域的區域最大值。影像的特定區域是指較大影像的一小部分。可以透過裁剪原始影像來去除不需要的區域,從而提取特定區域。

在Mahotas中,我們可以在影像的一部分中找到區域最大值。首先,我們透過指定x軸和y軸所需的尺寸來裁剪原始影像。然後我們使用裁剪後的影像並使用regmax()函式獲取區域最大值。

例如,假設我們將[:800, 70:]指定為x軸和y軸的尺寸。然後,裁剪後的影像的x軸範圍為0到800畫素,y軸範圍為70到最大尺寸。

示例

在這個例子中,我們正在獲取影像特定區域內的區域最大值。

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)
# Using specific region of the image
image = image[:800, 70:]
# 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 regional 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()

輸出

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

Specific Region Image
廣告