GLCM 特征和LBP特征提取

GLCM 特征

GLCM(灰度共生矩阵)特征用于描述图像中像素灰度级之间的空间关系,常用于纹理分析。GLCM特征通过统计图像中各个灰度级对之间的出现频率来描述图像的纹理特征。GLCM特征包括能量(ASM)、对比度(Contrast)、熵(Entropy)、逆差矩(Inverse Difference Moment,IDM)和相关性(Correlation)等。

1. 能量(ASM)
   - 能量表示图像中像素灰度级对之间的概率的平方和,即像素对之间的均匀性程度。公式中的 \( p(i,j) \) 表示灰度级对 \( (i,j) \) 出现的概率,求和对所有可能的灰度级对进行。ASM越大,表示图像中的像素对越均匀,纹理越均匀。

2. 对比度(Contrast)
   - 对比度反映了图像中像素对的灰度级之间的差异程度。公式中的 \( (i-j)^2 \) 表示两个灰度级之间的差异的平方, \( p(i,j) \) 表示灰度级对 \( (i,j) \) 出现的概率。对比度越大,表示图像的纹理越粗糙,灰度级对之间的差异越大。

3. 熵(Entropy)
   - 熵用于描述图像的不确定性或信息量。公式中的 \( -\log(p(i,j)) \) 表示灰度级对 \( (i,j) \) 出现的概率的负对数,求和对所有可能的灰度级对进行。熵越大,表示图像中的像素对越不规则,纹理越复杂。

4. 逆差矩(IDM)
   - 逆差矩用于描述图像中灰度级对的一致性程度。公式中的 \( \frac{1}{1+(i-j)^2} \) 表示逆差值, \( p(i,j) \) 表示灰度级对 \( (i,j) \) 出现的概率。逆差矩越大,表示图像中的灰度级对越一致,纹理越均匀。

5. 相关性(Correlation)
   - 相关性用于描述图像中灰度级对的线性相关性程度。公式中的 \( \mu_x \) 和 \( \mu_y \) 分别表示灰度级对的横坐标和纵坐标的平均值, \( \sigma_x \) 和 \( \sigma_y \) 分别表示横坐标和纵坐标的标准差。相关性越接近1,表示图像中的灰度级对越线性相关。

GLCM特征的计算公式能够全面反映图像的纹理特征,能够用于纹理分析、图像分类等应用中。

 读取一张灰度图像,为了计算方便,本节将灰度图像的最高灰度级别从 256 降至 16。 之后从四个方向(0 度,45 度,90 度,135 度),步长d =1分别生成对应的四个灰度共生矩阵。图 4-5 所示为灰度图像 Imgw*h及其生成的四个灰度共生矩阵(其中,w为图片的宽度,h 为图片的高度, wh 不一定相等)。该灰度图像的最高灰度级别为 4,其在生成对应的灰度共生矩阵时,首先根据灰度图像的最高灰度级别生成一个4*4的矩阵,之后分别从四个方向,使用步长d =1遍历整个灰度图像,统计相应的灰度像素对出现的次数。 例如在灰度图像 Imgw*h中,水平方向 0 1 像素对出现的次数为 2,则其在水平方向的灰度共生矩阵中第 0 行第 1 列的元素记为 2。之后,基于每个灰度共生矩阵使用表 4- 2 所示的公式提取 5 个统计特征,即为该灰度共生矩阵提取的 5 GLCM 特征。最后每个可执行文件提取到 20 维的 GLCM 特征。

引用自参考论文  基于二进制文件的 C 语言编译器特征提取及识别

 实现代码

import numpy as np
import cv2
from skimage.feature import graycomatrix


def read_and_reduce_gray_levels(image_path, num_levels=16):
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
    image_reduced = (image // (256 // num_levels)).astype(np.uint8)
    return image_reduced


def compute_glcm(image, distances=[1], angles=[0, np.pi / 4, np.pi / 2, 3 * np.pi / 4]):
    glcm = graycomatrix(image, distances=distances, angles=angles, levels=16, symmetric=True, normed=True)
    return glcm


def compute_glcm_features(glcm):
    features = []
    num_levels = glcm.shape[0]

    for i in range(glcm.shape[2]):
        for j in range(glcm.shape[3]):
            glcm_matrix = glcm[:, :, i, j]
            asm = np.sum(glcm_matrix ** 2)
            contrast = np.sum(
                [[(i - j) ** 2 * glcm_matrix[i, j] for i in range(num_levels)] for j in range(num_levels)])
            entropy = -np.sum(
                [[(glcm_matrix[i, j] * np.log2(glcm_matrix[i, j] + 1e-10)) for i in range(num_levels)] for j in
                 range(num_levels)])
            idm = np.sum(
                [[(glcm_matrix[i, j] / (1 + (i - j) ** 2)) for i in range(num_levels)] for j in range(num_levels)])

            # Calculate mean and standard deviation for correlation
            i_indices = np.arange(num_levels)
            j_indices = np.arange(num_levels)
            mu_x = np.sum(i_indices * np.sum(glcm_matrix, axis=1))
            mu_y = np.sum(j_indices * np.sum(glcm_matrix, axis=0))
            sigma_x = np.sqrt(np.sum((i_indices - mu_x) ** 2 * np.sum(glcm_matrix, axis=1)))
            sigma_y = np.sqrt(np.sum((j_indices - mu_y) ** 2 * np.sum(glcm_matrix, axis=0)))

            correlation = 0
            if sigma_x > 0 and sigma_y > 0:
                correlation = np.sum([[(i - mu_x) * (j - mu_y) * glcm_matrix[i, j] for i in range(num_levels)] for j in
                                      range(num_levels)]) / (sigma_x * sigma_y)

            features.extend([asm, contrast, entropy, idm, correlation])

    return np.array(features)


def main(image_path):
    # 读取并降灰度级
    image = read_and_reduce_gray_levels(image_path, num_levels=16)

    # 计算GLCM
    glcm = compute_glcm(image)

    # 计算GLCM特征
    features = compute_glcm_features(glcm)

    print("GLCM Features:", features)
    return features


# 使用主函数处理图像并提取GLCM特征
glcm_features = main('test2.png')

利用上一篇文章用B2M算法生成的图像进行FLCM特征提取,得到如下数据

LBP特征

LBP(Local Binary Pattern)是一种简单而有效的图像纹理描述方法。

1. 纹理特征的有效捕捉

LBP能够很好地捕捉局部纹理特征。它通过比较每个像素与其邻域像素的灰度值,生成一个二进制模式,然后将这些模式转换为整数值来描述图像的局部纹理特征。这些局部特征可以用于图像的分类和识别。

2. 计算简单且高效

LBP算法计算简单,只涉及灰度值的比较和二进制编码操作,没有复杂的数学运算,因此计算效率高,适合实时应用。对于每个像素点,生成LBP值的过程仅需要比较其与邻域像素的值并进行二进制编码,这使得LBP在处理大规模图像数据时非常高效。

3. 旋转不变性和灰度不变性

LBP具有旋转不变性和灰度不变性。当图像中的物体旋转或光照条件改变时,LBP特征能够保持相对稳定。这是因为LBP只考虑像素之间的相对关系,而不是绝对灰度值,从而使其对图像的旋转和灰度变化具有鲁棒性。

4. 统计特性

LBP特征向量是通过统计每个LBP模式在图像中出现的频率得到的,这种统计特性使得LBP能够捕捉图像的全局纹理信息。在实际应用中,256维的LBP特征向量可以用于构建纹理直方图,这种直方图能够反映图像的整体纹理分布。

实现:

  1. 计算LBP

    • local_binary_pattern函数计算每个像素点的LBP值,P表示邻域像素的数量(默认8),R表示半径(默认1)。
    • 这种方法通过对每个像素点进行局部计算,能够有效捕捉局部纹理特征。
  2. 统计LBP值的频率

    • np.histogram函数统计LBP值在整个图像中出现的频率,并生成256维的特征向量。
    • 这种统计方法能够反映图像的整体纹理分布,有助于图像的分类和识别。
import numpy as np
import cv2
from skimage.feature import local_binary_pattern


def compute_lbp_features(image, P=8, R=1):
    # 计算LBP
    lbp = local_binary_pattern(image, P, R, method='uniform')

    # 统计256维向量
    n_bins = int(lbp.max() + 1)
    hist, _ = np.histogram(lbp, bins=n_bins, range=(0, n_bins), density=True)

    return hist


def main(image_path):
    # 读取灰度图像
    image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

    # 计算LBP特征
    lbp_features = compute_lbp_features(image)

    print("LBP Features:", lbp_features)
    return lbp_features


# 使用主函数处理图像并提取LBP特征
lbp_features = main('test2.png')

使用的是刚刚同一图像,结果如下

  • 15
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GLCM stands for Gray Level Co-occurrence Matrix, which is a statistical method used to analyze the texture of an image. In Python, the scikit-image library provides functions to compute GLCMs and extract features from them. Here's an example of how to compute a GLCM in Python using scikit-image: ```python from skimage.feature import greycomatrix # Load an image image = ... # Compute a GLCM with a distance of 1 and an angle of 0 degrees glcm = greycomatrix(image, distances=[1], angles=[0], levels=256, symmetric=True, normed=True) ``` This will compute a GLCM for the input image with a distance of 1 pixel and an angle of 0 degrees. The `levels` parameter specifies the number of gray levels in the image (usually 256 for an 8-bit image). The `symmetric` parameter specifies whether the GLCM should be symmetric (i.e., whether the order of the pixel pairs matters). The `normed` parameter specifies whether the GLCM should be normalized (i.e., whether the values in the matrix should sum to 1.0). Once you have a GLCM, you can extract features from it using the `skimage.feature.greycoprops` function. For example, here's how to compute the contrast, energy, and homogeneity of a GLCM: ```python from skimage.feature import greycoprops contrast = greycoprops(glcm, 'contrast') energy = greycoprops(glcm, 'energy') homogeneity = greycoprops(glcm, 'homogeneity') ``` The `greycoprops` function accepts a GLCM and a feature name as input and returns the corresponding feature value. Other features that can be extracted from a GLCM include correlation, entropy, and dissimilarity.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值