【OpenCV教程】OpenCV中对矩阵的常用操作


1.全零矩阵

CV_NODISCARD_STD static MatExpr Mat::zeros(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::zeros(Size size, int type);

CV_NODISCARD_STD static MatExpr Mat::zeros(int ndims, const int* sz, int type);
//not recommended
  • 参数如下
参数含义
rows行数
cols列数
type数据类型(CV_16F)
sizeSize(宽(列数),高(行数))
  • Size与Mat中的成员函数.size()的返回值,有相同的数据类型,是[宽*高]。
  • Mat中的成员变量.size,与以上二者不同,是 rows*cols

2.全一矩阵

CV_NODISCARD_STD static MatExpr Mat::ones(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::ones(Size size, int type);

CV_NODISCARD_STD static MatExpr Mat::ones(int ndims, const int* sz, int type);
//not recommended
  • 参数如下
参数含义
rows行数
cols列数
type数据类型(CV_16F)
sizeSize(宽(列数),高(行数))

3.单位矩阵

CV_NODISCARD_STD static MatExpr Mat::eye(int rows, int cols, int type);
CV_NODISCARD_STD static MatExpr Mat::eye(Size size, int type);
  • 参数如下
参数含义
rows行数
cols列数
type数据类型(CV_16F)
sizeSize(宽(列数),高(行数))

4.矩阵转置

MatExpr Mat::t() const;

5.求逆矩阵

MatExpr Mat::inv(int method=DECOMP_LU) const;

6.逗号式分隔创建矩阵

  • 常用于自定义卷积核
template<typename _Tp> inline
Mat_<_Tp>::Mat_(int _rows, int _cols)
    : Mat(_rows, _cols, traits::Type<_Tp>::value)
{
}

template<typename _Tp> inline
Mat_<_Tp>::Mat_(int _rows, int _cols, const _Tp& value)
    : Mat(_rows, _cols, traits::Type<_Tp>::value)
{
    *this = value;
}

template<typename _Tp> inline
Mat_<_Tp>::Mat_(Size _sz)
    : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
{}

template<typename _Tp> inline
Mat_<_Tp>::Mat_(Size _sz, const _Tp& value)
    : Mat(_sz.height, _sz.width, traits::Type<_Tp>::value)
{
    *this = value;
}
  • 以下为使用实例,注意括号的位置
Mat a=(Mat_<int>(2,2)<<1,2,3,4);
Mat b=(Mat_<double>(Size(2,2))<<1,2,3,4);

注意 :给出的数据类型必须是基本数据类型,如int,double。不能是CV_16F等。

7.矩阵定义(只列出常用的)

Mat::Mat() CV_NOEXCEPT;
Mat::Mat(int rows, int cols, int type);
Mat::Mat(Size size, int type);
Mat::Mat(int rows, int cols, int type, const Scalar& s);
Mat::Mat(Size size, int type, const Scalar& s);
Mat::Mat(const std::vector<int>& sizes, int type);
Mat::Mat(const std::vector<int>& sizes, int type, const Scalar& s);
Mat::Mat(const Mat& m);

void Mat::create(int rows, int cols, int type);
void Mat::create(Size size, int type);
void Mat::create(const std::vector<int>& sizes, int type);
  • 参数如下
参数含义
rows行数
cols列数
type数据类型(CV_16F)
sizeSize(宽(列数),高(行数))

7.1 数据类型Scalar

  • Scalar(gray)
  • Scalar(blue,green,red)

8.通过ptr与at函数遍历矩阵

8.1 Vec类型

typedef Vec<uchar, 2> Vec2b;
typedef Vec<uchar, 3> Vec3b;
typedef Vec<uchar, 4> Vec4b;
 
typedef Vec<short, 2> Vec2s;
typedef Vec<short, 3> Vec3s;
typedef Vec<short, 4> Vec4s;
 
typedef Vec<ushort, 2> Vec2w;
typedef Vec<ushort, 3> Vec3w;
typedef Vec<ushort, 4> Vec4w;
 
typedef Vec<int, 2> Vec2i;
typedef Vec<int, 3> Vec3i;
typedef Vec<int, 4> Vec4i;
typedef Vec<int, 6> Vec6i;
typedef Vec<int, 8> Vec8i;
 
typedef Vec<float, 2> Vec2f;
typedef Vec<float, 3> Vec3f;
typedef Vec<float, 4> Vec4f;
typedef Vec<float, 6> Vec6f;
 
typedef Vec<double, 2> Vec2d;
typedef Vec<double, 3> Vec3d;
typedef Vec<double, 4> Vec4d;
typedef Vec<double, 6> Vec6d;
  • 以下为实例
Mat a(Size(2560,1440),CV_8UC3);
for(int i=0;i<a.rows;i++){
      for(int j=0;j<a.cols;j++){
          a.ptr(i,j)[0]=0;
          a.ptr(i,j)[1]=0;
          a.ptr(i,j)[2]=255;
      }
}
for(int i=0;i<a.rows;i++){
      for(int j=0;j<a.cols;j++){
          a.ptr<Vec3b>(i,j)[0]=0;
          a.ptr<Vec3b>(i,j)[1]=0;
          a.ptr<Vec3b>(i,j)[2]=255;
      }
}
for(int i=0;i<a.rows;i++){
      for(int j=0;j<a.cols;j++){
          a.at<Vec3b>(i,j)[0]=0;
          a.at<Vec3b>(i,j)[1]=0;
          a.at<Vec3b>(i,j)[2]=255;
      }
}
  • 用ptr访问可以不加Vec类型,ptr访问是最快的
  • 用at访问必须加Vec类型,at访问比ptr略微慢一些

9.通过迭代器遍历矩阵(easy but very very slow)

Mat a(Size(2560,1440),CV_8UC3);
for(auto iter=a.begin<Vec3b>();iter!=a.end<Vec3b>();iter++){
      iter[0]=255;
      iter[1]=0;
      iter[2]=0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UnderTurrets

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值