clc
clear all
%% found colorset
colorset = zeros(10,3);
%色彩1
colorset(1,1) = 255;
colorset(1,2) = 0;
colorset(1,3) = 0;
%色彩2
colorset(2,1) = 0;
colorset(2,2) = 255;
colorset(2,3) = 0;
%色彩3
colorset(3,1) = 0;
colorset(3,2) = 0;
colorset(3,3) = 255;
%色彩4
colorset(4,1) = 255;
colorset(4,2) = 255;
colorset(4,3) = 0;
%色彩5
colorset(5,1) = 125;
colorset(5,2) = 125;
colorset(5,3) = 125;
%色彩6
colorset(6,1) = 0;
colorset(6,2) = 0;
colorset(6,3) = 125;
%色彩7
colorset(7,1) = 0;
colorset(7,2) = 255;
colorset(7,3) = 255;
%色彩8
colorset(8,1) = 255;
colorset(8,2) = 0;
colorset(8,3) = 255;
%色彩9
colorset(9,1) = 125;
colorset(9,2) = 125;
colorset(9,3) = 0;
%色彩10
colorset(10,1) = 125;
colorset(10,2) = 125;
colorset(10,3) = 0;
%...
%色彩n
%% 输入训练数据和待分类图像信息
% line = 200;
% sample = 200;
%% 读取整幅遥感图像
%% 调用ReadImage函数
flag1 = ReadImage();
%% 读取ROI数据
[FileName,PathName,FilterIndex] = uigetfile({'*.txt','Text Files (*.txt)'},'请选择训练样本');
if (FilterIndex==0)
flag = 0;
return;
end
path=fullfile(PathName,FileName);
fin = fopen(path,'r');
tline = fgetl(fin);
%% 略去注释行
i=1;
while (tline(1) == ';')
flag = strfind(tline,'; Number of ROIs');
if isempty(flag)==0
class_num = sscanf(tline,'; Number of ROIs: %d');
tline = fgetl(fin);
continue;
end
flag = strfind(tline,'; ROI npts');
if isempty(flag)==0
num(i) = sscanf(tline,'; ROI npts: %d');
i = i + 1;
tline = fgetl(fin);
continue;
end
tline = fgetl(fin);
end
%% 生成纯数值文本
path = [path,'_new.txt'];
fout = fopen(path ,'w');
fprintf(fout,'%s\n',tline);
while ~feof(fin)
tline = fgetl(fin);
fprintf(fout,'%s\n',tline);
end
fclose(fout);
fclose(fin);
%% 调用Readtrain函数
flag2 = Readtrain(path,num);
train_num = sum(num);
%% 读入数据
%% %%载入训练数据
if flag2==0
% msgbox('请先选择训练样本数据!','错误','error','modal');
return;
else
load train.mat
P = train_data';
T = train_label;
end
%% %%载入待分类图像数据
if flag1==0
% msgbox('请先选择待分类的遥感图像!','错误','error','modal');
return;
else
load image.mat
P = [P,Image'];
end
%% %%载入验证数据
% load test.mat
% P = [P,test_data'];
%% 特征数据归一化
[P_scale,PS] = mapminmax(P);
clear P
%% 打乱排序,构建训练和测试样本
[trainV,valV,testV] = dividevec(P_scale(:,1:train_num),T,0.2,0);%训练数据和验证数据
testV.P = P_scale(:,train_num+1:end);%图像数据
% testV.T = test_label;
% %% 建立LM-ANN神经网络
% % 建立网络结构
% net = newff(trainV.P,trainV.T,30,{'logsig' 'logsig'},'trainlm');
% % 设置训练参数
% % net.trainFcn = 'trainlm'; %'trainlm' --- Levenberg-Marquardt算法 'trainbfg'--- Quasi-Newton Algorithms-BFGS Algorithm
% net.performFcn = 'sse'; %Sum squared error performance function
% % net.performFcn = 'mse'; %Mean squared error performance function
% net.trainParam.show = 1; %迭代输出间隔no. epochs between showing error
% net.trainParam.lr = 0.05; %学习步长yita learning rate, not default trainlm
%
% net.trainParam.epochs = 1000; %最高训练次数n Max no. of epochs to train
% net.trainParam.goal = 0.01; %目标精度e stop training if the error goal hit
%% 循环试验模型精度选取精度较高的模型
Accuracy = 0.0;
while (Accuracy < 0.85)
%% 训练神经网络
% [net,tr] = train(net,trainV.P,trainV.T);
%% 训练RBF-ANN神经网络
Q = size(trainV.P,2);
% [net,tr] = newrb(trainV.P,trainV.T,0.01,0.9,Q,1); %Design radial basis network
[net,tr] = newrb(trainV.P,trainV.T,0.01);
%% 数据预测
T_predicted = sim(net,valV.P);
%% 预测结果化整
[Max Index] = max(T_predicted);
for j = 1:size(T_predicted,2)
T_predicted(:,j) = 0;
T_predicted(Index(j),j) = 1;
end
%% 准确率计算
delta = any(valV.T - T_predicted);
acc_num = 0;
for i=1:length(delta)
if delta(i)==0
acc_num = acc_num + 1;
end
end
Accuracy = acc_num/length(delta)
end
%% 保存网络
% save('net.mat','net');
%% 中间变量释放
clear Image P_scale T
%% 对整幅图像进行预测
T_predicted=sim(net,testV.P);
% test1 = testV.P(:,1:100000);
% T_predicted1 = sim(net,test1);
% test2 = testV.P(:,100001:200000);
% T_predicted2 = sim(net,test2);
% test3 = testV.P(:,200001:300000);
% T_predicted3 = sim(net,test3);
% test4 = testV.P(:,300001:363699);
% T_predicted4 = sim(net,test4);
% T_predicted=[T_predicted1,T_predicted2,T_predicted3,T_predicted4];
%% 预测结果化整
[Max Index] = max(T_predicted);
for j = 1:size(T_predicted,2)
T_predicted(:,j) = 0;
T_predicted(Index(j),j) = 1;
end
%% 获得分类结果图像数据
pn2(1:line,1:sample,1:3)=0;
I = zeros(size(T_predicted,2),1);
image = zeros(line,sample);
for i=1:size(T_predicted,2)
I(i) = find(T_predicted(:,i)==1);
end
for i=1:line
for j=1:sample
image(i,j)=I((i-1)*line+j);
pn2(i,j,1)=colorset(image(i,j),1);
pn2(i,j,2)=colorset(image(i,j),2);
pn2(i,j,3)=colorset(image(i,j),3);
end
end
imshow(pn2);
imwrite(pn2,'F:\output.tif');
% dlmwrite('RBF-ANN_asc.txt',image);%, '-append','delimiter', '\t', 'precision', '%10.6f','newline', 'pc'
- 1
- 2
- 3
- 4
- 5
- 6
前往页