I have 2D .jpg images which I want to convert into 3D.stl files with same thickness throughout (eg two Z values) in the created .stl file
13 views (last 30 days)
Show older comments
1)I already have a code which converts a 2D .jpg (MATLAB_Example.jpg) image into a 2D .stl file (MATLAB_ANSWERS_IMG2STL_EXACT.m) but when I open this .stl file created by the code in 3D printing software like cura i am not able extrude the z axis since its a single layer of x(i),y(i) points. I would really appreciate if someone can guide me on what approach is needed in order to add another layer with same x y coordinates but a new Z (for the third dimension)?
% MATLAB_ANSWERS_IMG2STL_EXACT.m
% parameters
prescale = 0.1; % this scales the contours prior to simplification
% 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
% create the 2d triangulation
% for each contour, prepare the polygons
for i=1:length(c_cell)
c_tmp = c_cell{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')
end of 1st part
2)I tried it with alphaShape functionand gave two values for Z(1 and -1) in the (MATLAB_ANSWERS_IMG2STL_3D.m) code and I was able to obtain the 3D structure (with same thickness in the entire structure as i need) but it does not look like the .jpg input image (there is something which is wrong with the code)
%MATLAB_ANSWERS_IMG2STL_3D.m
% 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
% create the 2d triangulation
% for each contour, prepare the polygons
for i=1:length(c_cell)
c_tmp = c_cell{i};
x_vec{i} = c_tmp(:,1).';
y_vec{i} = c_tmp(:,2).';
end
% get the polygon and make the triangulation
for i=1:size(x_vec,2)
polyXYZsecond{i} =[x_vec{i}.', y_vec{i}.',-ones(1,size(x_vec{i},2)).'];
polyXYZfirst{i} =[x_vec{i}.', y_vec{i}.',ones(1,size(x_vec{i},2)).'];
end
allXYZcell=[polyXYZfirst,polyXYZsecond];
for i=1:2*size(x_vec,2)
polyXall{i}=allXYZcell{1,i}(1:end,1);
end
for i=1:2*size(x_vec,2)
polyYall{i}=allXYZcell{1,i}(1:end,2);
end
for i=1:2*size(x_vec,2)
polyZall{i}=allXYZcell{1,i}(1:end,3);
end
Xallvalues= cat(1, polyXall{:});
Yallvalues= cat(1, polyYall{:});
Zallvalues= cat(1, polyZall{:});
XYZ=[Xallvalues,Yallvalues,Zallvalues];
poly=alphaShape(XYZ)
[bf, P] = boundaryFacets(poly);
TR=triangulation(bf,P)
triplot(TR)
axis equal
stlwrite(TR,'mything.stl','text')
I would really appreciate if someone can help me with this issue. (Please see the attachments for clarity)
0 Comments
Answers (1)
yanqi liu
on 22 Jan 2022
yes,sir,may be use
f is Faces,v is Vertices
tr2 = triangulation(f,v);
stlwrite(tr2,'demo.stl');
0 Comments
See Also
Categories
Find more on Triangulation Representation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!