How to convert a 2D image (which are pixel values plotted using 'imagesc') into a 3D surface plot?

20 views (last 30 days)
Tanmoy Chatterjee
Tanmoy Chatterjee on 25 Jan 2021
Commented: Tanmoy Chatterjee on 28 Jan 2021
How to convert a 2D image which are pixel values (plotted by 'imagesc' in 'colormap(gray)') representing the unit cell of a material into a 3D surface plot by placing multiple 2D images on top of one another?
I have tried a number of solutions provided in the MATLAB answers, however, nothing seems to work. For example, I have tried 'imread' and then 'surf', but it is displaying error in the 'surf'. Not sure whether I am doing right or not.
Can anyone provide some solution to the above problem?

Answers (1)

Walter Roberson
Walter Roberson on 25 Jan 2021
IMG1 = imread('cameraman.tif');
IMG2 = imread('flamingos.jpg');
%size(IMG1), size(IMG2)
[X1, Y1] = meshgrid(1:size(IMG1,2), 1:size(IMG1,1));
[X2, Y2] = meshgrid(1:size(IMG2,2), 1:size(IMG2,1));
Z1 = 6*ones(size(X1));
Z2 = 5*ones(size(X2));
%size(X1), size(Y1), size(Z1)
%size(X2), size(Y2), size(Z2)
surf(X1, Y1, Z1, 'CData', IMG1, 'edgecolor', 'none');
hold on
surf(X2, Y2, Z2, 'CData', IMG2, 'edgecolor', 'none');
hold off
view(3)
The above is what you asked for. However, a lot of the time it is not what you would want. If you want a surface plot, you would typically want the image to follow the contours of a surface. The easiest way to do that is to use warp() https://www.mathworks.com/help/images/ref/warp.html
  4 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!