Mahotas 简明教程

Mahotas - Haralic Features

Haralick 特征描述图像的纹理。纹理是指图像中赋予其特定外观的图案,例如表面的平滑度或物体的排列。

要处理 Haralick 特征,我们使用一个称为灰度共生矩阵 (GLCM) 的特殊矩阵。它是一个代表图像中像素强度对之间的关系的矩阵。

它提供了有关在图像中的特定距离处不同像素强度值组合出现频率的信息。

Haralic Features in Mahotas

要使用 Mahotas 计算 Haralick 特征,请通过指定像素对的距离和方向创建 GLCM。接下来,使用 GLCM 计算构成 Haralick 特征的各种统计度量。

这些度量包括对比度、相关性、能量、熵、均匀性等。最后,检索计算出的 Haralick 特征。

例如,Haralick 纹理分析中的对比度特征告诉我们图像中相邻像素的明暗度差异。要计算此特征,我们分析 GLCM 矩阵。

此矩阵显示了具有不同亮度级别的像素对一起出现以及在图像中的位置的频率。

我们可以使用 mahotas.features.haralick() 函数来计算 mahotas 中的 haralick 特征。

The mahotas.features.haralick() function

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() 函数接受的参数 −

  1. f − 这是输入图像。

  2. ignore_zeros (optional) − 它计算在计算 Haralick 特征时是否应该忽略(True)或考虑(False)输入矩阵中的零值。

  3. preserve_haralick_bug (optional) − 它决定是否在方程式中复制 Haralick 的印刷错误

  4. compute_14th_feature (optional) − 它指示是否计算第 14 个 Haralick 特征(相异性)。默认情况下,它设置为 False。

  5. use_x_minus_y_variance (optional) − 默认情况下,mahotas 使用 VAR[P(|x−y|)], 但如果此参数为 True,则使用 VAR[|x−y|].

  6. distance (optional) − 它表示在计算 GLCM 时使用的像素距离。它决定了在分析像素之间的空间关系时所考虑的邻域大小。默认情况下,它设置为 1。

  7. return_mean − 设置为 True 时,该函数返回所有方向的平均值。

  8. return_mean_ptp − 设置为 True 时,该函数返回所有方向的平均值和点对点 (ptp) 值(max() 和 min() 之间的差值)。

以下是 matlab 中计算 haralic 特征的基本示例 −

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 Features with Ignore Zeros

在某些图像分析场景中,需要在计算 Haralick 纹理特征期间忽略特定的像素值。

一种常见的情况是当零值表示应该从分析中排除的特定背景或噪声时。

在 Mahotas 中,我们可以通过将 ignore_zeros 参数设置为 True. 来忽略零值

这将忽略零值。

Example

在这里,我们试图通过忽略其零值来计算图像的 haralicks 特征 −

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]]

获得的图像如下所示:

haralic features ignore zeros

Computing Haralick Features with 14th Feature

第 14 个特征(平方和方差)被计算为 GLCM 元素的方差,其权重由它们的距离的平方决定。它提供了有关纹理平滑度的信息。

较高值表示像素对在强度和距离方面分布更加分散,表示纹理粗糙。而较低值表示纹理更加均匀或平滑。

在 Mahotas 中,我们可以通过将 compute_14th_feature 参数设为 True 来计算 Haralick 的第 14 个特征。

Example

现在,我们正在计算一张图像的第 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]]

我们获得图像如下 −

haralic features1