1. 程式人生 > >Learn OpenCV之Rotation Matrix To Euler Angles

Learn OpenCV之Rotation Matrix To Euler Angles

本文要介紹的是 3 × 3 3 \times 3 的旋轉矩陣與尤拉角(Euler Angles)之間的相互轉換方法。

本文其實和OpenCV關係不大,但是譯者曾經花了一些時間解決自己在這部分知識上的困擾,看見原部落格寫的還不錯,決定還是記錄一下

一個旋轉矩陣能表示三個角度自由度,即繞著三維的座標軸的三個座標做旋轉,數學家們對三個自由度使用了不同的表示方式,有用三個數字表示、有用四個數字表示的、還有用 3

× 3 3 \times 3 的旋轉矩陣表示的。使用較廣的還是三個數字表示的方法,也有少數用四個數字表示。

在一個三維的旋轉操作中,可以將其描述為分別繞X、Y、Z軸旋轉的旋轉角,也可以將其描述為分別繞Z、Y、X軸旋轉的旋轉角。這三個角度我們稱之為尤拉角(Euler angles)或者泰特布萊恩角(Tait–Bryan angles)。在尤拉角中,旋轉角被描述為依次繞著Z、X、Z軸或者Y-X-Y或者Z-Y-Z等得到的角度,即其即第一個旋轉軸和最後一個旋轉軸相同。而繞著三個不同軸得到的角度,例如Z-Y-X,應該稱為泰特布萊恩角。但是尤拉角稱呼比較普遍,所以文中我們將泰特布萊恩角也稱之為尤拉角。

泰特布萊恩角有如下六種情況,X-Y-Z, X-Z-Y, Y-Z-X, Y-X-Z, Z-X-Y, Z-Y-X。 你可能會認為隨便選一種情況來進行計算就可以了(因為不同的旋轉順序會導致不同的計算結果,這點是要注意的,所以必須旋轉一種旋轉方式進行計算),其實不能隨便選的。工業上一般會選擇Z-Y-X這樣一個順序,三個旋轉角的名字分別稱為yaw,pitch,roll。

這裡有個關於旋轉矩陣的計算問題值得注意,對於給定的一個點point(x,y,z),利用旋轉矩陣將其旋轉,因為這個是矩陣運算,所以將點向量左乘矩陣和右乘矩陣得到的效果是不一樣的。這兩個旋轉矩陣互為轉置關係。

上面描述的意思是沒有將一個標準定義旋轉矩陣轉換為尤拉角。文中將要敘述的轉換程式碼參考於MATLAB中的rotm2euler.m實現。不同於MATLAB實現的是,它的旋轉順序是Z-Y-X,而下面的實現是X-Y-Z。

在計算將旋轉矩陣轉換成尤拉角之前,先介紹一下尤拉角轉換為旋轉矩陣的方法。

尤拉角轉換為旋轉矩陣

假如已知旋轉角,繞著X-Y-Z三個軸的角度分別為 θ x θ y θ z \theta_{x} \quad \theta_{y} \quad \theta_{z} 。那麼三個旋轉矩陣可以表示如下
R x = [ 1 0 0 0 c o s θ x s i n θ x 0 s i n θ x c o s θ x ] R_{x} = \left[\begin{matrix}1 & 0 & 0 \\ 0 & cos\theta_{x} & -sin\theta_{x} \\ 0 & sin\theta_{x} & cos\theta_{x} \end{matrix} \right]

R y = [ c o s θ y 0 s i n θ y 0 1 0 s i n θ y 0 c o s θ y ] R_{y} = \left[\begin{matrix}cos\theta_{y} & 0 & sin\theta_{y} \\ 0 & 1 & 0 \\ -sin\theta_{y} & 0 & cos\theta_{y} \end{matrix} \right]

R z = [ c o s θ z s i n θ z 0 s i n θ z c o s θ z 0 0 0 1 ] R_{z} = \left[\begin{matrix}cos\theta_{z} & -sin\theta_{z} & 0 \\ sin\theta_{z} & cos\theta_{z} & 0 \\ 0 & 0 & 1 \end{matrix} \right]

如果現在旋轉順序是Z-Y-X,那麼旋轉矩陣表示如下
R = R z R y R x R=R_{z}\cdot R_{y}\cdot R_{x}
對應程式碼如下
C++

// Calculates rotation matrix given euler angles.
Mat eulerAnglesToRotationMatrix(Vec3f &theta)
{
    // Calculate rotation about x axis
    Mat R_x = (Mat_<double>(3,3) <<
               1,       0,              0,
               0,       cos(theta[0]),   -sin(theta[0]),
               0,       sin(theta[0]),   cos(theta[0])
               );
     
    // Calculate rotation about y axis
    Mat R_y = (Mat_<double>(3,3) <<
               cos(theta[1]),    0,      sin(theta[1]),
               0,               1,      0,
               -sin(theta[1]),   0,      cos(theta[1])
               );
     
    // Calculate rotation about z axis
    Mat R_z = (Mat_<double>(3,3) <<
               cos(theta[2]),    -sin(theta[2]),      0,
               sin(theta[2]),    cos(theta[2]),       0,
               0,               0,                  1);
     
     
    // Combined rotation matrix
    Mat R = R_z * R_y * R_x;
     
    return R;
 
}

Python

# Calculates Rotation Matrix given euler angles.
def eulerAnglesToRotationMatrix(theta) :
     
    R_x = np.array([[1,                  0,                   0],
                    [0, math.cos(theta[0]), -math.sin(theta[0])],
                    [0,  math.sin(theta[0]), math.cos(theta[0]) ]])
                           
    R_y = np.array([[math.cos(theta[1]), 0, math.sin(theta[1])],
                    [0,                  1,                  0],
                    [-math.sin(theta[1]), 0, math.cos(theta[1])]])
                 
    R_z = np.array([[math.cos(theta[2]), -math.sin(theta[2]), 0],
                    [math.sin(theta[2]), math.cos(theta[2]),  0],
                    [0,                  0,                   1]])
                     
                     
    R = np.dot(R_z, np.dot( R_y, R_x ))
 
    return R

將旋轉矩陣轉換為旋轉角

將旋轉矩陣轉換為旋轉角就有點麻煩了。因為選擇不同的旋轉順序,得到的旋轉角一般是不一樣的。你可以證明下面兩個尤拉角,[0.1920, 2.3736, 1.1170](角度製為[11, 136, 64]),[-2.9496, 0.7679, -2.0246](角度製為[-169, 44, -116]),可以得到同一個旋轉矩陣。下面程式碼參考於MATLAB中的rotm2euler.m實現。不同於MATLAB實現的是,它的旋轉順序是Z-Y-X,而下面的實現是X-Y-Z。

C++

// Checks if a matrix is a valid rotation matrix.
bool isRotationMatrix(Mat &R)
{
    Mat Rt;
    transpose(R, Rt);
    Mat shouldBeIdentity = Rt * R;
    Mat I = Mat::eye(3,3, shouldBeIdentity.type());
     
    return  norm(I, shouldBeIdentity) < 1e-6;
     
}
 
// Calculates rotation matrix to euler angles
// The result is the same as MATLAB except the order
// of the euler angles ( x and z are swapped ).
Vec3f rotationMatrixToEulerAngles(Mat &R)
{
 
    assert(isRotationMatrix(R));
     
    float sy = sqrt(R.at<double>(0,0) * R.at<double>(0,0) +  R.at<double>(1,0) * R.at<double>(1,0) );
 
    bool singular = sy < 1e-6; // If
 
    float x, y, z;
    if (!singular)
    {
        x = atan2(R.at<double>(2,1) , R.at<double>(2,2));
        y = atan2(-R.at<double>(2,0), sy);
        z = atan2(R.at<double>(1,0), R.at<double>(0,0));
    }
    else
    {
        x = atan2(-R.at<double>(1,2), R.at<double>(1,1));
        y = atan2(-R.at<double>(2,0), sy);
        z = 0;
    }
    return Vec3f(x, y, z);
     
}

Python

# Checks if a matrix is a valid rotation matrix.
def isRotationMatrix(R) :
    Rt = np.transpose(R)
    shouldBeIdentity = np.dot(Rt, R)
    I = np.identity(3, dtype = R.dtype)
    n = np.linalg.norm(I - shouldBeIdentity)
    return n < 1e-6
 
 
# Calculates rotation matrix to euler angles
# The result is the same as MATLAB except the order
# of the euler angles ( x and z are swapped ).
def rotationMatrixToEulerAngles(R) :
 
    assert(isRotationMatrix(R))
     
    sy = math.sqrt(R[0,0] * R[0,0] +  R[1,0] * R[1,0])
     
    singular = sy < 1e-6
 
    if  not singular :
        x = math.atan2(R[2,1] , R[2,2])
        y = math.atan2(-R[2,0], sy)
        z = math.atan2(R[1,0], R[0,0])
    else :
        x = math.atan2(-R[1,2], R[1,1])
        y = math.atan2(-R[2,0], sy)
        z = 0
 
    return np.array([x, y, z])

上述程式碼的計算原理,可以將三個矩陣展開,即將 R = R x R y R z R=R_{x}\cdot R_{y}\cdot R_{z}

相關推薦

Learn OpenCVRotation Matrix To Euler Angles

本文要介紹的是 3 × 3 3 \tim

Learn OpenCVWarpTriangle

這篇文章將講述的是如何將一個圖片內的三角形內容對映到另一個圖片內的不同形狀的三角形內。 在圖形學的研究中,研究者常常進行三角形之間的變換操作,因為任意的3D表面都可以用多個三角形去近似表示。同樣的,圖片也可以分解成多個三角形來表示。但是在OpenCV中並沒有一個直接可以將三角形轉換為另一

Learn OpenCVHeatmap

本文是利用熱圖(Heatmap)分析視訊序列的標定。 注意,這裡目的不是標定而是分析標定好的資料,或者也可以是檢測的結果資料 文章結構是這樣的,先詳細的解釋一下熱圖分析有什麼用,根據一些具體的應用例項給出相應的教程和Python實現程式碼。 為什麼要用熱圖對Logo檢測結果進行分析

Learn OpenCVConvex Hull

這篇文章講的是如何尋找給出的點集的凸包(Convex Hull),先簡單介紹演算法原理,之後利用OpenCV實現一個找凸包的程式。 什麼是凸包(Convex Hull)? 這個問題可以分成兩個概念理解,Convex 和 Hull 凸形狀(Convex object)就是沒有大於1

Calculate Rotation Matrix to align Vector A to Vector B in 3d?

Octave/Matlab Implementation The basic implementation is very simple. You could improve it by factoring out the common expressions of dot(A,

opencv紋理特征(熵值)的提取

ext size blog alt 提取 src log ng- 特征 opencv之紋理特征(熵值)的提取

OpenCV 圖像分割 (一)

tutorial 轉化 分析 可選 name lar 機器 不能 mes 1 基於閾值 1.1 基本原理 灰度閾值化,是最簡單也是速度最快的一種圖像分割方法,廣泛應用在硬件圖像處理領域 (例如,基於 FPGA 的實時圖像處理)。 假設輸入圖像為 f,輸出圖

OpenCV 網絡攝像頭

rtsp blank space mat word http log ddr bsp 1 RTSP RTSP (Real Time Streaming Protocol),是一種語法和操作類似 HTTP 協議,專門用於音頻和視頻的應用層協議。 和 HTTP 類似,R

How to convert matrix to RDD[Vector] in spark

toarray kcon tex logs def supports iterator ati true The matrix is generated from SVD, and I am using the results from SVD to do clusteri

aNDROID-OpENCVCVCaMERa

.com oid lis andro 5% ongl http camera list L%E7%AE%80%E5%8D%95%E7%9A%842D%E7%BB%98%E5%9B%BE%EF%BC%88%E6%9C%BA%E5%99%A8%E4%BA%BA%EF%BC%89

opencvhaar特征+AdaBoos分類器算法流程(三)

alt jsb pop fcm avi tex ext con trac opencv之haar特征+AdaBoos分類器算法流程(三)

opencv從視頻幀中截取圖片

圖片 capture cas randint read ide sca path process 最近在訓練一個人臉識別的模型,而項目訓練需要大量真實人臉圖片樣本。 剛好項目用到opencv識別人臉,可以把每一幀圖片保存下來,用此方法可以方便的獲取大量的臉部樣本,

OpenCV資料分享

tps article www. blank org 圖像濾波 arc htm 有用 分享一些有用的資料鏈接: OpenCV入門教程:http://blog.csdn.net/column/details/opencv-tutorial.html OpenCV入門教程(組件

OpenCV圖像像素&圖像色彩通道

red python版本 usr 有一個 imwrite 圖片路徑 class get gre 一次OpenCV相關作業,有一個助教小姐姐寫的tutorial,很有用,鏈接如下: 鏈接:http://pan.baidu.com/s/1bZHsJk 密碼:854s 1. 色

opencvSURF圖像匹配

draw 空間變換 設置 extract mes 基於 scene body contain 1.概述 前面介紹模板匹配的時候已經提到模板匹配時一種基於灰度的匹配方

opencv光照補償和去除光照

本部落格借用了不少其他部落格,相當於知識整理 一、光照補償 1.直方圖均衡化 #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream> using namespac

opencv大津法Otsu介紹

一、大津法(Otsu)         所謂大津法(Otsu)就是最大類間差方法,通過統計影象直方圖資訊來自動確定閾值T,從而來區分前景與背景,說白了就是能自動區分影象前景與背景的二值化。 演算法流程描述: 1.遍歷影象畫素,統計每個畫素值出現

pandas縱向學習10 minutes to pandas(一)

10mins官方文件 10 Minutes to pandas 必要的庫匯入: import pandas as pd import numpy as np import matplotlib.pyplot as plt 建立物件 pandas常用資料

opencvmat資料型別

opencv之mat資料型別 cv::Mat定義並初始化 cv::Mat mat(row_num, col_num, CV_64F3, cv::Scalar(0)) cv::Mat先定義再賦值 cv::Mat mat; mat = cv::Mat::zeros(row_num, c

opencvtype()函式返回值對應表

opencv之type()函式返回值對應表 cv::Mat 類的物件有一個成員函式 type() 用來返回矩陣元素的資料型別,返回值是 int 型別,不同的返回值代表不同的型別。 int Mat::type() const 返回值與具體型別對應關係表: | |C1| C2| C3| C4| --|--|