Plotting 3D surface using Depth map and grey scale Image.
44 views (last 30 days)
Show older comments
I have 2 matrices of size 115x115
First matrix : grey_scale_img - which has grey scale intensity value(0 to 256) of the image at each pixel location
Second matrix : depth_map - which has z cordinate value at each pixel location
I need to plot a 3D surface with height of the surface at the each location given by depth_map and the colour of the surface at each location given by grey_scale_img.
(If I use surf(depth_map), it plots a 3D surface but with some different colours. My requirement is to produce 3D surface as produced by the surf(depth_map), but with colour of the surface given by grey_scale_img)
Please help me with this.
Thanks in advance!
0 Comments
Accepted Answer
Bhanu Prakash
on 13 Mar 2023
Hi Dhinesh,
As per my understanding, you are trying to plot a 3D surface with the height of the surface given by the matrix “depth_map” and the colour of the surface given by the matrix “grey_scale_img”.
For this, you can use the function "surf(X,Y,Z,C)", where X and Y being the two 115x115 matrices, Z being the "depth_map" matrix and C (color) being the "grey_scale_img" matrix.
Please find the below attached documentation of the function "surf":
Hope this answer helps you.
Thanks,
Bhanu Prakash.
More Answers (1)
Hw
on 10 Jan 2024
Hello, Mabe im in wrong section if i am.. sorry!
I look for a code that can make a depthmap of a 2d image. So i can 3d print or cnc this image in 3d
Mabe i ask to much ibdont have anny experiance in matlabs so i need a full code and instruction.. (i feel so blond)
Mabe there is among you verry intelegent people someone who can help me.
Thanks for reading in advance
Greetz from holland
5 Comments
DGM
on 11 Jan 2024
Just to be extra clear, I hope you're not asking to fully reconstruct a 3D object from a single depth image. There's only enough information there to make a simple relief as in the other example.
DGM
on 17 Jan 2024
Edited: DGM
on 17 Jan 2024
I was bored, so here.
% the image is a crusty RGB JPG with annotation marks and a white BG
inpict = imread('lion.jpg');
inpict = im2gray(inpict);
% clean up the image to get rid of the marks and fix the background
mk = inpict<100;
mk = imfill(mk,'holes');
mk = bwareafilt(mk,1);
mk = imerode(mk,strel('disk',4)); % erode to make room for mask softening
mk = imgaussfilt(im2double(~mk),2); % soften the mask
outpict = im2double(inpict).*(1-mk); % fill the background
% get rid of the annotation mark in the center by inpainting
p = [760 406; 760 423; 775 423; 775 406];
r = poly2mask(p(:,1),p(:,2),size(mk,1),size(mk,2));
outpict = regionfill(outpict,r);
% reduce the image resolution
% this is just for sake of making the demo faster
% this doesn't dictate the physical size, just the resolution
% if the resolution is not reduced, and JPG images are used
% then you should consider doing some noise reduction filtering
outpict = imresize(outpict,0.3);
% make sure the image is clamped and full-scale
outpict = min(max(outpict,0),1);
outpict = mat2gray(outpict);
% show the cleaned image
imshow(outpict,'border','tight')
% object scale parameters (mm)
reliefheight = 10;
basethickness = 2;
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 = im2double(outpict)*reliefheight + basethickness; % scale z
% display the surface
figure
hs = surf(X,Y,Z); hold on
hs.EdgeColor = 'none';
hs.FaceColor = [1 1 1]*0.8;
followerlight(hs); % attached
% 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); % SEE FEX #42876
TR = triangulation(F,V); % complains, but i'm going to ignore that
% display it again
figure
hs = trisurf(TR);
hs.EdgeColor = 'none';
hs.FaceColor = [1 1 1]*0.8;
followerlight(hs);
% write it to a STL
stlwrite(TR,'mylionrelief.stl')
Then you can just throw the result in whatever application will use a STL.
I'm assuming this just goes straight to your slicer, since feeding the STL to Blender or something seems like a waste of time, since the entire job could probably be done in Blender (at least after cleaning up the image). For example, the solid model could be generated even with something as simple as OpenSCAD:
// THIS IS OPENSCAD, NOT MATLAB CODE
// the clean image size
szx = 455;
szy = 240;
// object scale parameters (mm)
reliefheight = 10;
basethickness = 2;
xwidth = 150; // y-width is calculated to preserve aspect ratio
// generate solid in the same location as before
ywidth = xwidth*szy/szx;
union(){
translate([0,0,basethickness])
scale([xwidth/szx,ywidth/szy,reliefheight/100])
surface(file = "cleanlion.png"); // the clean image
cube([xwidth,ywidth,basethickness]);
}
// render it to generate a mesh
render();
// now just export the STL from the toolbar or file menu
I'm a total novice in Blender, so don't ask me how to do the equivalent. I'm just saying that if you're going to use other modeling tools anyway, you're probably familiar enough with them to do part of the job without needing to figure out how to do the whole thing in MATLAB.
See the attached functions. Followerlight() imrproves interactive viewing of the solid model, but is kind of superfluous on the forum. Surf2solid() comes from the File Exchange.
See Also
Categories
Find more on Lighting, Transparency, and Shading in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!