from skimage import feature import cv2 import numpy as np import os # 图像路径 img_path = "2.jpg" # ✅ 1. 检查文件是否存在 if not os.path.exists(img_path): raise FileNotFoundError(f"图像文件不存在,请检查路径: {img_path}") # ✅ 2. 以灰度模式读取图像 img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) if img is None: raise ValueError(f"无法读取图像,可能是文件损坏或格式问题: {img_path}") print(f"✅ 图像加载成功,原始尺寸: {img.shape}") ''' height, width = img.shape start_row = height // 3 * 2 # 从 2/3 处开始,到底部 bottom_region = img[start_row:, :] # 所有列,只取底部 1/3 行 print(f"✅ 裁剪出底部 1/3 区域,尺寸: {bottom_region.shape}") ''' # ✅ 3. 提取最下方三分之一的区域 bottom_region = img # ✅ 4. 计算 GLCM(建议降低 levels 或使用压缩) # 注意:levels=256 对计算资源要求高,可考虑先归一化到 32 或 64 灰度级 # 这里我们先保持 256,若报内存错误可改用降级方法 glcm = feature.graycomatrix( bottom_region, distances=[1], angles=[0, np.pi/4, np.pi/2, 3*np.pi/4], # 多方向 levels=256, symmetric=True, normed=True ) # ✅ 5. 提取平均纹理特征(在所有方向上取均值) properties = ['contrast', 'dissimilarity', 'homogeneity', 'energy', 'correlation'] features = {} for prop in properties: values = feature.graycoprops(glcm, prop)[0] features[prop] = np.mean(values) # 对多个角度取平均 # ✅ 6. 输出结果 print("\n📊 图像底部 1/3 区域的纹理特征:") print(f"Contrast: {features['contrast']:.3f}") # 越大越粗糙230 print(f"Dissimilarity: {features['dissimilarity']:.3f}") # 越大差异越大7.265 print(f"Homogeneity: {features['homogeneity']:.3f}") # 越小越不均匀(粗糙)0.291 print(f"Energy: {features['energy']:.3f}") # 越小越复杂(纹理丰富)0.033 print(f"Correlation: {features['correlation']:.3f}") # 灰度线性相关性0.941