Perspective and STLs: how to create thinning edges along a 3D shape?
Show older comments
Hello kind people, I'm trying to create a program that uploads a STL image of a 3D object, have the user rotate the shape to whatever perspective, and then have it output a 2D image of that rotated shape. The catch here is that I want the lines closest to the viewpoint to be thick, and gradually thin out the farther away the edge is from the viewpoint.
I've put in a few basic GUI's guiding the user to upload the file and coded the lighting such that it would accomodate each time the user rotated the image.
I'm basically looking for a function to detect all vertices of the STL, detect the distance from the camera viewpoint, and then somehow create a thick to thin line gradient along the edges such that those edges closest to the camera are thickest and those farthest from the camera are thinnest.
%%Begin GUI
% --- Executes on button press in pushbutton1.
function x = Program()
Button1 = questdlg ('Specify image file?');
switch Button1
case 'Yes'
I = uigetfile ('*.stl*', 'Specify 3D image file');
figure
fv = stlread (I);
patch(fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceColor', [0.5,0.5,0.5], ...
'FaceLighting', 'flat', ...
'EdgeLighting', 'flat', ...
'AmbientStrength', 0.8);
h1 = rotate3d;
h1.ActionPostCallback = @onOrbit;
% Add a camera light, and tone down the specular highlighting
h2 = camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
view([-135 35]);
case 'No'
disp ('Operation terminated')
end
function onOrbit(obj,event_obj)
% obj handle to the figure that has been clicked on
% event_obj object containing struct of event data (same as the
% event data of the 'ActionPreCallback' callback)
camlight(h2,'left');
Button2 = questdlg ('Finalize image?');
switch Button2
case 'Yes'
%create function here that thins outermost edges to two pixels
%and maps the closest edges to ten pixels and gradually thins
%them out. The farther away from the camera the edges are, the
%thinner they should be. The edges closest to the camera face
%should be ten pixels and farthest away should be two pixels.
%This will probably require detecting all the vertices and
%defining vertices closest and farthest from the camera face.
%Then, defining edge thickness at intervals away from the
%camera face.
%capture 2d image of rotated 3d figure
fr=getframe(gcf); %need to capture frame of rotated image
%without axes
img=frame2im(fr);
imagesc(img); % <- or imagesc(ff.cdata);
assignin ('base', 'frameshot', img);
%Below is the code to thin a png image to one pixel:
% b= bwmorph(im2bw(-rgb2gray(im2double(frameshot))+1,0.5),'thin'...
% ,Inf);imagesc(frameshot)
disp ('Image finalized')
%Below is the code to enter in an image name and save
%data from a function to the workspace. This is not
%quite right yet.
%Button3 = uiputfile ('Specify folder to save')
% prompt = {'Enter image name:'};
% title = 'Image display - assignin';
% lines = 1;
% def = {};
% answer = inputdlg(prompt, title, lines, def);
% assignin('base', {}, fv);
case 'No'
disp ('Operation terminated')
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Display 2-D Images 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!