基于C++的矩阵运算
程序员文章站
2022-07-11 17:57:10
...
矩阵运算
矩阵内积
完整代码如下:
#include “pch.h”
#include
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int juzhen(Mat D,Mat B);
int main()
{
Mat A = Mat::ones(3, 3, 0);
A.at<uchar>(0, 0) = 2;
A.at<uchar>(0, 1) = 2;
A.at<uchar>(0, 2) = 2;
A.at<uchar>(1, 0) = 0;
A.at<uchar>(1, 1) = 0;
A.at<uchar>(1, 2) = 0;
A.at<uchar>(2, 0) = 1;
A.at<uchar>(2, 1) = 1;
A.at<uchar>(2, 2) = 1;
Mat im = Mat::ones(3, 3, 0);
im.at<uchar>(0, 0) = 2;
im.at<uchar>(0, 1) = 2;
im.at<uchar>(0, 2) = 2;
im.at<uchar>(1, 0) = 0;
im.at<uchar>(1, 1) = 0;
im.at<uchar>(1, 2) = 0;
im.at<uchar>(2, 0) = 1;
im.at<uchar>(2, 1) = 1;
im.at<uchar>(2, 2) = 1;
cout << juzhen(A, im);
}
int juzhen(Mat D,Mat B)
{
int C=0;
int sum=0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
C = D.at(i, j) * B.at(i, j);
sum = sum + C;
}
}
return sum;
}