使用STLRead拍摄3D模型的图像 [英] Taking images of 3D model using STLRead
问题描述
我正在尝试拍摄3D模型的图像".我可以通过加载STL文件此代码.我需要做的是能够旋转对象,并在旋转时拍摄其图像".
I'm trying to take "images" of a 3D model. I am able to load an STL file through this code. What I need to do is be able rotate the object, and take "images" of it as it rotates.
stlread输出与patch()兼容的面和顶点结构,因此我可以显示该对象,但不确定如何将图像实际存储到矩阵中.
stlread outputs a face and vertex structure that is compatible with patch(), so I can display the object, but I'm not sure how to actually store that image into a matrix.
推荐答案
我认为您追求的是功能 getframe ,它捕获轴的内容并将数据存储为常规的图像"矩阵.
I think what you are after is the function getframe, which captures the content of an axes and store the data as a regular "image" matrix.
我制作了一个简单的GUI来说明这个概念.基本上,我从您提供的链接(stldemo.m
)中获取了代码,并添加了一些用于拍摄快照的功能.在这种情况下,我添加了命令rotate3d on
以在轴内旋转股骨,并添加了一个按钮,该按钮的回调调用getframe
并捕获了轴的内容.基本上,您可以根据需要旋转股骨//修补对象,然后按按钮获取快照.在代码中,我弹出了一个新的数字来说服您使用它,但是您当然可以对数据进行任何操作.
I made a simple GUI to illustrate the concept. Basically I took the code from the link you provided (stldemo.m
) and added a few features to take snapshots. In this case I added the command rotate3d on
to rotate the femur inside the axes and a pushbutton whose callback calls getframe
and captures the content of the axes. Basically you rotate the femur//patch object as you wish and you press the button to get a snapshot. In the code I make a new figure pop up to convince you it works, but you can of course do whatever you want with the data.
请注意,使用getframe
输出的结构带有一个名为cdata
的字段...这就是要获取轴内容作为矩阵的目的.
Note that using getframe
outputs a structure with a field called cdata
...that's what you are after in order to get the content of the axis as a matrix.
下面是代码和一些快照来说明:
So here is the code and a few snapshots to illustrate:
function SnapShotSTL(~)
%// Taken from the stldemo.m file.
%% 3D Model Demo
% This is short demo that loads and renders a 3D model of a human femur. It
% showcases some of MATLAB's advanced graphics features, including lighting and
% specular reflectance.
% Copyright 2011 The MathWorks, Inc.
%% Load STL mesh
% Stereolithography (STL) files are a common format for storing mesh data. STL
% meshes are simply a collection of triangular faces. This type of model is very
% suitable for use with MATLAB's PATCH graphics object.
% Import an STL mesh, returning a PATCH-compatible face-vertex structure
handles.fv = stlread('femur.stl');
%% Render
% The model is rendered with a PATCH graphics object. We also add some dynamic
% lighting, and adjust the material properties to change the specular
% highlighting.
%// Create figure, axes and a pushbutton.
hFigure = figure('Units','Pixels','Position',[200 200 500 600]);
hAxes1 = axes('Units','pixels','Position',[50 50 450 400]);
hSnapShot = uicontrol('Style','push','Position',[50 500 60 30],'String','SnapShot','Callback',@(s,e) GetSnapshot);
patch(handles.fv,'FaceColor', [0.8 0.8 1.0], ...
'EdgeColor', 'none', ...
'FaceLighting', 'gouraud', ...
'AmbientStrength', 0.15);
% Add a camera light, and tone down the specular highlighting
camlight('headlight');
material('dull');
% Fix the axes scaling, and set a nice view angle
axis('image');
view([-135 35]);
rotate3d on %// Enable to rotate the object in the axes
guidata(hFigure,handles);
function GetSnapshot
CurrentImage = getframe(gca);
ImageData = CurrentImage.cdata; %// Access the data. I create a variable but that's not strictly necessary.
%// Here a new figure is created and the image displayed in it... you can store it and do as you like with it.
figure;
imshow(ImageData);
guidata(hFigure,handles);
end
end
因此,GUI在打开时如下所示:
So the GUI looks like this when opened:
然后旋转对象/股骨:
最后,在按下按钮后,将拍摄快照(即执行getframe
),结果图像矩阵将显示在新图中:
and finally after pressing the pushbutton, a snapshot is taken (i.e. getframe
is executed) and the resulting image matrix displayed in a new figure:
请注意,我使用imshow
来实际显示图像,但也可以对数据进行操作.
Note that I used imshow
to actually display the image but the data can be manipulated as well.
此外,如果要检索与数据关联的颜色图,还可以对getframe
使用以下调用语法:
Moreover, you can also use the following calling syntax for getframe
in case you want to retrieve the colormap associated with your data:
F = getframe;
[X,map] = frame2im(F);
在这种情况下,使用此代码时X
等于G:
In this case, X
would be equivalent to G when using this code:
G = F.cdata;
最后,您可以设置一个循环,在该循环中,实际的补丁对象将自己旋转并自动拍摄帧.实施起来应该不太困难:)
Finally, you could set up a loop in which the actual patch object is rotated by itself and frames are taken automatically. That should not be too hard to implement :)
希望有帮助!
这篇关于使用STLRead拍摄3D模型的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!