从Matlab中的体积数据生成scatter3()的参数

时间:2014-04-23 19:41:40

标签: matlab

我曾经使用isosurface()渲染我的体积数据,但现在我想将它们渲染为点以加快速度。

我拥有的数据是一个3D数组,代表一个3D对象。如果体素属于此对象,则其值为1,否则为零。

为了使用scatter3(),我需要为那些值为1的体素生成坐标。我目前使用以下代码来完成这项工作:

function [ x, y, z ] = scatter3_assist( volume )
[R, C, D] = size(volume);
x = zeros( size(volume(:)) );
y = x;
z = x;
idx = 1;
for d=1:D
  for r=1:R
    for c=1:C
      if volume(r, c, d) == 0
        x(idx) = 0; y(idx) = 0; z(idx) = 0;
      else
        x(idx) = C - c + 1; y(idx) = R - r + 1; z(idx) = d;
      end
      idx = idx + 1;
    end
  end
end
x(x==0) = [];
y(y==0) = [];
z(z==0) = [];
x = x - 1;
y = y - 1;
z = z - 1;
end

返回值x,y,z是属于我的对象的体素的坐标,然后我调用scatter3(x, y, z, '*');来渲染它。

是否有更有效的方法为scatter3()使用的特定体素生成坐标?

1 个答案:

答案 0 :(得分:1)

我建议使用find查找数组中非零条目的线性索引,然后使用ind2sub转换为索引并执行获取所需的任何转换xyz。类似的东西:

I = find(volume ~= 0);
[y, x, z] = ind2sub(size(volume),I); %// note that x and y are switched as in your code above
x = size(volume,2)-x;
y = size(volume,1)-y;

您需要仔细检查xy上的这些操作,以确保它们与您的代码相同。