MATLAB将.mat矩阵写成.tif图片
close all;
clear;
clc;
format compact; %压缩空格
tic;
%% 读取mat矩阵,写成.tif文件
load('PaviaU_gt.mat');
a=size(paviaU_gt);
ground_truth=zeros(a(1,1),a(1,2),3);
for i=1:a(1,1)
for j=1:a(1,2)
if paviaU_gt(i,j)==0
ground_truth(i,j,1)=0;
ground_truth(i,j,2)=0;
ground_truth(i,j,3)=0;
elseif paviaU_gt(i,j)==1
ground_truth(i,j,1)=255;
ground_truth(i,j,2)=0;
ground_truth(i,j,3)=0;
elseif paviaU_gt(i,j)==2
ground_truth(i,j,1)=0;
ground_truth(i,j,2)=255;
ground_truth(i,j,3)=0;
elseif paviaU_gt(i,j)==3
ground_truth(i,j,1)=0;
ground_truth(i,j,2)=0;
ground_truth(i,j,3)=255;
elseif paviaU_gt(i,j)==4
ground_truth(i,j,1)=0;
ground_truth(i,j,2)=255;
ground_truth(i,j,3)=255;
elseif paviaU_gt(i,j)==5
ground_truth(i,j,1)=255;
ground_truth(i,j,2)=0;
ground_truth(i,j,3)=255;
elseif paviaU_gt(i,j)==6
ground_truth(i,j,1)=255;
ground_truth(i,j,2)=255;
ground_truth(i,j,3)=0;
elseif paviaU_gt(i,j)==7
ground_truth(i,j,1)=100;
ground_truth(i,j,2)=100;
ground_truth(i,j,3)=0;
elseif paviaU_gt(i,j)==8
ground_truth(i,j,1)=100;
ground_truth(i,j,2)=0;
ground_truth(i,j,3)=100;
elseif paviaU_gt(i,j)==9
ground_truth(i,j,1)=0;
ground_truth(i,j,2)=100;
ground_truth(i,j,3)=100;
end
end
end
% figure;
% imshow(ground_truth);
imwrite(ground_truth,'./pic/ground_truth.tif');
load('PaviaU.mat');
mm = max(max(max(paviaU)));
paviaU=paviaU/mm*255;
b=size(paviaU);
for k=1:b(1,3)
result=zeros(b(1,1),b(1,2));
result=uint8(paviaU(:,:,k));
names_save=['./pic/src/p#_',num2str(k),'.tif'];
imwrite(result,char(names_save));
end
toc;