MATLAB实现高光谱分类算法

一、环境配置与数据加载

matlab 复制代码
% 安装必要工具箱
% 需要Image Processing Toolbox和Statistics and Machine Learning Toolbox

%% 数据加载(Indian Pines数据集)
load('Indian_pines_corrected.mat'); % 原始数据
load('Indian_pines_gt.mat');        % 标注数据

% 转换为MATLAB数组
X = indian_pines_corrected;         % 145x145x220
gt = indian_pines_gt;               % 145x145

% 去除无效样本(标签为0)
valid_indices = gt(:) ~= 0;
X = X(valid_indices);
gt = gt(valid_indices);

% 数据维度调整
X = reshape(X, [], size(X,3));      % (145 * 145)x220 = 21025x220
gt = gt(valid_indices);

二、数据预处理

1. 光谱归一化
matlab 复制代码
% 最小-最大归一化
X_normalized = (X - min(X(:))) ./ (max(X(:)) - min(X(:)));

% PCA降维(保留95%方差)
[coeff, score, ~] = pca(X_normalized);
cum_var = cumsum(var(score))/sum(var(score));
n_components = find(cum_var >= 0.95, 1);
X_pca = score(:,1:n_components);
2. 数据划分
matlab 复制代码
% 划分训练集/测试集(7:3)
rng(0); % 固定随机种子
cv = cvpartition(size(X_pca,1),'HoldOut',0.3);
X_train = X_pca(cv.training,:);
y_train = gt(cv.training);
X_test = X_pca(cv.test,:);
y_test = gt(cv.test);

三、监督分类算法

1. 支持向量机(SVM)
matlab 复制代码
%% SVM分类
tic;
svmModel = fitcecoc(X_train, y_train, 'Learners', 'linear', 'Coding', 'onevsall');
y_pred = predict(svmModel, X_test);
accuracy = sum(y_pred == y_test)/numel(y_test);
fprintf('SVM Accuracy: %.2f%%
', accuracy*100);
toc;
2. 随机森林(Random Forest)
matlab 复制代码
%% 随机森林分类
tic;
rfModel = TreeBagger(200, X_train, y_train, 'Method', 'classification');
y_pred = predict(rfModel, X_test);
accuracy = sum(strcmp(y_pred, num2str(y_test'))) / numel(y_test);
fprintf('RF Accuracy: %.2f%%
', accuracy*100);
toc;

四、非监督分类算法

1. K-means聚类
matlab 复制代码
%% K-means分类
tic;
[idx, centers] = kmeans(X_pca, 16, 'MaxIter', 1000, 'Replicates', 5);
y_pred = idx;

% 计算轮廓系数
silhouetteScore = mean(silhouette(X_pca, idx));
fprintf('K-means Silhouette Score: %.2f
', silhouetteScore);
toc;

% 可视化
figure;
gscatter(X_pca(:,1), X_pca(:,2), y_pred);
title('K-means Clustering Results');
xlabel('PC1'); ylabel('PC2');

五、深度学习方法(3D-CNN)

1. 网络架构定义
matlab 复制代码
layers = [
    image3dInputLayer([9 9 200](@ref) % 9x9空间窗口,200波段
    convolution3dLayer([3 3 7], 32, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling3dLayer([2 2 2](@ref)
    
    convolution3dLayer([3 3 5], 64, 'Padding', 'same')
    batchNormalizationLayer
    reluLayer
    maxPooling3dLayer([2 2 2](@ref)
    
    fullyConnectedLayer(128)
    reluLayer
    dropoutLayer(0.5)
    fullyConnectedLayer(16)
    softmaxLayer
    classificationLayer];
2. 模型训练
matlab 复制代码
%% 数据准备
X_train_reshaped = reshape(X_train', 9,9,200,[]);
X_test_reshaped = reshape(X_test', 9,9,200,[]);

% 数据增强
augmentedData = imageDataAugmenter('RandRotation', [-15,15], 'RandXReflection', true);
augmentedSet = augmentedImageDatastore([9 9 200], X_train_reshaped, y_train, 'DataAugmentation', augmentedData);

% 训练配置
options = trainingOptions('adam',...
    'MaxEpochs', 50,...
    'MiniBatchSize', 64,...
    'InitialLearnRate', 0.001,...
    'Shuffle', 'every-epoch',...
    'ValidationData',{X_test_reshaped, y_test},...
    'Plots', 'training-progress');

% 模型训练
net = trainNetwork(augmentedSet, layers, options);
3. 分类预测
matlab 复制代码
%% 预测与评估
y_pred = classify(net, X_test_reshaped);
accuracy = sum(y_pred == y_test)/numel(y_test);
fprintf('3D-CNN Accuracy: %.2f%%
', accuracy*100);

% 混淆矩阵
figure;
confusionchart(y_test, y_pred);
title('Confusion Matrix');

六、特征可视化与分析

1. 光谱特征提取
matlab 复制代码
% 提取训练样本光谱特征
sample_indices = randperm(size(X_pca,1), 5);
sample_spectra = X_pca(sample_indices,:);

% 绘制光谱曲线
figure;
plot(1:size(sample_spectra,2), sample_spectra');
xlabel('波段序号'); ylabel('反射率');
legend('样本1','样本2','样本3','样本4','样本5');
title('典型地物光谱特征');
2. t-SNE降维可视化
matlab 复制代码
%% t-SNE可视化
tic;
Y = tsne(X_pca, 'NumDimensions', 2, 'Perplexity', 30);
toc;

figure;
gscatter(Y(:,1), Y(:,2), y_train);
title('t-SNE降维可视化');
xlabel('t-SNE 1'); ylabel('t-SNE 2');

参考代码 高光谱分类算法代码 www.youwenfan.com/contentcsk/79437.html

建议结合ENVI工具进行数据预处理,并使用MATLAB Parallel Server加速大规模计算。

相关推荐
机器学习之心1 小时前
基于BiGRU-Attention的轴承剩余寿命预测(MATLAB实现):从振动信号到RUL曲线的完整闭环
数据结构·算法·matlab·轴承剩余寿命预测·振动信号·bigru-attention
Evand J14 小时前
【MATLAB代码,车联网8】MPC网联车辆节能跟驰控制:速度跟踪、间距约束与能耗优化。MATLAB完整代码订阅专栏后可直接查看
matlab·代码·车联网·mpc·车辆·网联·网联技术
Jialu.16 小时前
中文 BERT 多任务分类项目:从模型结构到训练细节
人工智能·pytorch·分类·微软·nlp·bert
YOLO数据集集合1 天前
无人机屋顶与地物分割数据集 | 屋顶分割 航拍识别 城市规划 材质分类 实例分割9036期
人工智能·分类·无人机·材质·中国城市建筑·建筑识别
YOLO数据集集合1 天前
无人机型号识别数据集 | 无人机型号识别 细粒度分类 低空安防 无人机机型识别 大疆无人机 低空威胁9039期
目标检测·分类·数据挖掘·无人机·无人机数据集·无人机机型识别·无人机类别
我找到地球的支点啦2 天前
Matlab系列(009) 一CRC循环冗余校验详解
开发语言·数据结构·算法·matlab·信息与通信
YOLO数据集集合2 天前
中国城市建筑功能矢量数据集 | 建筑功能 矢量数据 城市规划 空间分析 Shapefile 多分类 GIS数据集8017期
人工智能·分类·数据挖掘·空间分析·城市规划·中国城市建筑·功能矢量
向哆哆4 天前
打击罂粟种植检测数据集分享(适用于YOLO系列深度学习分类检测任务)
深度学习·yolo·目标检测·分类
吃好睡好便好5 天前
查找函数的使用
学习·算法·matlab·生活·查找函数
chhttty5 天前
Design Verifier检查:整型数溢出检查与修复
matlab·simulink