一、四则运算
1.符号运算
·直接利用数学符号表示矩阵的运算
·e = a + b;
·f = c - d;
·g = 2 * a;
·h = d/2.0;
·i = a - 1;
两个矩阵相乘
·矩阵乘积----------"*"
cij = ai1*b1j + ai2*b2j + ai3*b3j
·向量内积----------".dot"
f = d1*e1 + d2*e2 + d3*e3
·对应位元素乘积-------".mul()"
cij = aij*bij
2.opencv中提供的运算函数
可通过官网资料查看
示例
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv; //opencv的命名空间
using namespace std;
int main()
{
system("color F0"); //把界面由黑色变为白色
Mat a = (cv::Mat_<int>(3, 3) << 1, 2, 3, 4, 5, 6, 7, 8, 9);
Mat b = (cv::Mat_<int>(3, 3) << 1, 2, 3, 7, 8, 9, 4, 5, 6);
Mat c = (cv::Mat_<double>(3, 3) << 1.0, 2.1, 3.2, 4.0, 5.1, 6.2, 2, 2, 2);
Mat d = (cv::Mat_<double>(3, 3) << 1.0, 2.1, 3.2, 4.0, 5.1, 6.2, 2, 2, 2);
cout << "两个矩阵的和" << endl << a + b << endl;
cout << "两个矩阵的差" << endl << c - d << endl;
cout << "矩阵数乘" << endl << 2 * a << endl;
cout << "矩阵数除" << endl << d / 2.0 << endl;
cout << "矩阵减数" << endl << a - 1 << endl;
cout << "两矩阵相乘" << endl << c * d << endl;
cout << "矩阵内积" << endl << a.dot(b) << endl;
cout << "矩阵对应位相乘" << endl << a.mul(b) << endl;
cout << "两个矩阵最小值" << endl << min(a, b) << endl; //所给出的结果是每一位中的最小值的数
return 0;
}
结果