OpenCV (c++) 数组计算: 矩阵

目录

加/减/乘/除1个数

叉乘

转置

统计-非0元素个数

均值/标准差

最值

cv::minMaxLoc


加/减/乘/除1个数

例子1

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat A(1,3,CV_32FC1);
    Mat B(3,2,CV_32FC1);
    Mat AB;
    vector<float> x = {-1,1,2};
    for (int i=0; i<x.size(); i++)
    { A.at<float>(0,i) = x[i];
      B.at<float>(i,0) = x[i]; 
      B.at<float>(i,1) = x[i]; }

    cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
    cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
    AB = B + 0.5;
    cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
    
    for (int i=0; i<AB.rows; i++)
    {   
        for (int j=0; j<AB.cols; j++)
        {
            cout << AB.at<float>(i,j) << ", ";
        }
        cout << endl;
    }
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
-0.5, -0.5, 
1.5, 1.5, 
2.5, 2.5,

例子2

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat A(1,3,CV_32FC1);
    Mat B(3,2,CV_32FC1);
    Mat AB;
    vector<float> x = {-1,1,2};
    for (int i=0; i<x.size(); i++)
    { A.at<float>(0,i) = x[i];
      B.at<float>(i,0) = x[i]; 
      B.at<float>(i,1) = x[i]; }

    cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
    cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
    AB = (B + 0.5) / 10;
    cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
    
    for (int i=0; i<AB.rows; i++)
    {   
        for (int j=0; j<AB.cols; j++)
        {
            cout << AB.at<float>(i,j) << ", ";
        }
        cout << endl;
    }
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
-0.05, -0.05, 
0.15, 0.15, 
0.25, 0.25, 

例子3

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat A(1,3,CV_32FC1);
    Mat B(3,2,CV_32FC1);
    Mat AB;
    vector<float> x = {-1,1,2};
    for (int i=0; i<x.size(); i++)
    { A.at<float>(0,i) = x[i];
      B.at<float>(i,0) = x[i]; 
      B.at<float>(i,1) = x[i]; }

    cout << "rows:" << A.rows << ", cols:" << A.cols << ", channels:" << A.channels() << endl;
    cout << "rows:" << B.rows << ", cols:" << B.cols << ", channels:" << B.channels() << endl;
    AB = (B + 0.5) / 10;
    cout << "rows:" << AB.rows << ", cols:" << AB.cols << ", channels:" << AB.channels() << endl;
    
    cout << A << endl;
    cout << B << endl;
    cout << AB << endl;
}
rows:1, cols:3, channels:1
rows:3, cols:2, channels:1
rows:3, cols:2, channels:1
[-1, 1, 2]
[-1, -1;
 1, 1;
 2, 2]
[-0.050000001, -0.050000001;
 0.15000001, 0.15000001;
 0.25, 0.25]

叉乘

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat A(1,3,CV_32FC1);
    Mat B(3,2,CV_32FC1);
    Mat AB;
    vector<float> x = {-1,1,2};
    for (int i=0; i<x.size(); i++)
    { A.at<float>(0,i) = x[i];
      B.at<float>(i,0) = x[i]; 
      B.at<float>(i,1) = x[i]; }

    AB = A * B;
    
    cout << A << endl;
    cout << B << endl;
    cout << AB << endl;
}
[-1, 1, 2]
[-1, -1;
 1, 1;
 2, 2]
[6, 6]

转置

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat B(2,3,CV_32FC1);
    vector<vector<float>> x = { {1,2,3},
                                {4,5,6}};
    for (int i=0; i<x.size(); i++)
    {
        for (int j=0; j<x[0].size(); j++)
        {
            B.at<float>(i,j) = x[i][j];
        }
    }  

    Mat B_trans;
    B_trans = B.t();

    cout << B << endl;
    cout << "-----------\n";
    cout << B_trans << endl;
}
[1, 2, 3;
 4, 5, 6]
-----------
[1, 4;
 2, 5;
 3, 6]

统计-非0元素个数

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat B(2,3,CV_32FC1);
    vector<vector<float>> x = { {1,0.00,3},
                                {4,0.0000001,6}};
    for (int i=0; i<x.size(); i++)
    {
        for (int j=0; j<x[0].size(); j++)
        { B.at<float>(i,j) = x[i][j]; }
    }  
    cout << cv::countNonZero(B) << endl;
}
5

均值/标准差

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat B(2,3,CV_32FC1);
    vector<vector<float>> x = { {1,2,3},
                                {4,5,6}};
    for (int i=0; i<x.size(); i++)
    {
        for (int j=0; j<x[0].size(); j++)
        { B.at<float>(i,j) = x[i][j]; }
    }  

    Mat mean, stddev;
    cv::meanStdDev(B,mean,stddev);

    cout << mean << endl;
    cout << stddev << endl;

    Mat process = (B - mean) / stddev;
    cout << process << endl;
}
[3.5]
[1.707825127659933]
[-1.4638501, -0.87831008, -0.29277003;
 0.29277003, 0.87831008, 1.4638501]

最值

cv::minMaxLoc
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main()
{
    Mat B(2,3,CV_32FC1);
    vector<vector<float>> x = { {2,3,1},
                                {4,5,6}};
    for (int i=0; i<x.size(); i++)
    {
        for (int j=0; j<x[0].size(); j++)
        { B.at<float>(i,j) = x[i][j]; }
    }  
    cout << B << endl;
    cout << "-------------\n";

    double maxValue,minValue;
    cv::Point maxIndex,minIndex;
    cv::minMaxLoc(B,&minValue,&maxValue,&minIndex,&maxIndex);
    cout << maxValue << ", " << maxIndex << endl;
    cout << minValue << ", " << minIndex << endl;
}
[2, 3, 1;
 4, 5, 6]
-------------
6, [2, 1]
1, [2, 0]

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值