New updated version with faster 3D median:
A=ones(14,14,9);
function C=fastmedian3D(A,nbd)
Author: Rowthu Vijayakrishna
Place: Dept. of Mathematics and Statistics, IIT Kanpur
Copyleft: Yes
Copyright: No
version: 2.0
[h w d]=size(A);
B=zeros((2*nbd+1)^3,h,w,d);
for j=-nbd:nbd
for k=-nbd:nbd
for l=-nbd:nbd
v=(l+nbd+1 +(k+nbd)*(2*nbd+1) +(j+nbd)*(2*nbd+1)^2 );
B(v,:,:,:)=shift(shift(shift(A,l,3),k,2),j,1);
endfor;
endfor;
endfor;
C=shiftdim(median(B));
####end of the function file
Old version:
Version 1.0
function C=fastmedian3D(A,nb)
[h w d]=size(A);
axlen=2*nb+1 ;
nb3d=zeros(axlen, axlen,axlen);
B=zeros( numel(nb3d),h,w,d );
tic
for j=1:h
for k=1:w
for l=1:d
nblen=(min(j+nb,h) - max(1,j-nb)+1) *(min(k+nb,w) - max(1,k-nb)+1) *(min(l+nb,d) - max(1,l-nb)+1) ;
B(1:nblen,j,k,l)=A( max(1,j-nb):min(j+nb,h),max(1, k-nb):min(k+nb,w) ,max(1,l-nb):min(l+nb,d) )(:);
endfor;
endfor;
endfor;
C=zeros(h,w,d);
C(:,:,:)=median(B)(1,:,:,:);
size(C)
toc
Still not the fastest
A=ones(14,14,9);
nb=1;
New Version 2.0:
Author: Rowthu Vijayakrishna
Place: Dept. of Mathematics and Statistics, IIT Kanpur
Copyleft: Yes
Copyright: No
version: 2.0
[h w d]=size(A);
B=zeros((2*nbd+1)^3,h,w,d);
for j=-nbd:nbd
for k=-nbd:nbd
for l=-nbd:nbd
v=(l+nbd+1 +(k+nbd)*(2*nbd+1) +(j+nbd)*(2*nbd+1)^2 );
B(v,:,:,:)=shift(shift(shift(A,l,3),k,2),j,1);
endfor;
endfor;
endfor;
C=shiftdim(median(B));
####end of the function file
Old version:
Version 1.0
function C=fastmedian3D(A,nb)
[h w d]=size(A);
axlen=2*nb+1 ;
nb3d=zeros(axlen, axlen,axlen);
B=zeros( numel(nb3d),h,w,d );
tic
for j=1:h
for k=1:w
for l=1:d
nblen=(min(j+nb,h) - max(1,j-nb)+1) *(min(k+nb,w) - max(1,k-nb)+1) *(min(l+nb,d) - max(1,l-nb)+1) ;
B(1:nblen,j,k,l)=A( max(1,j-nb):min(j+nb,h),max(1, k-nb):min(k+nb,w) ,max(1,l-nb):min(l+nb,d) )(:);
endfor;
endfor;
endfor;
C=zeros(h,w,d);
C(:,:,:)=median(B)(1,:,:,:);
size(C)
toc
Still not the fastest
కామెంట్లు లేవు:
కామెంట్ను పోస్ట్ చేయండి