好久没用MATLAB了,不少基础的还要查一下以运用,因为学得快,忘得也很快,所以最好的方式找个地写下来。这里对遥感影像的预测处理主要涉及影像的归一化(线性归一化和标准差归一化)、影像裁剪、影像写出(写出含负数的浮点型数据的矩阵)。
总结几点,另附有关代码。
几点总结:
一、MATLAB下字符的常用操作
1)拼接几个字符串
data_path = 'C:\Users\zl\Desktop\example\';
filepath='top_mosaic_09cm_area1.tif'; %图像名称与路径
strcat(data_path, filepath); %%拼接字符串-方法一
[data_path filepath];或者[data_path,filepath] %利用矩阵的思想拼接字符
2)打印变量
disp(strcat(data_path, filepath)); %打印方式一
strcat(data_path, filepath) %打印方式二 注意不要“;”,这样直接在命令行窗口输出,更便捷
二、对tif格式的影像使用MATLAB读进时,tif格式是一种无压缩的影像数据保存格式,可以是一张也可以是几张影像存放在一起,形成‘假象’的一张张影像
1)对于不清tif格式的影像时,读取方式:
data_path = 'C:\Users\zl\Desktop\example\';
filepath='top_mosaic_09cm_area1.tif'; %%图像名称与路径
%disp(strcat(data_path, filepath)) %%拼接字符串和打印路径
Info=imfinfo(strcat(data_path, filepath)); %%获取图片信息并判断是否为tif
tif='tif';
format=Info.Format;
if (strcmp(format ,tif)==0)
disp('载入的不是tif图像,请确认载入的数据'); %%确保载入的图像是tiff图像
end
Slice=size(Info,1); %%获取图片z向帧数
band = Info.BitDepth ;Width=Info.Width;
Height=Info.Height;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Image=zeros(Height,Width,Slice);
for i=1:Slice
Image(:,:,i)=imread('C:\Users\zl\Desktop\example\dsm_09cm_matching_area1.tif',i); %%一层一层的读入图像
end
2)知道tif的‘内幕’后,直接使用imread函数进行读取
image = imread('C:\Users\zl\Desktop\example\dsm_09cm_matching_area1.tif');
三、矩阵的一些操作
因为在使用Python,而这与MATLAB的对矩阵一些操作还是有很多的不同的。
1)length(name_add) %获取长度的函数
2)name_add(2,3) %索引方式
3)获取几张影像中的最大值或最小值,对组合为一列的dem_all求max(),min()即可
% dem_all = [];
% for i=1:length(name_add)
% name_full = [name_part,num2str(name_add(i)),'.tif']; %拼接影像文件名和扩展名
% image = imread([data_path,name_full]);
% a = image(:);
% dem_all = [dem_all;a] ;
% end
4)几个函数,mean(),std()计算均值和标准差
5)保存含有负数的浮点数据时,使用dlmwrite以txt格式的方式保存更好,如何用imwrite函数储存为tif会自动转为uint8格式,同时自动归一化0-255这样存在精度损失
有关的代码
%##########裁剪为512*512的单张影像的均值和标准差的归一化##########
%MATLAB实现裁剪,裁剪为512*512的,不足的补0;计算均值和标准差时时不考虑0值;写出矩阵;
name_part = 'dsm_09cm_matching_area';
data_path = 'C:\Users\dl\Desktop\data\ISPRS_semantic_labeling_Vaihingen\dsm\';
name_add = [1,3,5,7,13,17,21,23,26,32,37]; %,11,15,28,30,34
num=0;
for i=1:length(name_add)
name_full = [name_part,num2str(name_add(i)),'.tif']; %拼接影像文件名和扩展名
img = imread([data_path name_full]);
[h, w] = size(img);
for m=1:ceil(h/512) % ceil()向上取整函数\
for n=1:ceil(w/512)
target_img = zeros([512,512]);
if m>floor(h/512)&&n>floor(w/512) % matlab 逻辑用语https://blog.csdn.net/jiejianmin2666/article/details/78148533
target_img(1:h-floor(h/512)*512,1:w-floor(w/512)*512) = img(floor(h/512)*512+1:end,floor(w/512)*512+1:end);
p_img = img(floor(h/512)*512:end,floor(w/512)*512:end);
loc_mean = mean(p_img(:));
loc_std = std(p_img(:));
elseif m>floor(h/512)&&n<=floor(w/512) %取等号要考虑,等于次最大
target_img(1:h-floor(h/512)*512,1:512) = img(floor(h/512)*512+1:end,(n-1)*512+1:n*512);
p_img = img(floor(h/512)*512:end,(n-1)*512+1:n*512);
loc_mean = mean(p_img(:));
loc_std = std(p_img(:));
elseif (n>floor(w/512))&&(m<=floor(h/512))
target_img(1:512, 1:w-floor(w/512)*512) = img((m-1)*512+1:m*512,floor(w/512)*512+1:end);
p_img = img((m-1)*512+1:m*512,floor(w/512)*512:end);
loc_mean = mean(p_img(:));
loc_std = std(p_img(:));
else
target_img = img((m-1)*512+1:m*512,(n-1)*512+1:n*512);
p_img = img((m-1)*512+1:m*512,(n-1)*512+1:n*512);
loc_mean = mean(p_img(:));
loc_std = std(p_img(:));
end
target_img=(target_img-loc_mean)/loc_std;
txt_name = ['C:\Users\dl\Desktop\data_process\save_gs_nl_dem\dsm_txt\','x',num2str(num),'.txt'];
dlmwrite(txt_name,target_img,'delimiter','\t','precision','%.6f');
% imwrite(double(target_img),['C:\Users\dl\Desktop\data_process\save_gs_nl_dem\dsm\','x',num2str(num),'.tif']);
num = num+1
end
end
% i
end