基于MATLAB的HOG+GLCM特征提取与SVM分类实现

一、核心流程
  1. 数据准备:组织训练集/测试集文件夹结构
  2. 图像预处理:灰度化、归一化、尺寸统一
  3. 特征提取:HOG(梯度方向直方图) + GLCM(灰度共生矩阵)
  4. 特征融合:串联HOG与GLCM特征向量
  5. SVM分类:训练多类分类模型并评估性能

二、MATLAB代码实现
1. 数据预处理与特征提取
matlab 复制代码
%% 参数设置
imgSize = [64,64];      % 统一图像尺寸
cellSize = [8,8];       % HOG单元尺寸
distances = [1];        % GLCM计算距离
angles = [0, pi/4, pi/2, 3*pi/4]; % GLCM方向

%% 加载数据集
trainingSet = imageSet('train_data/', 'recursive');
testSet = imageSet('test_data/', 'recursive');

%% 特征提取函数
function features = extractFeatures(img)
    % 灰度化与尺寸调整
    grayImg = rgb2gray(imresize(img, imgSize));
    
    % 提取HOG特征
    hogFeat = extractHOGFeatures(grayImg, 'CellSize', cellSize);
    
    % 提取GLCM特征
    glcmFeat = [];
    for i = 1:length(angles)
        glcm = graycomatrix(grayImg, 'Offset', [0, distances(i)*sin(angles(i)), 0, distances(i)*cos(angles(i))]);
        glcmFeat = [glcmFeat, graycoprops(glcm, {'Contrast', 'Energy', 'Correlation', 'Homogeneity'})];
    end
    
    % 特征融合
    features = [hogFeat, glcmFeat];
end

%% 构建特征矩阵与标签
trainingFeatures = [];
trainingLabels = [];
testFeatures = [];
testLabels = [];

for i = 1:trainingSet.Count
    img = read(trainingSet, i);
    label = trainingSet(i).Description;
    features = extractFeatures(img);
    trainingFeatures = [trainingFeatures; features];
    trainingLabels = [trainingLabels; repmat(label, size(features,1), 1)];
end

for i = 1:testSet.Count
    img = read(testSet, i);
    label = testSet(i).Description;
    features = extractFeatures(img);
    testFeatures = [testFeatures; features];
    testLabels = [testLabels; repmat(label, size(features,1), 1)];
end
2. SVM分类模型训练
matlab 复制代码
%% 数据标准化
scaler = fitcecoc(trainingFeatures, trainingLabels, 'Learners', 'svm', ...
    'Coding', 'onevsall', 'CrossVal', 'on', 'KFold', 5);

%% 模型训练
svmModel = fitcecoc(trainingFeatures, trainingLabels, ...
    'Learners', templateSVM('KernelFunction', 'rbf', 'BoxConstraint', 10, 'KernelScale', 'auto'));

%% 预测与评估
predictedLabels = predict(svmModel, testFeatures);
confMat = confusionmat(testLabels, predictedLabels);
accuracy = sum(diag(confMat))/sum(confMat(:));
disp(['分类准确率: ', num2str(accuracy*100, '%.2f'), '%']);

三、关键优化
1. 特征降维
matlab 复制代码
% 使用PCA降维(保留95%方差)
[coeff, score, ~] = pca(trainingFeatures);
selectedFeatures = score(:,1:100); % 选择前100个主成分
2. 参数调优
matlab 复制代码
% 网格搜索优化SVM参数
tuneModel = fitcsvm(trainingFeatures, trainingLabels, ...
    'KernelFunction', 'rbf', ...
    'OptimizeHyperparameters', 'auto', ...
    'HyperparameterOptimizationOptions', struct('AcquisitionFunctionName', 'expected-improvement-plus'));
3. 类别不平衡处理
matlab 复制代码
% 添加类别权重
classWeights = [0.8, 0.2]; % 根据样本比例设置
svmModel = fitcecoc(trainingFeatures, trainingLabels, ...
    'ClassNames', unique(trainingLabels), ...
    'Cost', [0 1; 2 0], ... % 错误分类惩罚矩阵
    'ClassNames', classWeights);

四、性能评估
matlab 复制代码
%% 分类报告
disp('分类报告:');
disp(classificationReport(testLabels, predictedLabels));

%% ROC曲线(二分类示例)
[X,Y,T,AUC] = perfcurve(testLabels, predictedLabels, 'cat1');
figure;
plot(X,Y);
xlabel('False Positive Rate');
ylabel('True Positive Rate');
title(['AUC = ', num2str(AUC, '%.2f')]);

参考代码 对图片提取HOG、GLCM特征,利用SVM进行分类 www.youwenfan.com/contentcsq/60101.html

五、完整代码结构
matlab 复制代码
Project/
├── data/
│   ├── train_data/  # 训练集(子文件夹为类别)
│   └── test_data/   # 测试集
├── src/
│   ├── feature_extraction.m  # 特征提取函数
│   └── svm_classifier.m      # SVM分类函数
├── results/
│   ├── confusion_matrix.png
│   └── roc_curve.png
└── main.m                    # 主程序入口

六、典型应用场景
  1. 工业质检:识别金属表面缺陷(划痕、斑点等)
  2. 医学影像:分类X光片中的病变区域(如肺炎检测)
  3. 交通标志识别:基于偏振特性的多角度分类

七、注意事项
  1. 数据集平衡 :使用imbalancedData工具箱处理类别不均衡
  2. 特征可视化 :通过t-SNE降维观察特征分布
  3. 实时性优化 :使用parfor并行计算加速特征提取

八、扩展改进
  1. 多尺度特征融合:结合不同尺度的HOG特征
  2. 深度学习结合:使用预训练CNN提取高层特征
  3. 迁移学习:在小样本场景下微调预训练模型
相关推荐
Loacnasfhia92 小时前
贝类海产品物种识别与分类_---_基于YOLOv10n与特征金字塔共享卷积的改进方法
yolo·分类·数据挖掘
机器学习之心2 小时前
Bayes-TCN+SHAP分析贝叶斯优化深度学习多变量分类预测可解释性分析!Matlab完整代码
深度学习·matlab·分类·贝叶斯优化深度学习
机器学习之心2 小时前
TCN+SHAP分析深度学习多变量分类预测可解释性分析!Matlab完整代码
深度学习·matlab·分类·多变量分类预测可解释性分析
爱吃rabbit的mq4 小时前
第7章 逻辑回归:二分类的基础
算法·分类·逻辑回归
XX風4 小时前
4.1 spectral clusterig
人工智能·机器学习·支持向量机
fengfuyao9855 小时前
基于MATLAB/Simulink的车辆自适应巡航控制(ACC)实现
开发语言·matlab
lrh1228006 小时前
详解逻辑回归算法:分类任务核心原理、损失函数与评估方法
人工智能·分类·数据挖掘
是小蟹呀^6 小时前
图像分类里的小样本学习(Few-shot Image Classification)
学习·分类·数据挖掘
guygg887 小时前
傅立叶光学的Matlab实现方法
开发语言·matlab