matlab遥感图像分类
遥感图片的分类对于区域的分类显得尤为重要。可以分块显示,对于区域的划分显得尤其重要!
一、参考代码
clear all;close all;clc;
%读入图像
Image=imread('test.tif');
Image=Image(2:(size(Image,1)-1),2:(size(Image,2)-1),:);
Ls_Image=Linear_stretch(Image,8);%线性灰度拉伸
Im=double(Image)/255;
nums=size(Im,3);%波段数
%读入样本
snum=14;%样本数
cli_list=1:14;%各样本对应初分类别号
for i=1:snum
????eval(['sss{i}=double(imread(''s',num2str(i),'.tif''));']);%样本文件名:si.bmp,i=1,2,3...
????if size(sss{i},1)<=2 || size(sss{i},1)<=2
????????error('第%d个样本图太小!',i);
????end
????s{i}=sss{i}(2:(size(sss{i},1)-1),2:(size(sss{i},2)-1),:);
end
%初分类别各参数计算
numcli=length(unique(cli_list));%初分类别数
%当类标不是连续自然数时,修改为从1开始的连续自然数编号
cli_sort=sort(unique(cli_list));
for i=1:numcli
????ff=find(cli_list==cli_sort(i));
????clilist(ff)=i;%整理好的连续自然数类别表
end
%ELM分类
sample_s=[];
label=[];
for i=1:snum
????for j1=1:size(s{i},1)
????????for j2=1:size(s{i},2)
????????????????
????????end
????end
end
sample_s=double(sparse(sample_s))/255;
x=sample_s;
%重新编码
la_temp=label;
for i=1:ceil(log(snum)/log(2))
????y(:,i)=mod(la_temp,2);
????la_temp=floor(la_temp/2);
end
y=sign(y-0.5);
nump=size(y,1);
ELM=ELM_train(y,x,300);
%待分类原图,数据变形
Cmap=zeros(size(Im,1),size(Im,2));
for i=1:size(Im,2)
????Pclass=ELM_classify(reshape(Im(:,i,:),size(Im,1),nums),ELM);
????for j=1:1:ceil(log(snum)/log(2))
????????la_temp=(sign(Pclass)+1)*0.5;
????????Cmap(:,i)=Cmap(:,i)+la_temp(:,j)*2^(j-1);
????end
end
Cmap(find(Cmap>snum))=snum;
Cmap(find(Cmap<1))=1;
Image_cli=mat2gray(Cmap);
%制作配色表
for i=1:numcli
????map(i,1)=i;
????map(i,2)=floor(abs(i-0.5*numcli)*2);
????map(i,3)=numcli-i+1;
end
map=map./max(max(map));
Image_cli_c=grayslice(Image_cli,numcli);
%类别合并,以下内容在初次分类完成后,给定参数
?????????%1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
clf_list=[1 1 2 2 2 2 3 3 4 5 5 5 6 6];%对初分类标进行合并,各类标对应的最终类标(连续自然数);
numclf=length(unique(clf_list));
for i=1:size(Cmap,1)
????for j=1:size(Cmap,2)
????????Im_clf(i,j)=clf_list(Cmap(i,j));%最终分类矩阵
????end
end
fprintf('\n最终分类完成。');
Image_clf=mat2gray(Im_clf);
for i=1:numclf
????mapf(i,1)=i;
????mapf(i,2)=floor(abs(i-0.5*numclf)*2);
????mapf(i,3)=numclf-i+1;
end
mapf=mapf./max(max(mapf));
Image_clf_c=grayslice(Image_clf,numclf);
figure();
imshow(Image);
title('待分类原始图像');
figure();
imshow(Ls_Image)
title('待分类图像(线性拉伸)');
figure();
imshow(Image_cli_c,map);
title('初次分类结果图');
figure();
imshow(Image_clf_c,mapf);
title('最终分类结果图');
ill=ones(50,150);
illu=ill;
if(numclf>1)
for i=2:numclf
????illu=[illu;ill*i];
end
end
figure();
imshow(illu,mapf);
ylabel(' ?6 ??????????5 ??????????4 ??????????3 ??????????2 ??????????1');
title('各类颜色图例');
?二、运行参考图
?
????
????????
????????
|