OpenCV向量到Mat,但不是element->行 [英] OpenCV vector to Mat but not element->row

查看:72
本文介绍了OpenCV向量到Mat,但不是element->行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种非常简单的方法可以根据矢量构造Mat,只需执行以下操作即可:

There is a very simple way to construct a Mat from a vector...just by doing:

vector<int> myVector;
Mat myMatFromVector(myVector,true); //the boolean is to define if you want to copy the data

此构造函数的问题是每个向量的元素都将放置在Matrix的每一行中.我想要的是将向量的每个元素放置在矩阵的每一列中.

The problem with this contructor is that each vector's element will be placed in each row of the Matrix. What I want is each element of my vector to be placed in each column of the matrix.

As is:
vector<int> = [1,2,3,4]
Matrix = [1;2;3;4]

I want:
vector<int> = [1,2,3,4]
Matrix = [1,2,3,4]

推荐答案

要么指定Matrix的形状和类型,然后传递矢量数据

Either specify the shape and type of the Matrix and pass the vector data

   // constructor for matrix headers pointing to user-allocated data
    Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
    Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);

或在Mat上调用重塑以交换行沙柱数(不更改任何数据)

Or call reshape on the Mat to swap the number of row sand columns ( doesn't change any data)

    // creates alternative matrix header for the same data, with different
    // number of channels and/or different number of rows. see cvReshape.
    Mat reshape(int _cn, int _rows=0) const;

这篇关于OpenCV向量到Mat,但不是element-&gt;行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆