function [mat] = WP_remove_Inf_NaNs(rgbd)

[N,M,D] = size(rgbd);
mat = rgbd;
K = 3;

for n=1:N
    for m=1:M
        v = reshape(rgbd(n,m,:),[1,D]);
        if(max(isnan(v))==1 | max(isinf(v))==1)
            r_min = max(1,n-K);
            r_max = min(N,n+K);
            c_min = max(1,m-K);
            c_max = min(M,m+K);
            W= rgbd(r_min:r_max,c_min:c_max,:);
            [r,c]=find(~isnan(W(:,:,1)) & ~isinf(W(:,:,1)) & ~isnan(W(:,:,2)) ...
                & ~isinf(W(:,:,2)) & ~isnan(W(:,:,3)) & ~isinf(W(:,:,3)));
            try
            mat(n,m,:) = W(r(1),c(1),:);
            catch err
                tch = 0;
            end
        end
    end
end
end