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

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

一、核心流程

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

二、MATLAB代码实现

1. 数据预处理与特征提取
%% 参数设置
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分类模型训练
%% 数据标准化
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. 特征降维
% 使用PCA降维(保留95%方差)
[coeff, score, ~] = pca(trainingFeatures);
selectedFeatures = score(:,1:100); % 选择前100个主成分
2. 参数调优
% 网格搜索优化SVM参数
tuneModel = fitcsvm(trainingFeatures, trainingLabels, ...
    'KernelFunction', 'rbf', ...
    'OptimizeHyperparameters', 'auto', ...
    'HyperparameterOptimizationOptions', struct('AcquisitionFunctionName', 'expected-improvement-plus'));
3. 类别不平衡处理
% 添加类别权重
classWeights = [0.8, 0.2]; % 根据样本比例设置
svmModel = fitcecoc(trainingFeatures, trainingLabels, ...
    'ClassNames', unique(trainingLabels), ...
    'Cost', [0 1; 2 0], ... % 错误分类惩罚矩阵
    'ClassNames', classWeights);

四、性能评估

%% 分类报告
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')]);

五、完整代码结构

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

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

六、典型应用场景

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

七、注意事项

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

八、扩展改进

  1. 多尺度特征融合:结合不同尺度的HOG特征
  2. 深度学习结合:使用预训练CNN提取高层特征
  3. 迁移学习:在小样本场景下微调预训练模型
相关文章
|
6天前
|
人工智能 JSON 监控
Claude Code 源码泄露:一份价值亿元的 AI 工程公开课
我以为顶级 AI 产品的护城河是模型。读完这 51.2 万行泄露的源码,我发现自己错了。
4203 16
|
16天前
|
人工智能 JSON 机器人
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
本文带你零成本玩转OpenClaw:学生认证白嫖6个月阿里云服务器,手把手配置飞书机器人、接入免费/高性价比AI模型(NVIDIA/通义),并打造微信公众号“全自动分身”——实时抓热榜、AI选题拆解、一键发布草稿,5分钟完成热点→文章全流程!
11671 138
让龙虾成为你的“公众号分身” | 阿里云服务器玩Openclaw
|
4天前
|
人工智能 数据可视化 安全
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
本文详解如何用阿里云Lighthouse一键部署OpenClaw,结合飞书CLI等工具,让AI真正“动手”——自动群发、生成科研日报、整理知识库。核心理念:未来软件应为AI而生,CLI即AI的“手脚”,实现高效、安全、可控的智能自动化。
1436 8
王炸组合!阿里云 OpenClaw X 飞书 CLI,开启 Agent 基建狂潮!(附带免费使用6个月服务器)
|
6天前
|
人工智能 自然语言处理 数据挖掘
零基础30分钟搞定 Claude Code,这一步90%的人直接跳过了
本文直击Claude Code使用痛点,提供零基础30分钟上手指南:强调必须配置“工作上下文”(about-me.md+anti-ai-style.md)、采用Cowork/Code模式、建立标准文件结构、用提问式提示词驱动AI理解→规划→执行。附可复制模板与真实项目启动法,助你将Claude从聊天工具升级为高效执行系统。
|
6天前
|
人工智能 定位技术
Claude Code源码泄露:8大隐藏功能曝光
2026年3月,Anthropic因配置失误致Claude Code超51万行源码泄露,意外促成“被动开源”。代码中藏有8大未发布功能,揭示其向“超级智能体”演进的完整蓝图,引发AI编程领域震动。(239字)
2388 9

热门文章

最新文章