Mat多维数组初始化

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

int main()
{
    //定义一个float数组
    float a[4] = {1,2,3,4};
    //将数组转换为矩阵,2行2列的矩阵
    cv::Mat a_mat = cv::Mat(2,2,CV_32F,a);
    //输出矩阵
    std::cout << a_mat << std::endl;
    //查看矩阵的某个元素,查看矩阵第一行第二列的元素值
    std::cout << a_mat.at<float>(0, 1) << std::endl;
}

输出信息

[1, 2;
 3, 4]
2

Mat的dot

在使用Mat做dot需要注意,前一个矩阵列数必须等于后一个矩阵的行数

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

int main()
{
    //定义一个float数组
    float a[4] = {1,2,3,4};
    //将数组转换为矩阵,2行2列的矩阵
    cv::Mat a_mat = cv::Mat(2,2,CV_32F,a);
    float b[2] = { 5,6 };
    //将数组转换为2行一列的矩阵
    cv::Mat b_mat = cv::Mat(2, 1, CV_32F, b);
    //矩阵的dot
    cv::Mat c_mat = a_mat * b_mat;
    std::cout << c_mat << std::endl;
}

输出信息

[17;
 39]

Mat的Element-wise

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

int main()
{
    //定义一个float数组
    float a[4] = {1,2,3,4};
    //将数组转换为矩阵,2行2列的矩阵
    cv::Mat a_mat = cv::Mat(2,2,CV_32F,a);
    float b[4] = { 5,6,7,8 };
    //将数组转换为2行2列的矩阵
    cv::Mat b_mat = cv::Mat(2, 2, CV_32F, b);
    //矩阵的dot
    cv::Mat c_mat = a_mat.mul(b_mat);
    std::cout << c_mat << std::endl;
}

输出信息

[5, 12;
 21, 32]

 

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐