Image 倍频程的三维BWDST函数

Image 倍频程的三维BWDST函数,image,octave,Image,Octave,from octave提供了一个bwdist函数,该函数不同于Matlab的bwdist(),因为它只适用于二维矩阵,而不适用于三维矩阵 是否有一个与Matlabsbwdist函数类似的函数也可以处理三维矩阵?没有,只需自己编写一个,例如 function Out = bwdist3D (Obj) Obj = logical (Obj); ObjSize = size (Obj); ObjIndices = find (Obj == true); [X, Y,

from octave提供了一个
bwdist
函数,该函数不同于Matlab的
bwdist
(),因为它只适用于二维矩阵,而不适用于三维矩阵


是否有一个与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
唯一的缺点是它不会被优化,但它应该做到这一点。
注:本文件仅供参考;显然,上述功能缺少任何适当功能应有的所有必要输入检查/消毒。
编辑:上述内容的完全矢量化版本

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:code golf at's best:p

不,自己写一个就行了,例如

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
唯一的缺点是它不会被优化,但它应该做到这一点。
注:本文件仅供参考;显然,上述功能缺少任何适当功能应有的所有必要输入检查/消毒。
编辑:上述内容的完全矢量化版本

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:code高尔夫最精彩的时刻:p