STL to binary mask conversion
35 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Hitesh
2024 年 11 月 29 日 6:27
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:
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!