- Convert the gridded data into triangulated FV data either using surf2patch(...,'triangles). If you wanted to use FEX tools, use mesh2tri() (ca 2010), or surf2stl() (ca 2004) and reading the STL back from disk. Either that or just write two loops and do it the hard way.
- Get the open boundary of the surface. The triangulation() class wasn't introduced until R2013a, so we can't use that. You'd instead use TriRep() (R2009a), or you would have to find the edges yourself.
- Project the surface mesh onto your base elevation. Now you have a second set of vertices and faces. Flip the winding order on these faces.
- Triangulate the walls between the surface and base.
How can I turn a grayscale image into a .stl 3d file?
6 views (last 30 days)
Show older comments
I have an grayscale image, I'd like to export it as an .stl file for 3d printing using the function surf2stl. I have been trying to turn the image file, which is a two dimensional matrix, into a surface using the color values as a height. Using the function surfht, I can get the interpolate contour map I want to print, but it is only displayed and I can't use it in surf2stl, also the surf function returns a 1x1 value, which can't be right. Is anyone able to shed some light on what steps I should take to turn this image to a surface and then an stl, or what I am doing wrong?
Thanks!
0 Comments
Answers (1)
DGM
on 2 Apr 2025
Edited: DGM
on 3 Oct 2025 at 7:52
Step 0: start with a clean image at a decent resolution. We don't have that. We have a tiny ruined JPG screenshot.
% a tiny garbage JPG screenshot found in a dumpster
inpict = imread('cDszY.jpeg');
% scrape the grease and hair off the destroyed image
outpict = rgb2gray(inpict);
outpict = imcrop(outpict,[92.51 132.51 419.98 271.98]);
outpict = mat2gray(outpict);
% object scale parameters (mm)
reliefheight = 2;
basethickness = 5;
xwidth = 150; % y-width is calculated to preserve aspect ratio
% construct coordinate arrays
szi = size(outpict);
szo = xwidth*[szi(1)/szi(2) 1];
x = linspace(0,szo(2),szi(2));
y = linspace(szo(1),0,szi(1)); % shift origin to SW corner
[X Y] = meshgrid(x,y);
Z = outpict*reliefheight + basethickness; % scale z
% construct a closed surface
% surf2solid() outputs face/vertex lists, but the native stlwrite()
% only accepts triangulation objects, so convert it
[F V] = surf2solid(X,Y,Z,'elevation',0); % FEX #42876 (ca 2013)
% write the thing to a file
stlWrite('trashmap.stl',F,V) % FEX #20922 (ca 2008) (see also #51200)
% display it using patch()
patch('faces',F,'vertices',V,'facecolor','w','edgecolor','none');
view(3); camlight; view(-10,33)
axis equal; grid on
xlabel('X'); ylabel('Y'); zlabel('Z')
See also:
But wait. The comment says surf2solid() wasn't available until 2013! How would we loft the base without that? Same as the longer example here:
I'm not sure why it's necessary to print such a thing anyway, but on the bright side, it probably would have turned out like an unrecognizable lumpy brick of plastic waste anyway.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!