Opencv::Mat 與 Eigen互轉
Opencv::Mat轉Eigen
#include <Eigen/Dense>
#include <iostream>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace Eigen;
void main()
{
Mat img = imread("jasen.jpg",CV_LOAD_IMAGE_GRAYSCALE);
int row = img.rows;
int col = img.cols;
MatrixXd m(row, col);
cv2eigen(img,m);
return;
}
Eigen轉Opencv::Mat
#include <Eigen/Dense>
#include <iostream>
#include <opencv2/core/eigen.hpp>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
using namespace Eigen;
void main()
{
Mat img;
Matrix<int,100,100> m ;
m.fill(255);
eigen2cv(m, img);
return;
}
Eigen常用操作
#include <Eigen/Dense>
// 基本用法
// Eigen // Matlab // 註釋
x.size() // length(x) // 向量的長度
C.rows() // size(C,1) // 矩陣的行數
C.cols() // size(C,2) // 矩陣的列數
x(i) // x(i+1) // 訪問向量元素(Matlab的下標從1開始計數)
C(i,j) // C(i+1,j+1) // 訪問矩陣元素
A << 1, 2, 3, // 初始化A,元素也可以是矩陣,先按列堆疊,再按行堆疊。
4, 5, 6,
7, 8, 9;
B << A, A, A; // B 是3個A水平排列
A.fill(10); // 將A的所有元素填充爲10
// Eigen // Matlab 註釋
MatrixXd::Identity(rows,cols) // eye(rows,cols) //單位矩陣
C.setIdentity(rows,cols) // C = eye(rows,cols) //單位矩陣
MatrixXd::Zero(rows,cols) // zeros(rows,cols) //全零矩陣
C.setZero(rows,cols) // C = zeros(rows,cols) //全零矩陣
MatrixXd::Ones(rows,cols) // ones(rows,cols) //全一矩陣
C.setOnes(rows,cols) // C = ones(rows,cols) //全一矩陣
MatrixXd::Random(rows,cols) // rand(rows,cols)*2-1 //MatrixXd::Random 返回範圍爲(-1, 1)的均勻分佈的隨機數
C.setRandom(rows,cols) // C = rand(rows,cols)*2-1 //返回範圍爲(-1, 1)的均勻分佈的隨機數
VectorXd::LinSpaced(size,low,high) // linspace(low,high,size)' //返回size個等差數列,第一個數爲low,最後一個數爲high
v.setLinSpaced(size,low,high) // v = linspace(low,high,size)' //返回size個等差數列,第一個數爲low,最後一個數爲high
VectorXi::LinSpaced(((hi-low)/step)+1, // low:step:hi //以step爲步長的等差數列。((hi-low)/step)+1爲個數
low,low+step*(size-1)) //
// Matrix 切片和塊。下面列出的所有表達式都是可讀/寫的。
// 使用模板參數更快(如第2個)。注意:Matlab是的下標是從1開始的。
// Eigen // Matlab // 註釋
x.head(n) // x(1:n) //前n個元素
x.head<n>() // x(1:n) //前n個元素
x.tail(n) // x(end - n + 1: end) //倒數n個元素
x.tail<n>() // x(end - n + 1: end) //倒數n個元素
x.segment(i, n) // x(i+1 : i+n) //切片
x.segment<n>(i) // x(i+1 : i+n) //切片
P.block(i, j, rows, cols) // P(i+1 : i+rows, j+1 : j+cols) //塊
P.block<rows, cols>(i, j) // P(i+1 : i+rows, j+1 : j+cols) //塊
P.row(i) // P(i+1, :) //第i行
P.col(j) // P(:, j+1) //第j列
P.leftCols<cols>() // P(:, 1:cols) //前cols列
P.leftCols(cols) // P(:, 1:cols) //前cols列
P.middleCols<cols>(j) // P(:, j+1:j+cols) //中間cols列
P.middleCols(j, cols) // P(:, j+1:j+cols) //中間cols列
P.rightCols<cols>() // P(:, end-cols+1:end) //後cols列
P.rightCols(cols) // P(:, end-cols+1:end) //後cols列
P.topRows<rows>() // P(1:rows, :) //前rows行
P.topRows(rows) // P(1:rows, :) //前rows行
P.middleRows<rows>(i) // P(i+1:i+rows, :) //中間rows行
P.middleRows(i, rows) // P(i+1:i+rows, :) //中間rows行
P.bottomRows<rows>() // P(end-rows+1:end, :) //最後rows行
P.bottomRows(rows) // P(end-rows+1:end, :) //最後rows行
P.topLeftCorner(rows, cols) // P(1:rows, 1:cols) //左上角塊
P.topRightCorner(rows, cols) // P(1:rows, end-cols+1:end) //右上角塊
P.bottomLeftCorner(rows, cols) // P(end-rows+1:end, 1:cols) //左下角塊
P.bottomRightCorner(rows, cols) // P(end-rows+1:end, end-cols+1:end) //右下角塊
P.topLeftCorner<rows,cols>() // P(1:rows, 1:cols) //左上角塊
P.topRightCorner<rows,cols>() // P(1:rows, end-cols+1:end) //右上角塊
P.bottomLeftCorner<rows,cols>() // P(end-rows+1:end, 1:cols) //左下角塊
P.bottomRightCorner<rows,cols>() // P(end-rows+1:end, end-cols+1:end) //右下角塊
// 特別說明:Eigen的交換函數進行了高度優化
// Eigen // Matlab
R.row(i) = P.col(j); // R(i, :) = P(:, j)
R.col(j1).swap(mat1.col(j2)); // R(:, [j1 j2]) = R(:, [j2, j1]) //交換列
// Views, transpose, etc;
// Eigen // Matlab
R.adjoint() // R' // 共軛轉置
R.transpose() // R.' or conj(R') // 可讀/寫 轉置
R.diagonal() // diag(R) // 可讀/寫 對角元素
x.asDiagonal() // diag(x) // 對角矩陣化
R.transpose().colwise().reverse() // rot90(R) // 可讀/寫 逆時針旋轉90度
R.rowwise().reverse() // fliplr(R) // 水平翻轉
R.colwise().reverse() // flipud(R) // 垂直翻轉
R.replicate(i,j) // repmat(P,i,j) // 複製矩陣,垂直複製i個,水平復制j個
// 四則運算,和Matlab相同。但Matlab中不能使用*=這樣的賦值運算符
// 矩陣 - 向量 矩陣 - 矩陣 矩陣 - 標量
y = M*x; R = P*Q; R = P*s;
a = b*M; R = P - Q; R = s*P;
a *= M; R = P + Q; R = P/s;
R *= Q; R = s*P;
R += Q; R *= s;
R -= Q; R /= s;
// 逐像素操作Vectorized operations on each element independently
// Eigen // Matlab //註釋
R = P.cwiseProduct(Q); // R = P .* Q //逐元素乘法
R = P.array() * s.array(); // R = P .* s //逐元素乘法(s爲標量)
R = P.cwiseQuotient(Q); // R = P ./ Q //逐元素除法
R = P.array() / Q.array(); // R = P ./ Q //逐元素除法
R = P.array() + s.array(); // R = P + s //逐元素加法(s爲標量)
R = P.array() - s.array(); // R = P - s //逐元素減法(s爲標量)
R.array() += s; // R = R + s //逐元素加法(s爲標量)
R.array() -= s; // R = R - s //逐元素減法(s爲標量)
R.array() < Q.array(); // R < Q //逐元素比較運算
R.array() <= Q.array(); // R <= Q //逐元素比較運算
R.cwiseInverse(); // 1 ./ P //逐元素取倒數
R.array().inverse(); // 1 ./ P //逐元素取倒數
R.array().sin() // sin(P) //逐元素計算正弦函數
R.array().cos() // cos(P) //逐元素計算餘弦函數
R.array().pow(s) // P .^ s //逐元素計算冪函數
R.array().square() // P .^ 2 //逐元素計算平方
R.array().cube() // P .^ 3 //逐元素計算立方
R.cwiseSqrt() // sqrt(P) //逐元素計算平方根
R.array().sqrt() // sqrt(P) //逐元素計算平方根
R.array().exp() // exp(P) //逐元素計算指數函數
R.array().log() // log(P) //逐元素計算對數函數
R.cwiseMax(P) // max(R, P) //逐元素計算R和P的最大值
R.array().max(P.array()) // max(R, P) //逐元素計算R和P的最大值
R.cwiseMin(P) // min(R, P) //逐元素計算R和P的最小值
R.array().min(P.array()) // min(R, P) //逐元素計算R和P的最小值
R.cwiseAbs() // abs(P) //逐元素計算R和P的絕對值
R.array().abs() // abs(P) //逐元素計算絕對值
R.cwiseAbs2() // abs(P.^2) //逐元素計算平方
R.array().abs2() // abs(P.^2) //逐元素計算平方
(R.array() < s).select(P,Q ); // (R < s ? P : Q) //根據R的元素值是否小於s,選擇P和Q的對應元素
R = (Q.array()==0).select(P,A) // R(Q==0) = P(Q==0) R(Q!=0) = P(Q!=0) //根據Q中元素等於零的位置選擇P中元素
R = P.unaryExpr(ptr_fun(func)) // R = arrayfun(func, P) // 對P中的每個元素應用func函數
// Reductions.
int r, c;
// Eigen // Matlab //註釋
R.minCoeff() // min(R(:)) //最小值
R.maxCoeff() // max(R(:)) //最大值
s = R.minCoeff(&r, &c) // [s, i] = min(R(:)); [r, c] = ind2sub(size(R), i); //計算最小值和它的位置
s = R.maxCoeff(&r, &c) // [s, i] = max(R(:)); [r, c] = ind2sub(size(R), i); //計算最大值和它的位置
R.sum() // sum(R(:)) //求和(所有元素)
R.colwise().sum() // sum(R) //按列求和
R.rowwise().sum() // sum(R, 2) or sum(R')' //按行求和
R.prod() // prod(R(:)) //累積
R.colwise().prod() // prod(R) //按列累積
R.rowwise().prod() // prod(R, 2) or prod(R')' //按行累積
R.trace() // trace(R) //跡
R.all() // all(R(:)) //是否所有元素都非零
R.colwise().all() // all(R) //按列判斷,是否該列所有元素都非零
R.rowwise().all() // all(R, 2) //按行判斷,是否該行所有元素都非零
R.any() // any(R(:)) //是否有元素非零
R.colwise().any() // any(R) //按列判斷,是否該列有元素都非零
R.rowwise().any() // any(R, 2) //按行判斷,是否該行有元素都非零
// 點積,範數等
// Eigen // Matlab // 註釋
x.norm() // norm(x). //範數(注意:Eigen中沒有norm(R))
x.squaredNorm() // dot(x, x) //平方和(注意:對於複數而言,不等價)
x.dot(y) // dot(x, y) //點積
x.cross(y) // cross(x, y) //交叉積,需要頭文件 #include <Eigen/Geometry>
//// 類型轉換
// Eigen // Matlab // 註釋
A.cast<double>(); // double(A) //變成雙精度類型
A.cast<float>(); // single(A) //變成單精度類型
A.cast<int>(); // int32(A) //編程整型
A.real(); // real(A) //實部
A.imag(); // imag(A) //虛部
// 如果變換前後的類型相同,不做任何事情。
// 注意:Eigen中,絕大多數的涉及多個操作數的運算都要求操作數具有相同的類型
MatrixXf F = MatrixXf::Zero(3,3);
A += F; // 非法。Matlab中允許。(單精度+雙精度)
A += F.cast<double>(); // 將F轉換成double,並累加。(一般都是在使用時臨時轉換)
// Eigen 可以將已存儲數據的緩存 映射成 Eigen矩陣
float array[3];
Vector3f::Map(array).fill(10); // create a temporary Map over array and sets entries to 10
int data[4] = {1, 2, 3, 4};
Matrix2i mat2x2(data); // 將 data 複製到 mat2x2
Matrix2i::Map(data) = 2*mat2x2; // 使用 2*mat2x2 覆寫data的元素
MatrixXi::Map(data, 2, 2) += mat2x2; // 將 mat2x2 加到 data的元素上 (當編譯時不知道大小時,可選語法)