Turn a 2D Image into a rotating 3D image on Matlab

2019-04-16 01:53发布

I would like to know how to rotate a 2D image along its Z-axis using Matlab R2016b and to obtain an image after performing this procedure.

For example, let's take this 2D image:

enter image description here

Now, I rotate it of 45° roughly:

enter image description here

And now, of 90°:

enter image description here

Do you know if it is possible to perform the same operation in Matlab R2016b, please ?

Thank you very much for your help

Images Source: https://www.youtube.com/watch?v=m89mVexWQZ4

1条回答
成全新的幸福
2楼-- · 2019-04-16 02:45

Yes it's possible. The easiest thing to do would be to map the image on the y = 0 plane in 3D, then rotate the camera to the desired azimuth or the angle with respect to the y axis. Once you do that, you can use the getframe / cdata idiom to actually capture the actual image data in a variable itself. The reason why you do this with respect to the y plane is because the method that I will be using to present the image is through the surf command that plots surface plots in 3D, but the y axis here is the axis that goes into and out of the screen. The x axis is the horizontal and the z axis would be the vertical when displaying data.

First read in your image using something like imread, then you need to define the 4 corners of the image that map to the 3D plane and then rotate the camera. You can use the view function to help you rotate the camera by adjusting the azimuthal angle (first parameter) and leaving the elevation angle as 0.

Something like this could work. I'll be using the peppers image that is part of the image processing toolbox:

im = imread('peppers.png'); % Read in the image
ang = 45; % Rotate clockwise by 45 degrees

% Define 4 corners of the image
X = [-0.5 0.5; -0.5 0.5];
Y = [0 0; 0 0];
Z = [0.5 0.5; -0.5 -0.5];

% Place the image on the y = 0 plane
% Turn off the axis and rotate the camera
figure;
surf(X, Y, Z, 'CData', im, 'FaceColor', 'texturemap');
axis('off');
view(ang, 0);

% Get the image data after rotation
h = getframe;
rot_im = h.cdata;

rot_im contains the rotated image. To appreciate the rotation of the image, we can loop through angles from 0 to 360 in real time. At each angle, we can use view to dynamically rotate the camera and use drawnow to update the figure. I've also updated the title of the figure to show you what the angle is at each update. The code for that is below as well as the output saved as an animated GIF:

for ang = 0 : 360
    view(ang, 0);
    pause(0.01);
    drawnow;
    title(sprintf('Angle: %d degrees', ang));
end

查看更多
登录 后发表回答