这个函数原理有点类似于中值滤波之类的,小模板取均值来搞的,当然也可以选用其他策略,需要在函数set_Point
里面改。
1、全代码:
%% 填补 tianchong.m
function [mat]=tianchong(SST,SST1)
%% 这个函数用于对有缺失区域的数据进行填充
% 输入:
% SST 待填补的灰度矩阵
% SST1 模板灰度矩阵
% t 可调参数,小模板大小
% 输出:
% mat 填充之后的灰度矩阵
%-------------------------------------------------------------------
%%%% Authors: Bill O'Hanlon
%%%% EMAIL: ohanlon@qq.com
%%%% DATE: 30-10-2020
t=10;
[a,b]=size(SST);
for i=1:a
for j=1:b
if isnan(SST1(i,j))
SST(i,j)=nan;
else if isnan(SST(i,j))
a=set_Point(SST,i,j,t);
m=i;
while isnan(a)
a=set_Point(SST,m+1,j,t);
m=m+1;
end
SST(i,j)=a;
end
end
end
end
mat=SST;
end
%% 填补子函数
function [dc]=set_Point(SST,i,j,t)
%% 这个函数用于获取SST(i,j)的灰度值
% 输入:
% SST 待填补的灰度矩阵
% i 行数
% j 列数
% t 小模板大小 (2*t+1)x(2*t+1)
% 输出:
% dc 经计算得到的灰度值
%-------------------------------------------------------------------
%%%% Authors: Bill O'Hanlon
%%%% EMAIL: ohanlon@qq.com
%%%% DATE: 30-10-2020
[a,b]=size(SST);
m=2*t+1; %矩阵大小
if i-t<1
if j-t<1
mat=SST(1:m,1:m);
else if j+t>b
mat=SST(1:m,b-m+1:b);
else
mat=SST(1:m,j-t:j+t);
end
end
else if i+t>a
if j-t<1
mat=SST(a-m+1:a,1:m);
else if j+t>b
mat=SST(a-m+1:a,b-m+1:b);
else
mat=SST(a-m+1:a,j-t:j+t);
end
end
else
if j-t<1
mat=SST(i-t:i+t,1:m);
else if j+t>b
mat=SST(i-t:i+t,b-m+1:b);
else
mat=SST(i-t:i+t,j-t:j+t);
end
end
end
end
dc=nanmean(nanmean(mat));
end
2、调用实例
clc
clear
filepath='D:\matlab\mat\CHLA\';
file1=[filepath,'sorce\chla_201001.mat'];
file2=[filepath,'2010\CHLA201001.mat'];
load(file1);
load(file2);
figure
contourf(chla_201001,'LineStyle','none');colormap(jet);colorbar
newCHLA=tianchong(chla_201001,CHLA);
figure
contourf(CHLA,'LineStyle','none');colormap(jet);colorbar
figure
contourf(newCHLA,'LineStyle','none');colormap(jet);colorbar
结果: