%利用超像素的特征来训练分类器。一块一块地输入分类器分类 输入超像素块数来得到分类结果
clc;clear;
% no_classes = 16;
no_classes = 9;
no_train = round(1000);
load pu1200
pulabel1200=labels;
load('D:\ccpr论文程序\dataset\PaviaU.mat');
load('D:\ccpr论文程序\dataset\PaviaU_gt.mat');
load Indian_pines_gt
%%
img=paviaU;
indian_pines_gt=paviaU_gt;
indian_pines_corrected=paviaU;
%%
Resultfinal=[];
OAmax=0;
CAmean=zeros(no_classes,1);
for expi=1:1
tic
[supermatrix,sup_img,indexescell]=generatemeanmatrix(indian_pines_corrected,pulabel1200);
toc
img=supermatrix;
%选取训练集与测试集
[no_lines, no_rows, no_bands] = size(img);
img = ToVector(img);
img = img';
%每个样本选取的个数
% RandSampled_Num = round([300 300 300 300 300 300 300 300 300]);%pavia U
RandSampled_Num = round([200 200 200 200 200 200 200 200 200]);
Nonzero_map = zeros(no_lines,no_rows);
Nonzero_index = find(indian_pines_gt ~= 0);
Nonzero_map(Nonzero_index)=1;
Train_Label = [];
Train_index = [];
for ii = 1: no_classes
index_ii = find(indian_pines_gt == ii);
class_ii = ones(length(index_ii),1)* ii;
Train_Label = [Train_Label class_ii'];
Train_index = [Train_index index_ii'];
end
trainall = zeros(2,length(Train_index));
trainall(1,:) = Train_index;
trainall(2,:) = Train_Label;
indexes =[];
for i = 1: no_classes
W_Class_Index = find(Train_Label == i);
Random_num = randperm(length(W_Class_Index));
Random_Index = W_Class_Index(Random_num);
Tr_Index = Random_Index(1:RandSampled_Num(i));
indexes = [indexes Tr_Index];
end
indexes = indexes';
train_SL = trainall(:,indexes);
train_samples = img(:,train_SL(1,:))';
train_labels= train_SL(2,:)';
%选出部分分类结果作为训练样本
%1027个训练样本
% the remaining used for test
test_SL = trainall;
test_SL(:,indexes) = [];
test_samples = img(:,test_SL(1,:))';
test_labels = test_SL(2,:)';
% Normalizing Training and original img
[train_samples,M,m] = scale_func(train_samples);
[img ] = scale_func(img',M,m);
[sup_img ] = scale_func(sup_img',M,m);
%elm-------------------------------------------
train_samples=train_samples';
train_labels=train_labels';
img=img';
sup_img=sup_img';
tic
[IW,B,LW,TF,TYPE] = elmtrain(train_samples,train_labels,2050,'sig',1);%330,'sig',1);
Result=elmpredict(sup_img,IW,B,LW,TF,TYPE);%%ELM训练与测试过程
toc
temp=zeros(1,no_lines*no_rows);
for i=1:length(Result)
temp(indexescell{1,i})=Result(i);
end
Result=temp;%%将每个特征向量对应的标号赋予对应超像素
Result = Result';
%elm------------------------------------
ResultTest = Result(test_SL(1,:),:);
final=find(ResultTest==test_labels);
GroudTest = double(test_labels(:,1));
ResultTest = Result(test_SL(1,:),:);
[OA(expi),AA(expi),kappa(expi),CA(:,expi)]=confusion(GroudTest,ResultTest)
if OA(expi)>OAmax
OAmax=OA(expi);
Resultfinal=Result;%5/22
end
CAmean=CA(:,expi)+CAmean;
end
OA_mean=mean(OA)
AA_mean=mean(AA)
kappa_mean=mean(kappa)%%看十次平均值 revised 14/5/9
CAmean=CAmean/1
classmap=reshape(Resultfinal,no_lines, no_rows);
resultmap1=classmap.*Nonzero_map;
imagesc(resultmap1);
axis off;
% A=getframe;
% imshow(A.cdata)
% imwrite(A.cdata,'C:\Users\Think\Desktop\实验图像\2.jpg')