Mahotas 简明教程
Mahotas - Haralic Features
Haralick 特征描述图像的纹理。纹理是指图像中赋予其特定外观的图案,例如表面的平滑度或物体的排列。
Haralick features describes the texture of an image. Texture refers to the patterns in an image that give it a specific look, such as the smoothness of a surface or the arrangement of objects.
要处理 Haralick 特征,我们使用一个称为灰度共生矩阵 (GLCM) 的特殊矩阵。它是一个代表图像中像素强度对之间的关系的矩阵。
To work on Haralick features, we use a special matrix known as gray−level co−occurrence matrix (GLCM). It is a matrix that represents the relationship between pairs of pixel intensities in an image.
它提供了有关在图像中的特定距离处不同像素强度值组合出现频率的信息。
It provides information about how frequently different combinations of pixel intensity values occur at specific distances within the image.
Haralic Features in Mahotas
要使用 Mahotas 计算 Haralick 特征,请通过指定像素对的距离和方向创建 GLCM。接下来,使用 GLCM 计算构成 Haralick 特征的各种统计度量。
To calculate Haralick features using Mahotas, create the GLCM by specifying the distance and direction of pixel pairs. Next, use the GLCM to calculate various statistical measures that form the Haralick features.
这些度量包括对比度、相关性、能量、熵、均匀性等。最后,检索计算出的 Haralick 特征。
These measures include contrast, correlation, energy, entropy, homogeneity, and more. Finally, retrieve the computed Haralick features.
例如,Haralick 纹理分析中的对比度特征告诉我们图像中相邻像素的明暗度差异。要计算此特征,我们分析 GLCM 矩阵。
For example, the contrast feature in Haralick’s texture analysis tells us how much the brightness or darkness of neighboring pixels varies in an image. To calculate this feature, we analyze the GLCM matrix.
此矩阵显示了具有不同亮度级别的像素对一起出现以及在图像中的位置的频率。
This matrix shows how often pairs of pixelsx with different brightness levels appear together and where they are located in the image.
我们可以使用 mahotas.features.haralick() 函数来计算 mahotas 中的 haralick 特征。
We can use the mahotas.features.haralick() function to calculate the haralick features in mahotas.
The mahotas.features.haralick() function
haralick() 函数以灰度图像作为输入,返回计算出的 Haralick 特征。Haralick 特征基于灰度图像计算。
The haralick() function takes a grayscale image as input and returns the calculated Haralick features. Haralick features are calculated based on grayscale images.
Mahotas 允许我们通过分析图像的 GLCM 来计算 Haralick 特征。这样,我们可以提取图像中存在的纹理图案的信息。
Mahotas allows us to calculate Haralick features by analyzing the GLCM of an image. In such way, we can extract information about the texture patterns present in an image.
以下是 mahotas 中 haralick() 函数的基本语法 −
Following is the basic syntax of the haralick() function in mahotas −
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() 函数接受的参数 −
Following are the parameters accepted by the haralick() function in mahotas −
-
f − It is the input image.
-
ignore_zeros (optional) − It computes whether zero values in the input matrix should be ignored (True) or considered (False) when computing the Haralick features.
-
preserve_haralick_bug (optional) − It determines whether to replicate Haralick’s typo in the equations
-
compute_14th_feature (optional) − It indicates whether to compute the 14th Haralick feature (dissimilarity) or not. By default, it is set to False.
-
use_x_minus_y_variance (optional) − By default, mahotas uses VAR[P(|x−y|)], but if this parameter is True, it uses VAR[|x−y|].
-
distance (optional) − It represents the pixel distance used when computing the GLCM. It determines the neighborhood size considered when analyzing the spatial relationship between pixels. By default, it is set to 1.
-
return_mean − When set to True, the function returns the mean value across all the directions.
-
return_mean_ptp − When set to True, the function returns the mean value and the point−to−point (ptp) value (difference between max() and min()) across all the directions.
以下是 matlab 中计算 haralic 特征的基本示例 −
Following is the basic example to calculate the haralic features in mahotas −
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()
执行以上代码后,我们得到如下所示的输出 −
After executing the above code, we get the output as shown below −
[[ 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]]
显示的图像如下所示:
The image displayed is as shown below −
Haralick Features with Ignore Zeros
在某些图像分析场景中,需要在计算 Haralick 纹理特征期间忽略特定的像素值。
In certain image analysis scenarios, it is required to ignore specific pixel values during the computation of Haralick texture features.
一种常见的情况是当零值表示应该从分析中排除的特定背景或噪声时。
One common case is when zero values represent a specific background or noise that should be excluded from the analysis.
在 Mahotas 中,我们可以通过将 ignore_zeros 参数设置为 True. 来忽略零值
In Mahotas, we can ignore zero values by setting the ignore_zeros parameter to True.
这将忽略零值。
This will disregard zero values.
Example
在这里,我们试图通过忽略其零值来计算图像的 haralicks 特征 −
In here, we are trying to calculate tha haralicks feature of an image by ignoring its zero values −
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()
以下是上面代码的输出: -
Following is the output of the above code −
[[ 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]]
获得的图像如下所示:
The image obtained is as follows −
Computing Haralick Features with 14th Feature
第 14 个特征(平方和方差)被计算为 GLCM 元素的方差,其权重由它们的距离的平方决定。它提供了有关纹理平滑度的信息。
The 14th feature, Sum of Squares Variance, is calculated as the variance of the GLCM elements weighted by the square of their distances. It provides information about the smoothness of the texture.
较高值表示像素对在强度和距离方面分布更加分散,表示纹理粗糙。而较低值表示纹理更加均匀或平滑。
A high value indicates a more diverse distribution of pixel pairs in terms of their intensities and distances, indicating a rough texture. Whereas, a low value indicates a more uniform or smooth texture.
在 Mahotas 中,我们可以通过将 compute_14th_feature 参数设为 True 来计算 Haralick 的第 14 个特征。
In Mahotas, we can compute the Haralicks 14th feature by setting the compute_14th_feature parameter to True.
Example
现在,我们正在计算一张图像的第 14 个 haralick 特征 −
Now, we are computing the 14th haralick features of an image −
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()
下面显示了产生的输出:
The output produced is as shown below −
[[ 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]]
我们获得图像如下 −
We get the image as follow −