
STL to binary mask conversion
11 views (last 30 days)
Show older comments
I have an STL file and I would like to make a binary mask from it in MATLAB, that would be defined similarly to this:
stl_mask = zeros(size(stl_file));
stl_mask(stl_indices) = 1
I have searched around and there is a stlread function that lets me visualize the STL file in MATLAB, but I am a bit lost on how to work around it. This is the STL file that I'm using. For the moment, I have just this small code to visualize the STL file:
clearvars;
filename = 'example.STL';
[v, f, n, c, stltitle] = stlread(filename);
figure;
patch('Faces',f,'Vertices',v,'FaceVertexCData',c);
grid on; view(45, 25); daspect([1 1 1]); camlight(-30,-30);
figure;
plot3(v(:,1),v(:,2),v(:,3),'.');
I have also checked the stl_slice_and_plot function from https://es.mathworks.com/matlabcentral/fileexchange/62113-slice_stl_create_path-triangles-slice_height . It gives a nice plot of the contours, but again, I would need some help on how to convert the contour plot into (x y z) coordinates to generate a mask:
%this script shows how everything works together
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Sunil Bhandari
%3/17/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc; clear vars; close all;
triangles = read_binary_stl_file('example.STL');
original = triangles;
triangles = rotate_stl(triangles,'y',90);
slice_height = .1;
tic;[movelist, z_slices] = slice_stl_create_path(triangles, slice_height);toc;
%'plotting'
plot_slices(movelist,z_slices, 0)
Thank you
0 Comments
Accepted Answer
Hitesh
on 29 Nov 2024
Hi Nicolás,
You need to add "inpolyhedron" file exchange for checking whether the points are inside a 3D triangulated (faces/vertices) surface. Kindly refer to the below code ofr generating the binary mask.
filename = 'example.STL';
[TR,fileformat,attributes,solidID] = stlread(filename);
addpath('C:\Users\hiteshk\Downloads\inpolyhedron');
% Define the grid resolution and limits
gridSize = 100; % Adjust the resolution as needed
xRange = [min(TR.Points(:,1)), max(TR.Points(:,1))];
yRange = [min(TR.Points(:,2)), max(TR.Points(:,2))];
zRange = [min(TR.Points(:,3)), max(TR.Points(:,3))];
[xGrid, yGrid, zGrid] = ndgrid(...
linspace(xRange(1), xRange(2), gridSize), ...
linspace(yRange(1), yRange(2), gridSize), ...
linspace(zRange(1), zRange(2), gridSize));
% Initialize the binary mask
binaryMask = false(size(xGrid));
% Use inpolyhedron or similar function to fill the binary mask
binaryMask = inpolyhedron(TR.ConnectivityList, TR.Points, [xGrid(:), yGrid(:), zGrid(:)]);
% Reshape the binary mask
binaryMask = reshape(binaryMask, size(xGrid))
You need to "trisurf" function to visualize the STL using the triangulation object. Kindly refer to the below code for visualazing the STL file.
% Create a figure to visualize the STL using the triangulation object
figure;
trisurf(TR, 'FaceColor', 'cyan', 'EdgeColor', 'none');
title('STL Visualization');
xlabel('X');
ylabel('Y');
zlabel('Z');
grid on;
axis equal;
view(45, 25);
camlight('headlight');
lighting gouraud;

For more information regarding the "inpolyhedron" and "stlread", kindly refer to the below MATLAB documentation:
1 Comment
Toshini
on 19 Mar 2025
Edited: Toshini
on 19 Mar 2025
Hi Hitesh,
I'm trying to create binary mask for .stl files of segemented organs. With fat and muscle data of size (3116232x3 double) and (1521380x3 double) respectively, the inpolyhedron function doesn't work and shows an error saying it goes beyond 15 GB. So, I try to reduce their size. Which works fine, but on doing so a large amount of data is cropped out. Your suggestions would be valuable.
vertices = triangulationData.Points;
faces = triangulationData.ConnectivityList;
if size(vertices, 1) > 1e6 || size(faces, 1) > 1e6
[reducedFaces, reducedVertices] = reducepatch(struct('faces', faces, 'vertices', vertices), 5);
points = reducedVertices;
faces = reducedFaces;
else
points = vertices;
faces = faces;
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!