opencv c++中的9种方式创建矩阵Mat

35 篇文章 6 订阅
28 篇文章 4 订阅

 //1 create a mat with random values

//2 create zeors or ones Mat

 //3 common attribute of Mat

//4 access element of Mat by at

//5 access element of Mat by ptr

//6 access element of Mat by ptr and is continous

 //7 get one row or one col of a Mat

//8 get several rows or several cols of a Mat

//9 get sub region of a Mat

#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;

Mat convertGray2Color(Mat img_gray) {
    Mat img_color(img_gray.rows, img_gray.cols, CV_32FC3);
    float tmp = 0;
    for (int r = 0; r < img_gray.rows; r++) {
        for (int c = 0; c < img_gray.cols; c++) {
            tmp = img_gray.at<float>(r, c);
            img_color.at<Vec3f>(r, c)[0] = abs(255 - tmp);   //blue
            img_color.at<Vec3f>(r, c)[1] = abs(127 - tmp);   //green
            img_color.at<Vec3f>(r, c)[2] = abs(0 - tmp);     //red
        } 
    }
    return img_color;
}

int main()
{
    std::cout << "Hello World!\n";
    //1 create a mat with random values
    Mat m = Mat(2, 3, CV_32FC(1));
    randu(m, Scalar::all(0), Scalar::all(255));
    //cout << "random m" << m << endl;
    //imshow("randomMat", convertGray2Color(m));
    //waitKey(0);

    //2 create zeors or ones Mat
    Mat m1;
    m1.create(2, 3, CV_32FC(1));
    Mat m_zero = Mat::zeros(2, 3, CV_32FC1);
    Mat m_one = Mat::ones(2, 3, CV_32FC1);

    //3 common attribute of Mat
    Mat m_initList = (Mat_<int>(2, 3) << 1, 2, 3, 4, 5, 6);
    cout << m_initList << endl;
    cout << "size=" << m_initList.size() << endl;
    cout << "row="<< m_initList.rows << endl;
    cout << "cols="<< m_initList.cols << endl;
    cout << "channels=" << m_initList.channels() << endl;
    cout << "areas=" << m_initList.total() << endl;
    cout << "dims=" << m_initList.dims << endl;

    //4 access element of Mat by at
    Mat m2 = (Mat_<int>(3, 2) << 11, 12, 33, 43, 51, 16);
    for (int r = 0; r < m2.rows; r++) {
        for (int c = 0; c < m2.cols; c++) {
            cout << m2.at<int>(r, c)<<",";
        }
        cout << endl;
    }
    //5 access element of Mat by ptr
    for (int r = 0; r < m2.rows; r++) {
        const int* ptr = m2.ptr<int>(r);
        for (int c = 0; c < m2.cols; c++) {
            cout << ptr[c] << ",";
        }
        cout << endl;
    }
    //6 access element of Mat by ptr and is continous
    if (m2.isContinuous()) {
        int* ptr = m2.ptr<int>(0);
        for (int n = 0; n<m2.rows * m2.cols; n++) {
            cout << ptr[n] << ",";
        }
    }
    //Vec
    Vec<int, 3>vi(21, 32, 14);
    cout << "rows=" << vi.rows << endl;
    cout << "cols=" << vi.cols << endl;
    cout << "vi[0]=" << vi[0] << endl;
    cout << "vi(0)=" << vi(0) << endl;

    //7 get one row or one col of a Mat
    int r = 0, c=0;
    Mat mr = m2.row(r);
    Mat mc = m2.col(c);
    cout << "row r of m2" << mr << endl;
    cout << "col c of m2" << mc << endl;
    //8 get several rows or several cols of a Mat
    Mat m3 = (Mat_<int>(5, 5) << 1, 2, 3, 4, 5, 6, 
        7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 
        19, 20, 21, 22, 23, 24, 25);
    Mat r_range = m3.rowRange(Range(2, 4));
    cout << "r_range"<< r_range << endl;
    Mat c_range = m3.colRange(Range(1, 3));
    cout << "c_range" << r_range << endl;

    Mat r_range_clone1 = m3.rowRange(2, 4).clone();
    Mat r_range_clone2;
    m3.rowRange(2, 4).copyTo(r_range_clone2);
    //9 get sub region of a Mat
    Mat roi1 = m3(Rect(Point(2, 1), Point(3, 2)));  //point at left up corner, and right down corner
    Mat roi2 = m3(Rect(2, 1, 2, 2));  //x,y,width, height
    Mat roi3 = m3(Rect(Point(2, 1), Size(2, 2)));  //point at left up corner, and size


    return EXIT_SUCCESS;
}

 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

oceanstonetree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值