3D Binary Matrix / Surface Mesh Image in Matlab

How can I create a 3D binary matrix / image from a surface mesh in Matlab?

For example, when I create an ellipsoid using:

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

X, Y, and X are all 31 x 31 two-dimensional matrices.

Edited based on @Magla's suggestion:

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
source share
2 answers

Here is a way to create a binary mask from an ellipsoid. He creates the corresponding volume and sets it to NaNpoints outside the ellipsoid ( onesinside).

, . , , 3D- . convexhulln , .

enter image description here

%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)

, , &.

"" - .

+2

:

%// 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) 1, 0 .

: :

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

enter image description here

+4

All Articles