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加速大规模计算。

相关推荐
民乐团扒谱机14 小时前
脉冲在克尔效应下的频谱展宽仿真:原理与 MATLAB 实现
开发语言·matlab·光电·非线性光学·克尔效应
yuan1999714 小时前
基于扩展卡尔曼滤波的电池荷电状态估算的MATLAB实现
开发语言·matlab
chao18984415 小时前
多光谱图像融合:IHS、PCA与小波变换的MATLAB实现
图像处理·计算机视觉·matlab
我爱C编程15 小时前
基于无六环H校验矩阵和归一化偏移minsum算法的LDPC编译码matlab性能仿真
matlab·矩阵·ldpc·无六环·归一化偏移·minsum
bubiyoushang88815 小时前
使用MATLAB计算梁单元的刚度矩阵和质量矩阵
开发语言·matlab·矩阵
OpenBayes21 小时前
OCR 新范式!DeepSeek 以「视觉压缩」替代传统字符识别;Bald Classification数据集助力高精度人像分类
人工智能·深度学习·分类·数据挖掘·ocr·数据集·deepseek
lqqjuly21 小时前
Matlab2025a实现双目相机标定~业余版
开发语言·matlab·相机标定·双目相机
jerryinwuhan1 天前
SVM案例分析
算法·机器学习·支持向量机
Dev7z1 天前
结合HOG特征与支持向量机(SVM)的车牌字符识别系统
人工智能·分类·数据挖掘