OpenCV Mat基本操作总结

(1)参与点乘的两个Mat矩阵的数据类型(type)只能是 CV_32F、 CV_64FC1、 CV_32FC2、 CV_64FC2 这4种类 型中的一种。若选用其他类型,比如CV_8UC1,编译器会报错。
如下列子会报错

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
    Mat r=(Mat_<float>(3,3)<<1,1,1,2,2,2,3,3,3);
    Mat t=(Mat_<double>(3,3)<<1,1,1,2,2,2,3,3,3);
    Mat result=r*t;
    cout<<result;
    return 0;
}

解决矩阵乘法类型不匹配的方法
使用 mat.convertTo方法转换数据类型
如下就顺利解决

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
    Mat r=(Mat_<float>(3,3)<<1,1,1,2,2,2,3,3,3);
    Mat t=(Mat_<double>(3,3)<<1,1,1,2,2,2,3,3,3);
    cout<<r.type()<<endl;
    cout<<t.type()<<endl;
    //convert to CV_32F type
    t.convertTo(t,CV_32F);
    cout<<t.type()<<endl;
    Mat result=r*t;
    cout<<result<<endl;
    return 0;
}

(2)获取Mat 中存的数据类型
如下

Mat m;
m.type();

(3)取Mat 的子序列
使用Range类用来指定连续的子序列
如mat.rowRange mat.colRange
下面例子取r的2*2子序列。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
int main() {
    Mat r=(Mat_<float>(3,3)<<1,1,1,2,2,2,3,3,3);
    Mat subr;
    Mat depthsubr;
    //浅拷贝
    subr=r.rowRange(0,2).colRange(0,2);
    //深拷贝
 r.rowRange(0,2).colRange(0,2).copyTo(depthsubr);
    cout<<subr<<endl;
    cout<<depthsubr<<endl;
    return 0;
}

(3)取Mat中的某行或某列
使用mat.rowRange和mat.colRange,如(2)例
(4)Mat加减乘除常数值
如下

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main() {
    Mat r=(Mat_<float>(2,2)<<1,1,2,2);
    Mat show;
    //1.add const value
    show=r+1;
    cout<<show<<endl;
    //2.subtract
    show=r-1;
    cout<<show<<endl;
    //3., multiply
    show=r*2;
    cout<<show<<endl;
    //4.divide
    show=r/2;
    cout<<show<<endl;
    return 0;
}

(5)Mat 矩阵的加减乘除运算及含义
+-代表矩阵对应位置的元素相加相减;
*代表矩阵相乘
/要注意是对应位置的元素的相除;
如下面的列子

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace cv;
int main() {
    Mat a=(Mat_<float>(2,2)<<1,1,1,1);
    Mat b=(Mat_<float>(2,2)<<1,2,3,4);
    Mat show;
    //1.add 
    //show=r.rows(0);
    show=a+b;
    cout<<show<<endl;
    //2.subtract
    show=a-b;
    cout<<show<<endl;
    //3., multiply
    show=a*b;
    cout<<show<<endl;
    //4.divide
    show=a/b;
    cout<<show<<endl;
    return 0;
}

结果如下

[2, 3;
  4, 5]
[0, -1;
  -2, -3]
[4, 6;
  4, 6]
[1, 0.5;
  0.33333334, 0.25]

(6)初始化Mat方法
a)全初始化为0
Mat initZero=Mat::zeros(2,2,CV_32F);
b)全初始化为1
Mat initOne= Mat::ones(2,2,CV_32F);
c)全初始化为对角阵
Mat initEye=Mat::eye(2,2,CV_32F);
(7)矩阵求逆和求转置
用inv()求逆,t()求转置

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main() {
    Mat r=(Mat_<float>(2,2)<<1,1,0,2);
    cout<<r<<endl;
    cout<<r.inv()<<endl;
    cout<<r.t()<<endl;
    return 0;
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值