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

조회 수: 5 (최근 30일)
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)
Warning: Duplicate data points have been detected and removed.
poly =
alphaShape with properties: Points: [9268×3 double] Alpha: 4.0566 HoleThreshold: 0 RegionThreshold: 0
[bf, P] = boundaryFacets(poly);
TR=triangulation(bf,P)
TR =
triangulation with properties: Points: [9268×3 double] ConnectivityList: [18540×3 double]
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)

답변 (1개)

yanqi liu
yanqi liu 2022년 1월 22일
yes,sir,may be use
f is Faces,v is Vertices
tr2 = triangulation(f,v);
stlwrite(tr2,'demo.stl');

카테고리

Help CenterFile Exchange에서 Triangulation Representation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by