MATLAB 在当前视图和光照中保存图形数据
MATLAB Save figure data in current view and lighting
我需要创建一个随机表面(我使用 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矩阵对应图中的lighting/shading。有什么想法吗?
示例生成图:
编辑: 我打算使用保存的图像。总体目标是执行 photometric stereo (something on these lines)。因此,我需要在不同光照条件下生成多张表面图像。
PS: 感谢 h.LineStyle='none';
我假设您出于处理数据之外的其他原因想要这样做,因为从图中获取数据是最坏的情况。假设您也忘记了 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);
Z
矩阵仅包含表示表面的3D
数据,它不受您稍后应用于其可视化的光照/阴影设置的影响。
如果您想重现通过加载 "some saved data" 获得的图形,您必须保存 Z
矩阵(最好同时保存 X
和 Y
) 沿着照明设置的值。
为此,您首先必须通过将 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);
我需要创建一个随机表面(我使用 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矩阵对应图中的lighting/shading。有什么想法吗?
示例生成图:
编辑: 我打算使用保存的图像。总体目标是执行 photometric stereo (something on these lines)。因此,我需要在不同光照条件下生成多张表面图像。
PS: 感谢 h.LineStyle='none';
我假设您出于处理数据之外的其他原因想要这样做,因为从图中获取数据是最坏的情况。假设您也忘记了 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);
Z
矩阵仅包含表示表面的3D
数据,它不受您稍后应用于其可视化的光照/阴影设置的影响。
如果您想重现通过加载 "some saved data" 获得的图形,您必须保存 Z
矩阵(最好同时保存 X
和 Y
) 沿着照明设置的值。
为此,您首先必须通过将 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);