问题描述
我想知道如何使用Matlab R2016b沿其Z轴旋转2D图像,并在执行此过程后获取图像。
例如,我们来看看这张2D图像:
现在,90°:
你知道是否可以在Matlab R2016b中执行相同的操作吗?
非常感谢你的帮助
图片来源:
是的,这是可能的。最简单的方法是将图像映射到3D中的 y = 0
平面上,然后将相机旋转到所需的方位角或相对于<$的角度c $ c> y 轴。一旦你这样做,你可以使用 getframe / cdata
成语来实际捕获变量本身的实际图像数据。您对 y
平面执行此操作的原因是因为我将用于显示图像的方法是通过命令用于绘制3D中的曲面图,但是这里的 y
轴是进出屏幕的轴。 x
轴是水平轴, z
轴在显示数据时是垂直轴。
首先使用,然后您需要定义映射到3D平面的图像的4个角,然后旋转相机。您可以使用通过调整方位角(第一个参数)并将仰角保持为0来帮助您旋转摄像机。
这样的事情可以起作用。我将使用作为图像处理工具箱一部分的辣椒图像:
im = imread('peppers.png' ); %读入图片
ang = 45; %顺时针旋转45度
%定义图像的4个角
X = [-0.5 0.5; -0.5 0.5];
Y = [0 0; 0 0];
Z = [0.5 0.5; -0.5 -0.5];
%将图像放在y = 0平面上
%关闭轴并旋转相机
figure;
surf(X,Y,Z,'CData',im,'FaceColor','texturemap');
轴('关闭');
view(ang,0);
%轮换后获取图像数据
h = getframe;
rot_im = h.cdata;
rot_im
包含旋转的图像。为了欣赏图像的旋转,我们可以实时循环从0到360的角度。在每个角度,我们可以使用 view
来动态旋转相机并使用 drawnow
来更新数字。我还更新了图的标题,以显示每次更新时的角度。它的代码如下,以及保存为动画GIF的输出:
for ang = 0:360
view(ang,0);
暂停(0.01);
drawow;
title(sprintf('Angle:%d degrees',ang));
结束
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:
Now, I rotate it of 45° roughly:
And now, of 90°:
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
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
这篇关于在Matlab上将2D图像转换为旋转的3D图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!