matlab中如何實現三維圖像以指定角度旋轉,坐標軸不變?


題主所說的在保持坐標軸不變的情況下,對圖像進行特定角度的旋轉,其實就是將圖像按照某一固定軸旋轉一定角度。MATLAB中可以通過rotate函數實現(區別於viewrotate3d,它們只改便3D圖形的視角,並不旋轉圖像本身)。

首先,看一下如何對旋轉軸進行刻畫,一般有兩種方式:

  1. 以[xyz]表示的向量作為旋轉軸,即笛卡爾形式(Cartesian candidates),示意圖如下

  2. 以[	hetaphi]表示的球坐標(spherical candidates)為的旋轉軸,其中	heta為旋轉軸在xy平面投影與x正半軸的夾角,phi為旋轉軸與xy平面所成夾角,示意圖如下

其次,選定旋轉軸的起始點[x_0y_0z_0],特定的旋轉軸肯定是由一點和方向向量(direction,如上所述)唯一確定的,如果預設的話,MATLAB將其默認為plotbox中心點。最後,就是圍繞旋轉軸旋轉多少角度的問題,習慣以alpha標記標記所要旋轉的角度,那麼rotate函數的語法規則為:

rotate(h,direction,alpha,origin)

h is the graphics object.

direction is a two- or three-element vector that describes the axis of rotation.

positive alpha is defined as the righthand-rule angle about the direction vector as it extends from the origin of rotation.

origin is a three-element vector which describes the origin of the axis of rotation.

翻譯成人話就是:

  • h為待旋轉的圖像對象
  • direction為以笛卡爾或球坐標表示的旋轉軸,對應為3維和2維向量
  • alpha 為旋轉角度
  • origin 表示旋轉軸的起始點

簡單看個例子:

figure
sp11=subplot(2,2,1);
h11=surf(sp11,peaks(35));
title("No Rotation")
sp12=subplot(2,2,2);
h12=surf(sp12,peaks(35));
title("Rotation Around [1 1 1]-Axis")
zdir=[1 1 1];
rotate(h12,zdir,30)
sp21=subplot(2,2,3);
h21=surf(sp21,peaks(35));
title("Rotation Around [1 1 1]-Axis")
zdir=[45 45];
rotate(h21,zdir,30)
sp22=subplot(2,2,4);
h22=surf(sp22,peaks(35));
title("Rotation Around Z-Axis")
zdir=[0 90];
rotate(h22,zdir,90)

效果圖如下:

可以看到,第一張圖為旋轉前的原圖,第二張和第三張分別按照笛卡爾形式和球坐標形式給出的繞同一旋轉軸[1 1 1]旋轉30^circ後的效果圖,最後一張為繞Z軸旋轉90^circPS.

  1. 以上內容主要參考MATLAB強大的幫助文檔,部分截圖也出自源文檔。

  2. 作為一個學術界的搬運工,希望能幫到題主~

MATLAB關於rotate函數的官方註解(學渣英語不好,不想坑了題主,所以以上內容僅供參考)

rotate Rotate objects about specified origin and direction.

rotate(H,[THETA PHI],ALPHA) rotates the objects with handles H through angle ALPHA about an axis described by the 2-element direction vector [THETA PHI] (spherical coordinates).

All the angles are in degrees. The handles in H must be children of the same axes.

THETA is the angle in the xy plane counterclockwise from the positive x axis. PHI is the elevation of the direction vector from the xy plane (see also SPH2CART). Positive ALPHA is defined as the righthand-rule angle about the direction vector as it extends from the origin.

rotate(H,[X Y Z],ALPHA) rotates the objects about the direction vector [X Y Z] (Cartesian coordinates). The direction vector is the vector from the center of the plot box to (X,Y,Z).

rotate(...,ORIGIN) uses the point ORIGIN = [x0,y0,z0] as the center of rotation instead of the center of the plot box.


如果想要對複雜曲面(包含數據較多)旋轉或者做其他放射變換的話請用hgtransform(變換矩陣可以用makehgtform構建)

這樣的好處(在比較新的版本)是只需要向GPU傳遞一個4x4的變換矩陣由GPU對圖像重新渲染,而rotate的處理方式是提取數據並對數據計算(在內存中用CPU計算)之後重新交由GPU渲染,當被旋轉對象數據量較大時性能顯然不如前者,這一點尤其能在連續變換的動畫中體現出來


推薦閱讀:

Matlab中disp、fprintf和sprintf有什麼區別?
怎麼鍛煉 MATLAB 編程能力?
matlab有什麼不能做的事情?
Lingo和Matlab在解決最優化問題的時候的優缺點?
matlab畫圖,明明標記了11個點的坐標,最後畫出來的圖卻只有10個點,坐標沒有重疊。這是為什麼?

TAG:數學 | 圖像處理 | MATLAB | 數學建模 |