当前位置:   article > 正文

Mat矩阵初始化和访问_mat 矩阵初始化

mat 矩阵初始化

Mat矩阵初始化和访问

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

int main()
{
    /* Mat矩阵初始化 */
    // 1. 直接初始化
    Mat mat1(2, 2, CV_32FC3, Scalar(0, 0, 255));
    // 2. 利用特殊函数初始化
    Mat mat2 = Mat::eye(3, 3, CV_64F);
    // 3. 较小的mat矩阵初始化
    Mat mat3 = (Mat_<double>(3, 3) << 0, 2, 4, 9, 1, 2, 4, 9, 0);
    // 4. 浅拷贝,共享指针
    Mat mat4 = mat3;
    //5. 新建一个头指针,拷贝初始化
    Mat mat5_1 = mat1.row(0).clone();
    Mat mat5_2;
    mat1.row(0).copyTo(mat5_2);

    /* 访问Mat矩阵中的元素 */
    // 1. 利用Mat行列指针访问
    int nRows = mat1.rows;
    int nCols = mat1.cols * mat1.channels();
    float *p; // 指针类型要与Mat矩阵类型相同
    for(int i = 0; i < nRows; i++)
    {
        p = mat1.ptr<float>(i);
        for(int j = 0; j < nCols; j++)
        {
            // 这里只能赋值 不能修改吗???
            cout << p[j] << endl;
        }
    }

    // 2. 使用at<>访问矩阵元素
    for(int y = 0; y < mat1.rows; y++)
    {
        for(int x = 0; x < mat1.cols; x++)
        {
            cout << mat1.at<Vec3f>(y, x)[0] << mat1.at<Vec3f>(y, x)[1] << mat1.at<Vec3f>(y, x)[2] << endl;
        }
    }

    // 3. 使用迭代器访问
    MatIterator_<Vec3f> itm, itmEnd;
    for( itm = mat1.begin<Vec3f>(), itmEnd = mat1.end<Vec3f>(); itm != itmEnd; itm++)
    {
        cout << (*itm)[0] << "..." << (*itm)[1] << "..." << (*itm)[2] << endl;
    }

    return 0;
}
  • 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
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

github链接:https://github.com/purse1996/opencv_example

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/886848
推荐阅读
相关标签
  

闽ICP备14008679号