4

如何从 Matlab 中的表面网格创建 3D 二进制矩阵/图像?

例如,当我使用以下方法创建椭球时:

[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);

X、Y 和 X 都是 2D 矩阵,大小为 31 x 31。

根据@Magla 的建议编辑:

function Create_Mask_Basedon_Ellapsoid3()
         close all
        SurroundingVol  = [50, 50, 20];
        %DATA
        [MatX,MatY,MatZ] = meshgrid(-24:1:25, -24:1:25, -9:1:10);
        [mask1, x, y, z] = DrawEllipsoid([0, -10, 0], [6, 3, 3], MatX,MatY,MatZ);
        [mask2, x2, y2, z2] = DrawEllipsoid([15, 14, 6], [6, 3, 3], MatX,MatY,MatZ);
        mask =  mask1 + mask2;

        %Surface PLOT
        figure('Color', 'w');
        subplot(1,2,1);

        %help: Ideally I would like to generate surf plot directly from combined mask= mask1 + mask2;
        s = surf(x,y,z); hold on;
        s2 = surf(x2,y2,z2); hold off;     
        title('SURFACE', 'FontSize', 16);
        view(-78,22)

        subplot(1,2,2);
        xslice = median(MatX(:));
        yslice = median(MatY(:));
        zslice = median(MatZ(:));

        %help: Also how do I decide correct "slice" and angles to 3D visualization.
        h = slice(MatX, MatY, MatZ, double(mask), xslice, yslice, zslice)
        title('BINARY MASK - SLICE VOLUME', 'FontSize', 16);
        set(h, 'EdgeColor','none');   
        view(-78, 22)
        %az = 0; el = 90;
        %view(az, el);

    end

    function [mask, Ellipsoid_x, Ellipsoid_y, Ellipsoid_z] = DrawEllipsoid(CenterEllipsoid, SizeEllipsoid, MatX, MatY, MatZ)
        [Ellipsoid_x, Ellipsoid_y, Ellipsoid_z] =  ellipsoid(CenterEllipsoid(1), CenterEllipsoid(2), CenterEllipsoid(3), SizeEllipsoid(1)/2 , SizeEllipsoid(2)/2 , SizeEllipsoid(3)/2 ,30);
        v = [Ellipsoid_x(:), Ellipsoid_y(:), Ellipsoid_z(:)];                       %3D points
        %v = [x(:), y(:), z(:)];                                                    %3D points
        tri = DelaunayTri(v);                                                       %triangulation
        SI = pointLocation(tri,MatX(:),MatY(:),MatZ(:));                            %index of simplex (returns NaN for all points outside the convex hull)
        mask = ~isnan(SI);                                                          %binary
        mask = reshape(mask,size(MatX));                                            %reshape the mask 
    end
4

2 回答 2

4

你去:

%// Points you want to test. Define as you need. This example uses a grid of 1e6
%// points on a cube of sides [-10,10]:
[x y z] = meshgrid(linspace(-10,10,100)); 
x = x(:);
y = y(:);
z = z(:); %// linearize

%// Ellipsoid data
center = [0 0 0]; %// center
semiaxes = [5 4 3]; %// semiaxes

%// Actual computation:
inner = (x-center(1)).^2/semiaxes(1).^2 ...
      + (y-center(2)).^2/semiaxes(2).^2 ...
      + (z-center(3)).^2/semiaxes(3).^2 <= 1;

对于n坐标为x(n)y(n)、的网格的第 - 个点z(n),如果该点位于椭球内部,则为 ,inner(n)否则为。10

例如:绘制内部点:

plot3(x(inner), y(inner), z(inner), '.' , 'markersize', .5)

在此处输入图像描述

于 2013-10-17T15:11:35.350 回答
3

这是一种从椭圆体创建二进制掩码的方法。它创建一个相应的体积并设置到NaN椭圆体外部(ones内部)的点。

它没有考虑椭球的公式,而是使用凸包。实际上,它适用于可以由 3D 凸包正确描述的任何体积。在这里,convexhulln由于椭圆体已经是凸包,因此绕过了该步骤。

所有学分都转到将凸包转换为二进制掩码

下面的情节

在此处输入图像描述

%DATA
[x, y, z] = ellipsoid(0,0,0,5.9,3.25,3.25,30);

%METHOD
v = [x(:), y(:), z(:)];                       %3D points
[X,Y,Z] = meshgrid(min(v(:)):0.1:max(v(:)));  %volume mesh
tri = DelaunayTri(v);                         %triangulation
SI = pointLocation(tri,X(:),Y(:),Z(:));       %index of simplex (returns NaN for all points outside the convex hull)
mask = ~isnan(SI);                            %binary
mask = reshape(mask,size(X));                 %reshape the mask 

%PLOT
figure('Color', 'w');
subplot(1,2,1);
s = surf(x,y,z);
title('SURFACE', 'FontSize', 16);
view(-78,22)
subplot(1,2,2);
xslice = median(X(:));
yslice = median(Y(:));
zslice = median(Z(:));
h = slice(X, Y, Z, double(mask), xslice, yslice, zslice)
title('BINARY MASK - SLICE VOLUME', 'FontSize', 16);
set(h, 'EdgeColor','none');
view(-78,22)

几个椭球体

如果您有多个椭球体,则可以对每个椭球体使用这种掩蔽方法,然后将生成的掩码与&.

切片和角度的选择

“正确”是个人选择的问题。你可以

  • 创建未旋转的蒙版并在之后旋转它(在 matlab 中旋转 3D 数组)。
  • 在已经旋转的椭球上创建一个蒙版。
  • 在稍微旋转的椭圆体上创建一个蒙版(让您可以选择“正确”切片),然后将其进一步旋转到最终位置。
于 2013-10-17T20:15:36.880 回答