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. 光谱归一化
% 最小-最大归一化
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. 数据划分
% 划分训练集/测试集(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)
%% 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)
%% 随机森林分类
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聚类
%% 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. 网络架构定义
layers = [image3dInputLayer([9 9 200](@ref) % 9x9空间窗口,200波段convolution3dLayer([3 3 7], 32, 'Padding', 'same')batchNormalizationLayerreluLayermaxPooling3dLayer([2 2 2](@ref)convolution3dLayer([3 3 5], 64, 'Padding', 'same')batchNormalizationLayerreluLayermaxPooling3dLayer([2 2 2](@ref)fullyConnectedLayer(128)reluLayerdropoutLayer(0.5)fullyConnectedLayer(16)softmaxLayerclassificationLayer];
2. 模型训练
%% 数据准备
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. 分类预测
%% 预测与评估
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. 光谱特征提取
% 提取训练样本光谱特征
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降维可视化
%% 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加速大规模计算。
