使用 STLRead 拍摄 3D 模型的图像

Taking images of 3D model using STLRead

我正在尝试拍摄 "images" 的 3D 模型。我可以通过 this 代码加载 STL 文件。我需要做的是能够旋转对象,并在旋转时获取它的 "images"。

stlread 输出与 patch() 兼容的面和顶点结构,因此我可以显示对象,但我不确定如何将该图像实际存储到矩阵中。

我想你想要的是函数 getframe,它捕获轴的内容并将数据存储为常规 "image" 矩阵。

我制作了一个简单的 GUI 来说明这个概念。基本上,我从您提供的 link (stldemo.m) 中获取了代码,并添加了一些功能来拍摄快照。在这种情况下,我添加了命令 rotate3d on 以在轴内旋转股骨,并添加了一个按钮,其回调调用 getframe 并捕获轴的内容。基本上,您可以根据需要旋转股骨//补丁对象,然后按下按钮获取快照。在代码中,我弹出了一个新图形来让你相信它有效,但你当然可以对数据做任何你想做的事。

请注意,使用 getframe 会输出一个结构,其中包含一个名为 cdata 的字段...这就是您要将轴的内容作为矩阵获取的结果。

所以这里是代码和一些快照来说明:

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 如下所示:

然后经过object/femur的旋转:

最后在按下按钮后,拍摄快照(即执行 getframe)并将生成的图像矩阵显示在新图形中:

请注意,我使用 imshow 来实际显示图像,但数据也可以被操纵。

此外,如果您想检索与数据关联的颜色图,您还可以对 getframe 使用以下调用语法:

F = getframe;
[X,map] = frame2im(F);

在这种情况下,X 在使用此代码时等同于 G:

G = F.cdata;

最后,您可以设置一个循环,在该循环中,实际的补丁对象会自行旋转并自动拍摄帧。这应该不会太难实现:)

希望对您有所帮助!