Covert surf 3D image to 2D matrix

7 Ansichten (letzte 30 Tage)
Haomin Wang
Haomin Wang am 16 Jan. 2019
Beantwortet: Vedant Shah am 21 Feb. 2025
Hello,
I have a code like this:
[x,y] = meshgrid(1:15,1:15);
tri = delaunay(x,y);
z = peaks(15);
trisurf(tri,x,y,z)
view(2)
and it gives me a 2D projection on XY plane of a 3D surface like this:
example.png
I have two questions:
  1. How can I convert this projection to a new 2D image?
  2. I want to extract the Z values of the same figure to form a 2D matrix with arbitrary size, such as a 3*3 matrix. Is there any way to do this?
Thanks!

Antworten (1)

Vedant Shah
Vedant Shah am 21 Feb. 2025
To save an image into a new 2D projection, the saveas function can be used. Additionally, to extract z-values from the figure into a matrix of any arbitrary size, the interp2 function can be employed by interpolating the x and y values and calculating the corresponding z-values. For more information about these functions, please refer to the documentation using the following commands in MATLAB command line:
web(fullfile(docroot, "/matlab/ref/saveas.html"))
web(fullfile(docroot, "/matlab/ref/interp2.html"))
To save the image as a new 2D image, the following line can be added to your existing code:
saveas(gcf, 'projection.png')
To save the z-values as an arbitrary 3x3 matrix, you can refer to the following code:
% Define new grid size
new_size = 3;
% Create new grid
[xq, yq] = meshgrid(linspace(1, 15, new_size), linspace(1, 15, new_size));
% Interpolate Z values
z_new = interp2(x, y, z, xq, yq);
% Display the new matrix
disp(z_new);
Using this approach, we can obtain the z-values in a 3x3 matrix format as a result.

Produkte


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by