Matx为OpenCV轻量级的矩阵,被称为fixed matrix classes,意思是每个矩阵的大小都是固定的,主要是应对矩阵数据比较小的场景,最新的版本4.0不超过6*6大小的矩阵,旧版本一般不超过5*5,它的出现主要是应对小数据场景,因为是fixed matrix,所以其占用空间大小都是在编译阶段都是已知且固定好的,因为为轻量级的,数据较小,相对Mat,其申请和释放都是非常快速的,一般比较适合用在栈中场景中,Matx特点就是快。下面为“Learning OpenCv3”对其说明的一段话:

The fixed matrix classes are for matrices whose dimensions are known at compile time(hence "fixed").As a result, all memory for their data is allocated on the stacj, which means that they allocate and clean up quickly..Operations on them are fast, and there are specially optimized implemnetations for small matrices(2*2,3*3, etc), The fixed matrix classes are also central to many of the other basic types ub the C++ interface to OpenCV.

 Matx预定义

OpenCV中与定义了一系列的Matx,其格式为:

cv::Matx{1,2,...}{}1,2,...}{f,d}

1,2,...:二维矩阵两个维度大小

 f,d:为数据类型,f为float, d为double

提前定义的大小如下:

typedef Matx<float, 1, 2> Matx12f;
typedef Matx<double, 1, 2> Matx12d;
typedef Matx<float, 1, 3> Matx13f;
typedef Matx<double, 1, 3> Matx13d;
typedef Matx<float, 1, 4> Matx14f;
typedef Matx<double, 1, 4> Matx14d;
typedef Matx<float, 1, 6> Matx16f;
typedef Matx<double, 1, 6> Matx16d;

typedef Matx<float, 2, 1> Matx21f;
typedef Matx<double, 2, 1> Matx21d;
typedef Matx<float, 3, 1> Matx31f;
typedef Matx<double, 3, 1> Matx31d;
typedef Matx<float, 4, 1> Matx41f;
typedef Matx<double, 4, 1> Matx41d;
typedef Matx<float, 6, 1> Matx61f;
typedef Matx<double, 6, 1> Matx61d;

typedef Matx<float, 2, 2> Matx22f;
typedef Matx<double, 2, 2> Matx22d;
typedef Matx<float, 2, 3> Matx23f;
typedef Matx<double, 2, 3> Matx23d;
typedef Matx<float, 3, 2> Matx32f;
typedef Matx<double, 3, 2> Matx32d;

typedef Matx<float, 3, 3> Matx33f;
typedef Matx<double, 3, 3> Matx33d;

typedef Matx<float, 3, 4> Matx34f;
typedef Matx<double, 3, 4> Matx34d;
typedef Matx<float, 4, 3> Matx43f;
typedef Matx<double, 4, 3> Matx43d;

typedef Matx<float, 4, 4> Matx44f;
typedef Matx<double, 4, 4> Matx44d;
typedef Matx<float, 6, 6> Matx66f;
typedef Matx<double, 6, 6> Matx66d;

上述为一系列提前定义的Matx矩阵。

Matx数据存储

Matx数据较小,要满足其快速特点,其数据存储比较简单,直接采用数组形式:

_Tp val[m*n]; //< matrix elements

 Matx类

Matx类的直接操作方法主要如下:

Method

Description

 Matx();

默认构造函数

 explicit Matx(_Tp v0)

1x1带参数构造函数

Matx(_Tp v0, _Tp v1)

1x2 or 2x1 矩阵构造函数

 Matx(_Tp v0, _Tp v1, _Tp v2)

1x3 or 3x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)

 1x4, 2x2 or 4x1  矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)

1x5 or 5x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)

1x6, 2x3, 3x2 or 6x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)

1x7 or 7x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)

1x8, 2x4, 4x2 or 8x1 矩阵构造函数

 Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)

1x9, 3x3 or 9x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)

1x10, 2x5 or 5x2 or 10x1  矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,

         _Tp v4, _Tp v5, _Tp v6, _Tp v7,

         _Tp v8, _Tp v9, _Tp v10, _Tp v11)

1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 矩阵构造函数

  Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,

         _Tp v4, _Tp v5, _Tp v6, _Tp v7,

         _Tp v8, _Tp v9, _Tp v10, _Tp v11,

         _Tp v12, _Tp v13)

1x14, 2x7, 7x2 or 14x1 矩阵构造函数

Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,

         _Tp v4, _Tp v5, _Tp v6, _Tp v7,

         _Tp v8, _Tp v9, _Tp v10, _Tp v11,

         _Tp v12, _Tp v13, _Tp v14, _Tp v15

1x16, 4x4 or 16x1  矩阵构造函数

Matx(const _Tp* vals)

使用数组初始化矩阵

 Matx(std::initializer_list<_Tp>)

使用list初始化矩阵

static Matx all(_Tp alpha)

所有矩阵的值初始化为alpha

 static Matx zeros()

所有矩阵的值初始化为0

 static Matx ones()

所有矩阵的值初始化为1

static Matx eye()

创建一个单位矩阵

static Matx diag(const diag_type& d)

初始化对角矩阵

 static Matx randu(_Tp a, _Tp b)

创建矩阵的值在范围在a和b,该矩阵为均匀分布

Matx randn(_Tp a, _Tp b)

创建矩阵其值均值为a, 间隔为b,该矩阵为正太分布

_Tp dot(const Matx<_Tp, m, n>& v)

两个矩阵点乘,各个元素相乘并相加

double ddot(const Matx<_Tp, m, n>& v)

两个矩阵点乘,返回数据精度为double

 template<typename T2> operator Matx<T2, m, n>()

将矩阵进行强转,比如m44f = (Matx44f) m44d,强制转换矩阵为Matx44f

template<int m1, int n1> Matx<_Tp, m1, n1> reshape()

修改矩形的reshape,修改前后其矩阵元素个数总数必须一样

template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int base_row, int base_col)

生成一个矩阵镜像即取其一个子矩阵,base_row和base_col分别为原矩阵的行和列的偏移

m1和n1为生成镜像矩阵大小

Matx<_Tp, 1, n> row(int i)

选取原矩阵第i行

Matx<_Tp, m, 1> col(int i)

选取原矩阵第i列

diag_type diag() 

返回原矩阵的所有处于对角行值

Matx<_Tp, n, m> t()

矩阵专置

Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) 

矩阵的逆

template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU)

线性解决:solve linear system

Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method)

线性解决:solve linear system

 Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) 

两个矩阵相对应位的乘

Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a)

按元素划分两个矩阵:divide two matrices element-wise

const _Tp& operator ()(int row, int col)

按照row和col获取矩阵中的元素值

 _Tp& operator ()(int row, int col)

按照row和col获取矩阵中的元素值

const _Tp& operator ()(int i)

一维场景下获取矩阵中的值

 _Tp& operator ()(int i)

一维场景下获取矩阵中的值

Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)

矩阵相加运算

Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)

矩阵相减运算

template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)

矩阵缩放运算

 Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)

矩阵相乘运算

Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)

矩阵相除运算

template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)

矩阵乘运算

Matx(const Matx<_Tp, n, m>& a, Matx_TOp)

矩阵转置

 Matx类用例

1:Matx类用例,使用带参数的初始化一个3*3的矩阵:

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main()
{
	Matx33f m0(1, 2, 3, 4, 5, 6, 7, 8, 9);

	cout << "m0=:" << m0 << endl;
}

结果:

opencv 显示 矩阵视频 opencv datamatrix_Matx

2:使用数组初始化构建:

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main()
{
	float matx_value[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	Matx33f m0(matx_value);

	cout << "m0=:" << m0 << endl;
}

3:all初始化

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main()
{
	Matx33f m0 = Matx33f::all(10);

	cout << "m0=:" << m0 << endl;
}

运行结果:

 

opencv 显示 矩阵视频 opencv datamatrix_初始化_02

4:zero,one,eye初始化

#include <stdio.h>
#include "opencv2/opencv.hpp"

using namespace cv;
using namespace std;

void main()
{
	Matx33f m0 = Matx33f::ones();
	Matx33f m1 = Matx33f::zeros();
	Matx33f m2 = Matx33f::eye();

	cout << "m0=:" << m0 << endl;
	cout << "m1=:" << m1 << endl;
	cout << "m2=:" << m2 << endl;
}

运行结果: 

 

opencv 显示 矩阵视频 opencv datamatrix_Matx_03

Matx类中的变量

Matx类中的变量,Matx中存在比较重要的变量如下:

enum {
           rows     = m,
           cols     = n,
           channels = rows*cols,
#ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
           depth    = traits::Type<_Tp>::value,
           type     = CV_MAKETYPE(depth, channels),
#endif
           shortdim = (m < n ? m : n)
         };

    typedef _Tp                           value_type;
    typedef Matx<_Tp, m, n>               mat_type;
    typedef Matx<_Tp, shortdim, 1> diag_type;

rows为矩阵的行

cols为矩阵的列

channels:矩阵的元素总数

depth:深度即单个元素数据类型占用空间大小

type:矩阵的类型

shortdim:m和n中最小的,用于存储其对角矩阵

typedef _Tp                           value_type:元素数据类型

typedef Matx<_Tp, m, n>               mat_type;矩阵类型

typedef Matx<_Tp, shortdim, 1> diag_type:用于存储其矩阵对角元素值。

Matx重构operator

Matx重构operator操作:

operator

Method

 +=

template<typename _Tp1, typename _Tp2, int m, int n> static inline

Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)

-=

template<typename _Tp1, typename _Tp2, int m, int n> static inline

Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)

+

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)

-

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)

 *=

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)

*

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a)

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)

template<typename _Tp, int m, int n, int l> static inline

Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)

emplate<typename _Tp, int m, int n> static inline

Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)

-

template<typename _Tp, int m, int n> static inline

Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)

==

template<typename _Tp, int m, int n> static inline

bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)

!=

template<typename _Tp, int m, int n> static inline

bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)