I have 2d images in JPEG format created by matlab program and I want to convert them into .STL files using matlab code

65 views (last 30 days)
What is the approach needed in Matlab to convert 2d image in JPEG format into a simple .STL file (2D) for 3D printing ? Which function can I use to automate the conversion of images from jpeg to .STL?
For Example The output I got from Matlab code was the Image "MATLAB_Example.jpg" I want to extract the boundaries and convert it to .STL file to 3D print the Topology with a minumum thickness example 1mm looking exactly like the MATLAB_Example.jpg image. I can do this using the websites like https://anyconv.com/jpg-to-stl-converter/ (and obtain the boundaries eg. AnyConv.com__MATLAB_Example.stl) but I want to do it using Matlab as I have many images like this that I want to convert to .STL and print.
  5 Comments
Bashar Shami
Bashar Shami on 22 Jan 2022
Moved: DGM on 17 Jan 2024
Hello Airesh Kindly come back and talk again , your task is image processing task acquire segmentation of desired region mainly threshold segmentation then giving it dimension in z axis then triangulate for STL Facebook @Basharbme
Rik
Rik on 23 Jan 2022
Moved: DGM on 17 Jan 2024
How exactly is this an answer? Also, please only use flags to attract the attention of site admins.

Sign in to comment.

Accepted Answer

DGM
DGM on 16 Jan 2022
Well, here's a half-baked answer, but anyone is free to do better.
I just grabbed this mesh tool from the FEX and gutted it to avoid the PDE toolbox dependency and change the binarization behavior.
% parameters
prescale = 0.1; % this scales the contours prior to simplification
simplify_tol = 0.1;
% transform the image into binary
A = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/863735/MATLAB_Example.jpg');
A = ~imbinarize(rgb2gray(A));
A = bwareafilt(A,1);
% rotate coordinate
A = fliplr(A.');
% get the contours of the image
% find boundary
bnd = bwboundaries(A);
% parse the contours
for i=1:length(bnd)
bnd_tmp = bnd{i};
assert(all(bnd_tmp(1,:)==bnd_tmp(end,:)), 'contour is not closed')
c_cell{i} = prescale.*bnd_tmp;
end
% simplify all the contours
c_cell_out = {};
for i=1:length(c_cell)
c_tmp = c_cell{i};
[x_tmp, y_tmp] = reducem(c_tmp(:,1), c_tmp(:,2), simplify_tol);
if (nnz(x_tmp)>0)&&(nnz(y_tmp)>0)
c_cell_out{end+1} = [x_tmp, y_tmp];
end
end
% create the 2d triangulation
% for each contour, prepare the polygons
for i=1:length(c_cell_out)
c_tmp = c_cell_out{i};
x_vec{i} = c_tmp(:,1).';
y_vec{i} = c_tmp(:,2).';
end
% get the polygon and make the triangulation
poly = polyshape(x_vec, y_vec, 'Simplify', false);
tr = triangulation(poly);
triplot(tr)
axis equal
% write to stl
stlwrite(tr,'mything.stl','text')
Note that the part has arbitrary scale and offset. You'll have to figure out how you want to manage scale and offset, perhaps modifying the simplified contours prior to triangulation or something. The prescale parameter scales the contours prior to simplification and will change the number of vertices, so it's probably best treated as a simplification parameter rather than being used for scaling the output geometry.
  2 Comments
AARISH NAWAZ
AARISH NAWAZ on 16 Jan 2022
Edited: AARISH NAWAZ on 16 Jan 2022
Dear DGM, your solution rightly extracts the boundary of the contours and triangulates and writes the image to a file in .STL format. I tried to import the .STL file into a cura 3d printing software but since the z axis (thickness) is zero it shows an error and I am unable to use the .stl file for 3d printing. Can you duplicate the triangulation values with a few z values (to obtain a 3d object with minumum thickness) so that it has a thickness value not equal to zero.
(see images for clarification)
DGM
DGM on 16 Jan 2022
I'm sure it could be done, but like I said, I am totally unfamiliar with this process and how the extruded point list would need to be generated.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!