有一种非常简单的方法可以从向量构造垫...只需执行以下操作:
vector<int> myVector;
Mat myMatFromVector(myVector,true); //the boolean is to define if you want to copy the data
这个构造函数的问题是每个向量的元素将被放置在矩阵的每一行中。我想要的是将向量的每个元素放置在矩阵的每一列中。
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]
最佳答案
要么指定矩阵的形状和类型,传递向量数据
// 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 上调用 reshape 来交换行沙列的数量(不改变任何数据)
// 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->row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8376880/