八度的三维 bwdist 函数

Threedimensional bwdist function for octave

Image Package from octave offers a bwdist function which is different to Matlab's bwdist(Link) 因为它只适用于二维矩阵而不适用于三维矩阵。

有没有类似Matlabs的函数bwdist也可以处理三维矩阵的函数?

没有,自己写一个,例如

function Out = bwdist3D (Obj)
  Obj        = logical (Obj);
  ObjSize    = size (Obj);
  ObjIndices = find (Obj == true);
  [X, Y, Z] = ndgrid (1 : ObjSize(1), 1 : ObjSize(2), 1 : ObjSize(3)); 

  Out = zeros ([ObjSize, length(ObjIndices)]);
  for Iter = 1 : length (ObjIndices)
    [x, y, z] = ind2sub (ObjSize, ObjIndices(Iter));
    Out(:, :, :, Iter) = sqrt ((X - x).^2 + (Y - y).^2 + (Z - z).^2 );      
  end  

  Out = min (Out, [], 4);
end

唯一的缺点是它不会那么优化,但它应该可以解决问题。
PS:注意这仅供参考;显然,上述功能缺少所有必要的输入检查/消毒,任何适当的功能都应该具备。


编辑:上述的完全矢量化版本

function Out = bwdist3D (Obj)
  [X, Y, Z] = ndgrid (1 : size (Obj, 1), 1 : size (Obj, 2), 1 : size (Obj, 3)); 
  [x, y, z] = ind2sub (size(Obj), reshape(find(Obj>0), [1,1,1,sum(Obj(:)>0)]));
  Out = min (sqrt((X-x).^2 + (Y-y).^2 + (Z-z).^2), [] ,4);
end

PS:最好的代码高尔夫 :p