【问题标题】:MATLAB Save figure data in current view and lightingMATLAB 将图形数据保存在当前视图和照明中
【发布时间】:2018-08-15 18:41:08
【问题描述】:

我需要创建一个随机表面(我使用peaks 创建的)并将其保存在某些光照条件下(我使用light 添加)。绘图本身是使用surf 创建的。然后我使用view(2) 设置所需的视角。

[X,Y] = meshgrid(-2:.025:2);
Z = peaks(X,Y);
h = surf(X,Y,Z);

% Necessary for task
h.AmbientStrength = 0.;
h.SpecularStrength = 0.;
h.DiffuseStrength = 1.;
h.BackFaceLighting = 'unlit';
h.FaceLighting = 'gouraud';

view(2);
l = light('Position',[-2, -2, 50],'Style','local','Color',[1 1 1]);

Z 矩阵的尺寸为 161x161。我想保存一个 161x161 矩阵 对应于图中的光照/阴影。有什么想法吗?

生成的示例图:

编辑:我确实打算使用保存的图像。总体目标是执行photometric stereo (something on these lines)。因此,我需要在不同的光照条件下生成多个表面图像。

PS:感谢h.LineStyle='none';

【问题讨论】:

  • 我假设你想要它没有黑线? h.LineStyle='none';?

标签: matlab matlab-figure


【解决方案1】:

我假设您想要这个是出于处理数据以外的其他原因,因为从绘图中获取数据是最坏的情况。假设您忘记了h.LineStyle='none';,否则您将看不到它们。

这段代码大致可以完成这项工作:

% get the frame plotted in the figure
test=getframe(gca);
% the size of the image will be arbitrary, depends on the size of the figure on screen and your screen resolution. Lets interpolate, so we can get the values at the exact points you want. I use the same bounds as in your description
[Xi,Yi]=meshgrid(linspace(-2,2,size(test.cdata,2)),linspace(-2,2,size(test.cdata,1)));

% Only works if grayscale, repeat x3 for each dimension if you have color
img=interp2(Xi,Yi,double(test.cdata(:,:,1)),X,Y)/255;

figure;imshow(img);

【讨论】:

  • 澄清一下:在我原来的 sn-p h 指向错误的数字(没有照明的那个),因此 h.CData 只是原始数据。因此,我应该使用gca 查询当前轴。对吗?
  • @SabreSword 是的。 h指向右图,点亮后数据不变,还是数据。
【解决方案2】:

Zmatrix 仅包含用于表示表面的 3D 数据,它不受您稍后应用于其可视化的照明/阴影设置的影响。

如果你想重现你通过加载“一些保存的数据”获得的图形,你必须沿着值保存Z矩阵(最好也保存XY)照明设置。

为此,您首先必须通过将 lightig 值分配给相应的一组变量来修改您的代码,然后您必须使用此变量来设置光照。

最后你必须保存所有这些变量。

[X,Y] = meshgrid(-2:.025:2);
Z = peaks(X,Y);
h = surf(X,Y,Z);

% Necessary for task

%h.AmbientStrength = 0.;
%h.SpecularStrength = 0.;
%h.DiffuseStrength = 1.;
%h.BackFaceLighting = 'unlit';
%h.FaceLighting = 'gouraud';

% Assign the setting values to a set of varialbles
AmbientStrength_val=0.;
SpecularStrength_val=0.;
DiffuseStrength_val=1.;
BackFaceLighting_val='unlit';
FaceLighting_val='gouraud';


h.AmbientStrength = AmbientStrength_val;
h.SpecularStrength = SpecularStrength_val;
h.DiffuseStrength = DiffuseStrength_val;
h.BackFaceLighting = BackFaceLighting_val;
h.FaceLighting = FaceLighting_val;

view_val=2;
%view(2);
view(view_val);

light_pos=[-2, -2, 50]
light_style='local'
light_color=[1 1 1]
%l = light('Position',[-2, -2, 50],'Style','local','Color',[1 1 1]);
l = light('Position',light_pos,'Style',light_style,'Color',light_color);

save('data_peaks.mat','X','Y','Z','AmbientStrength_val','SpecularStrength_val', ...
                     'DiffuseStrength_val','BackFaceLighting_val', ...
                     'FaceLighting_val','light_pos','light_style', ...
                     'light_color','view_val')

为了重现该图,将来您可以使用如下脚本:

% Load the saved values
load data_peaks
% Plot the surface
h = surf(X,Y,Z);
% Set the parameters
h.AmbientStrength = AmbientStrength_val;
h.SpecularStrength = SpecularStrength_val;
h.DiffuseStrength = DiffuseStrength_val;
h.BackFaceLighting = BackFaceLighting_val;
h.FaceLighting = FaceLighting_val;

view(view_val);

l = light('Position',light_pos,'Style',light_style,'Color',light_color);

【讨论】:

  • 检查编辑。我的目的不是为了简化图像的重建。很抱歉有歧义。
猜你喜欢
  • 2014-07-25
  • 1970-01-01
  • 2019-11-17
  • 2021-11-27
  • 1970-01-01
  • 2015-05-31
  • 2016-02-15
  • 1970-01-01
  • 2011-06-15
相关资源
最近更新 更多