-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtwoDsmooth.m
37 lines (37 loc) · 1.09 KB
/
twoDsmooth.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function smat=twoDsmooth(mat,ker)
%TWO2SMOOTH?Smooth 2D matrix.
%
%smat=twoDsmooth(mat,ker)
%
%MAT is the 2D matrix to be smoothed.
%KER is either
%(1)a scalar,in which case a ker?by?ker matrix of 1/ker?2 is used
% as the matrix averaging kernel
%(2)a matrix which is used as the averaging kernel.
%
%SMATisthesmoothedmatrix(samesizeasmat).
if numel(ker)==1,%prod(size(ker))==1 if ker is a scalar
kmat=ones(ker,ker)/ker^2;
else
kmat=ker;
end
%make kmat have odd dimensions
[kr kc]=size(kmat);
if rem(kr,2)==0, %Remainder after division
kmat=conv2(kmat,ones(2,1))/2;
kr=kr+1;
end
if rem(kc,2)==0,
kmat=conv2(kmat,ones(1,2))/2;
kc=kc+1;
end
[mr mc]=size(mat);
fkr=floor(kr/2);%number of rows to copy on top and bottom
fkc=floor(kc/2);%number of columns to copy on either side
smat=conv2(...
[mat(1,1)*ones(fkr,fkc) ones(fkr,1)*mat(1,:)...
mat(1,mc)*ones(fkr,fkc);
mat(:,1)*ones(1,fkc) mat mat(:,mc)*ones(1,fkc)
mat(mr,1)*ones(fkr,fkc) ones(fkr,1)*mat(mr,:)...
mat(mr,mc)*ones(fkr,fkc)],...
rot90(kmat,2),'valid'); %flipud(fliplr(kmat)) replaced by rot90(kmat,2)