Mahotas - Haralick 特徵



Haralick 特徵描述了影像的紋理。紋理指的是影像中賦予其特定外觀的模式,例如表面的光滑度或物體的排列方式。

要處理 Haralick 特徵,我們使用一個稱為灰度共生矩陣 (GLCM) 的特殊矩陣。它是一個表示影像中畫素強度對之間關係的矩陣。

它提供了關於影像中特定距離內畫素強度值的不同組合出現的頻率資訊。

Mahotas 中的 Haralick 特徵

要使用 Mahotas 計算 Haralick 特徵,請透過指定畫素對的距離和方向來建立 GLCM。接下來,使用 GLCM 計算構成 Haralick 特徵的各種統計量度。

這些度量包括對比度、相關性、能量、熵、同質性和更多。最後,檢索計算出的 Haralick 特徵。

例如,Haralick 紋理分析中的對比度特徵告訴我們影像中相鄰畫素的亮度或暗度變化程度。為了計算此特徵,我們分析 GLCM 矩陣。

此矩陣顯示具有不同亮度級別的畫素對x有多頻繁地一起出現以及它們在影像中的位置。

我們可以使用 **mahotas.features.haralick()** 函式在 mahotas 中計算 Haralick 特徵。

mahotas.features.haralick() 函式

haralick() 函式以灰度影像作為輸入,並返回計算出的 Haralick 特徵。Haralick 特徵是根據灰度影像計算的。

Mahotas 允許我們透過分析影像的 GLCM 來計算 Haralick 特徵。這樣,我們可以提取有關影像中存在的紋理模式的資訊。

語法

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

mahotas.features.haralick(f, ignore_zeros=False, preserve_haralick_bug=False,
compute_14th_feature=False, return_mean=False, return_mean_ptp=False,
use_x_minus_y_variance=False, distance=1)

引數

以下是 mahotas 中 haralick() 函式接受的引數:

  • **f** - 它是輸入影像。

  • **ignore_zeros(可選)** - 它計算在計算 Haralick 特徵時是否應忽略(True)或考慮(False)輸入矩陣中的零值。

  • **preserve_haralick_bug(可選)** - 它確定是否複製 Haralick 方程中的拼寫錯誤。

  • **compute_14th_feature(可選)** - 它指示是否計算第 14 個 Haralick 特徵(差異性)。預設情況下,它設定為 False。

  • **use_x_minus_y_variance(可選)** - 預設情況下,mahotas 使用 VAR[P(|x−y|)],但如果此引數為 True,則使用 VAR[|x−y|]。

  • **distance(可選)** - 它表示計算 GLCM 時使用的畫素距離。它確定在分析畫素之間空間關係時考慮的鄰域大小。預設情況下,它設定為 1。

  • **return_mean** - 當設定為 True 時,函式返回所有方向上的平均值。

  • **return_mean_ptp** - 當設定為 True 時,函式返回所有方向上的平均值和點到點 (ptp) 值(max() 和 min() 之間的差值)。

示例

以下是計算 mahotas 中 Haralick 特徵的基本示例:

import mahotas
import numpy as np
import matplotlib.pyplot as mtplt
# Load a grayscale image
image = mahotas.imread('nature.jpeg', as_grey=True).astype(np.uint8)
# Compute Haralick texture features
features = mahotas.features.haralick(image)
print(features)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the haralick featured image
axes[1].imshow(features, cmap='gray')
axes[1].set_title('Haralick Feature')
axes[1].axis('off')
mtplt.show()
輸出

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

[[ 2.77611344e-03  2.12394600e+02  9.75234595e-01  4.28813094e+03
   4.35886838e-01  2.69140151e+02  1.69401291e+04  8.31764345e+00
   1.14305862e+01  6.40277627e-04  4.00793348e+00 -4.61407168e-01
   9.99473205e-01]
 [ 1.61617121e-03  3.54272691e+02  9.58677001e-01  4.28662846e+03
   3.50998369e-01  2.69132899e+02  1.67922411e+04  8.38274113e+00
   1.20062562e+01  4.34549344e-04  4.47398649e+00 -3.83903098e-01
   9.98332575e-01]
 [ 1.92630414e-03  2.30755916e+02  9.73079650e-01  4.28590105e+03
   3.83777866e-01  2.69170823e+02  1.69128483e+04  8.37735303e+00
   1.17467122e+01  5.06580792e-04  4.20197981e+00 -4.18866103e-01
   9.99008620e-01]
 [ 1.61214638e-03  3.78211585e+02  9.55884630e-01  4.28661922e+03
   3.49497239e-01  2.69133049e+02  1.67682653e+04  8.38060403e+00
   1.20309899e+01  4.30756183e-04  4.49912123e+00 -3.80573424e-01
   9.98247930e-01]]

顯示的影像如下所示:

Haralic Features

忽略零值的 Haralick 特徵

在某些影像分析場景中,需要在計算 Haralick 紋理特徵期間忽略特定的畫素值。

一個常見的情況是當零值表示應從分析中排除的特定背景或噪聲時。

在 Mahotas 中,我們可以透過將 **ignore_zeros** 引數設定為 **True** 來忽略零值。

這將忽略零值。

示例

在這裡,我們嘗試透過忽略其零值來計算影像的 Haralick 特徵:

import mahotas
import numpy as np
import matplotlib.pyplot as mtplt
# Load a grayscale image
image = mahotas.imread('sun.png', as_grey=True).astype(np.uint8)
# Compute Haralick texture features while ignoring zero pixels
g = ignore_zeros=True
features = mahotas.features.haralick(image,g)
print(features)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the haralick featured image
axes[1].imshow(features, cmap='gray')
axes[1].set_title('Haralick Feature')
axes[1].axis('off')
mtplt.show()

輸出

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

[[ 2.67939014e-03   5.27444410e+01 9.94759846e-01  5.03271870e+03
   5.82786178e-01   2.18400839e+02 2.00781303e+04  8.26680366e+00
   1.06263358e+01   1.01107651e-03 2.91875064e+00 -5.66759616e-01
   9.99888025e-01]
 [ 2.00109668e-03   1.00750583e+02 9.89991374e-01  5.03318740e+03
   4.90503673e-01   2.18387049e+02 2.00319990e+04  8.32862989e+00
   1.12183182e+01   7.15118996e-04 3.43564495e+00 -4.86983515e-01
   9.99634586e-01]
 [ 2.29690324e-03   6.34944689e+01 9.93691944e-01  5.03280779e+03
   5.33850851e-01   2.18354256e+02 2.00677367e+04  8.30278737e+00
   1.09228656e+01   8.42614942e-04 3.16166477e+00 -5.26842246e-01
   9.99797686e-01]
 [ 2.00666032e-03   1.07074413e+02 9.89363195e-01  5.03320370e+03
   4.91882840e-01   2.18386605e+02 2.00257404e+04  8.32829316e+00
   1.12259184e+01   7.18459598e-04 3.44609033e+00 -4.85960134e-01
   9.99629000e-01]]

獲得的影像如下所示:

Haralick Features Ignore Zeros

計算具有第 14 個特徵的 Haralick 特徵

第 14 個特徵,平方和方差,計算為 GLCM 元素的方差,加權為其距離的平方。它提供了有關紋理平滑度的資訊。

高值表示畫素對在強度和距離方面具有更多樣化的分佈,表示粗糙的紋理。而低值表示更均勻或更平滑的紋理。

在 Mahotas 中,我們可以透過將 **compute_14th_feature** 引數設定為 **True** 來計算 Haralick 的第 14 個特徵。

示例

現在,我們正在計算影像的第 14 個 Haralick 特徵:

import mahotas
import numpy as np
import matplotlib.pyplot as mtplt
# Load a grayscale image
image = mahotas.imread('tree.tiff', as_grey=True).astype(np.uint8)
# Compute Haralick texture features and include the 14th feature
features = mahotas.features.haralick(image, compute_14th_feature=True)
print(features)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the haralick featured image
axes[1].imshow(features, cmap='gray')
axes[1].set_title('Haralick Feature')
axes[1].axis('off')
mtplt.show()

輸出

產生的輸出如下所示:

[[ 9.21802518e-04  9.60973236e+02  9.37166491e-01  7.64698044e+03
   2.80301553e-01  2.25538844e+02  2.96269485e+04  8.67755638e+00
   1.32391345e+01  2.45576289e-04  5.30868095e+00 -2.86604804e-01
   9.94019510e-01  6.66066209e+00]
 [ 7.16875904e-04  1.64001329e+03  8.92817748e-01  7.65058234e+03
   2.39157134e-01  2.25628036e+02  2.89623161e+04  8.72580856e+00
   1.36201726e+01  1.80965000e-04  5.70631449e+00 -2.37235244e-01
   9.87128410e-01  6.52870916e+00]
 [ 8.28978095e-04  9.93880455e+02  9.35041963e-01  7.65017308e+03
   2.64905787e-01  2.25647417e+02  2.96068119e+04  8.69690646e+00
   1.33344285e+01  2.21103895e-04  5.38241896e+00 -2.74238405e-01
   9.92754897e-01  7.00379254e+00]
 [ 7.11697171e-04  1.51531034e+03  9.00967635e-01  7.65058141e+03
   2.38821560e-01  2.25628110e+02  2.90870153e+04  8.72404507e+00
   1.35861240e+01  1.82002747e-04  5.66026317e+00 -2.41641969e-01
   9.87980919e-01  6.65491250e+00]]

我們得到如下所示的影像:

Haralick Features
廣告