Python批量提取图像灰度共生矩阵(GLCM)、支持批量处理、任意图像格式

目录

一、介绍

二、实现

1、特征计算

2、批量处理

3、结果


一、介绍

灰度共生矩阵(Grey Level Co-occurrence Matrix)也叫做空间灰度级依赖矩阵(SGLDM),它是一种基于统计的纹理特征提取的方法。

一般包括四个方向:

  • (a,b)=(1,0),像素对是水平的,即0度扫描;
  • (a,b)=(0,1),像素对是垂直的,即90度扫描;
  • (a,b)=(1,1),像素对是右对角线的,即45度扫描;
  • (a,b)=(-1,1),像素对是左对角线,即135度扫描。

一般包括8个常用特征:均值、方差、角二阶矩、熵、对比度、相关性、Homogeneity、Dissimilarity

二、实现

1、特征计算

python 复制代码
# 建立特征函数索引
indexs = {0:calculate_gray_co_occurrence_matrix_entropy, # 熵
          1:calculate_gray_co_occurrence_matrix_mean, # 均值
          2:calculate_gray_co_occurrence_matrix_variance, # 方差
          3:calculate_gray_co_occurrence_matrix_homogeneity, # homogeneity
          4:calculate_gray_co_occurrence_matrix_contrast, # contrast
          5:calculate_gray_co_occurrence_matrix_dissimilarity, # Dissimilarity
          6:calculate_gray_co_occurrence_matrix_energy, # 能量or角二阶
          7:calculate_gray_co_occurrence_matrix_correlation, # 相关性
          8:calculate_gray_co_occurrence_matrix_autocorrelation # 自相关性
          }

2、批量处理

其中 .jpg 修改为自己文件的后缀

python 复制代码
if __name__=="__main__":
    path = r"./data"
    save_ = r"./texture"

    file_list = os.listdir(path)
    for i in file_list:
        if os.path.splitext(i)[1] == ".jpg":
            print("正在处理文件:", i)

            file_path = path + "/" + i
            glcm = glcm_features(file_path)
            print(glcm.shape)
            utils.save_img(glcm, save_+"/" + os.path.splitext(i)[0] +'.tif')

3、结果

我们提取下面的图像:

只提取均值这个特征:

代码链接:Python批量提取图像灰度共生矩阵(GLCM)、支持批量处理、任意图像格式

相关推荐
七牛云行业应用几秒前
Dots 完整教程:从安装入口、跑第一个任务到给 Codex 派活(2026 年 10 月)
人工智能·大模型·agent
亿道电子Emdoor1 分钟前
【Perforce】Klocwork-kwgcheck图形化界面无法打开如何解决
git·python·github
迅猛龙办公室2 小时前
python实现倒序输出数字
开发语言·python
镜象科技3 小时前
AI情感大模型应用场景全解:从校园到医院再到企业的落地地图
人工智能
Joshua-a4 小时前
工程数学-理解卷积的含义_线性时不变系统的冲激响应与卷积
人工智能·深度学习
song5014 小时前
React Native for OpenHarmony 实战:三方库 react-native-css-transformer 的鸿蒙化适配指南
css·人工智能·深度学习·react native·transformer
老金带你玩AI8 小时前
ChatGPT dots,到底能替你操多少心?
人工智能
AliCloudROS9 小时前
计算巢 X DeepSeek Harness — 云端智能体工作台
人工智能·阿里云
深蓝AI9 小时前
GitHub的Push一年涨4.9倍:AI Agent为什么逼它重做Git存储?
人工智能·github
stereohomology10 小时前
大模型的观点谨:StoryTold 「Crafting Apps」四件套 · 深度总览
人工智能·llm