Mahotas - 澤尼克矩



與澤尼克特徵類似,澤尼克矩也是一組描述影像中物體形狀的數學值。它們提供了關於形狀的具體細節,例如形狀的圓度或對稱性,或者是否存在任何特定的模式。

澤尼克矩具有一些特殊的屬性,如下所述:

  • 尺寸不變性 - 無論其大小如何,它們都可以描述形狀。因此,即使您擁有小型或大型物體,澤尼克矩仍然可以準確地捕捉其形狀。

  • 旋轉不變性 - 如果您旋轉影像中的物體,澤尼克矩將保持不變。

  • 縮放不變性 - 如果您調整影像中物體的尺寸,澤尼克矩將保持不變。

澤尼克矩使用稱為澤尼克多項式的特殊數學函式將形狀分解成更小的部分。

這些多項式充當構建塊。透過組合不同的澤尼克多項式,我們可以重現並表示物體形狀的獨特特徵。

Mahotas中的澤尼克矩

為了在mahotas中計算澤尼克矩,我們可以使用mahotas.features.zernike_moments()函式。

在 Mahotas 中,澤尼克矩是透過生成一組澤尼克多項式來計算的,這些多項式是表示各種形狀和輪廓的特殊數學函式。

這些多項式充當分析物體形狀的構建塊。

之後,透過將物體的形狀投影到澤尼克多項式上來計算澤尼克矩。這些矩捕捉重要的形狀特徵。

mahotas.features.zernike_moments() 函式

mahotas.features.zernike_moments() 函式接受兩個引數:影像物件和澤尼克多項式的最大半徑。該函式返回影像的澤尼克矩的一維陣列。

語法

以下是 mahotas.features.zernike_moments() 函式的基本語法:

mahotas.features.zernike_moments(im, radius, degree=8, cm={center_of_mass(im)})

其中,

  • im - 這是將對其計算澤尼克矩的輸入影像。

  • radius - 它定義了圓形區域的半徑(以畫素為單位),在此半徑上將計算澤尼克矩。由此半徑定義的圓形區域(以質心為中心)之外的區域將被忽略。

  • degree(可選) - 它指定要計算的澤尼克矩的最大數量。預設情況下,degree 值為 8。

  • cm(可選) - 它指定影像的質心。預設情況下,使用影像的質心。

示例

以下是使用預設 degree 值計算影像澤尼克矩的基本示例:

import mahotas as mh
# Load images of shapes
image = mh.imread('sun.png', as_grey=True)
# Compute Zernike moments
moments = mh.features.zernike_moments(image, radius=10)
# Compare the moments for shape recognition
print(moments)
輸出

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

[0.31830989 0.00534998 0.00281258 0.0057374  0.01057919 0.00429721
 0.00178094 0.00918145 0.02209622 0.01597089 0.00729495 0.00831211
 0.00364554 0.01171028 0.02789188 0.01186194 0.02081316 0.01146935
 0.01319499 0.03367388 0.01580632 0.01314671 0.02947629 0.01304526
 0.00600012]

使用自定義質心

影像的質心是影像中質量均勻分佈的點。自定義質心是影像中不一定是影像質心的點。

這在您想要使用不同的質心進行計算的情況下非常有用。

例如,您可能想要使用影像中物體的自定義質心來計算該物體的澤尼克矩。

為了在 mahotas 中使用自定義質心計算影像的澤尼克矩,我們需要將cm引數傳遞給 mahotas.features.zernike_moments() 函式。

cm 引數接受一個包含兩個數字的元組,它們表示自定義質心的座標。

示例

在這裡,我們嘗試使用自定義質心計算影像的澤尼克矩:

import mahotas
import numpy as np
# Load the image
image = mahotas.imread('nature.jpeg', as_grey = True)
# Calculate the center of mass of the image
center_of_mass = np.array([100, 100])
# Calculate the Zernike moments of the image, using the custom center of mass
zernike_moments = mahotas.features.zernike_moments(image, radius = 5,
cm=center_of_mass)
# Print the Zernike moments
print(zernike_moments)

輸出

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

[3.18309886e-01 3.55572603e-04 3.73132619e-02 5.98944983e-04
 3.23622041e-04 1.72293481e-04 9.16757235e-02 3.35704966e-04
 7.09426259e-02 1.17847972e-04 2.12625026e-04 3.06537827e-04
 1.94379185e-01 1.32093249e-04 8.54616882e-02 1.83274207e-04
 1.86728282e-04 3.08004108e-04 4.79437809e-04 1.97726337e-04
 3.61630733e-01 5.27467687e-04 8.25534856e-02 7.75593823e-06
 1.99419391e-01]

使用特定階數

澤尼克矩的階數是其可以表示的形狀複雜程度的度量。階數越高,矩可以表示的形狀越複雜。

為了在 mahotas 中使用特定階數計算影像的澤尼克矩,我們需要將degree引數傳遞給 mahotas.features.zernike_moments() 函式。

示例

在下面的示例中,我們嘗試使用指定的階數計算影像的澤尼克矩。

import mahotas
import numpy as np
# Load the image
image = mahotas.imread('nature.jpeg', as_grey = True)
# Calculate the Zernike moments of the image, using the specific order
zernike_moments = mahotas.features.zernike_moments(image,1, 4)
# Print the Zernike moments
print(zernike_moments)

輸出

上述程式碼的輸出如下所示:

[0.31830989 0.17086131 0.03146824 0.1549947 0.30067136 0.5376049 0.30532715 0.33032683 0.47908119]
廣告
© . All rights reserved.