问题描述
看似容易.毕竟,我们知道可以将std或openCV向量轻松转换为Matrix,如下所示:
Looks deceptively easy. After all we know that an std or openCV vector can be easily converted into Matrix like this:
vector<Point> iptvec(10);
Mat iP(iptvec);
在openCV cheatSheet中建议相反:
The reverse is suggested in openCV cheatSheet:
vector<Point2f> ptvec = Mat_ <Point2f>(iP);
但是,有一个警告:矩阵必须只有一行或一列.要转换任意矩阵,您必须重塑形状:
However, there is one caveat: the matrix has to have only one row or one column. To convert an arbitrary matrix you have to reshape:
int sz = iP.cols*iP.rows;
vector<Point2f> ptvec = Mat <Point2f>(iP.reshape(1, sz));
否则,您将得到一个错误:
Otherwise you will get an error:
* OpenCV错误:声明失败(尺寸== 2&&(尺寸[0] == 1 ||尺寸[1] == 1 ||尺寸[0] *尺寸[1] == 0) ),在文件/home/.../OpenCV-2.4.2/modules/core/src/matrix.cpp,第1385行...
*OpenCV Error: Assertion failed (dims == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in create, file /home/.../OpenCV-2.4.2/modules/core/src/matrix.cpp, line 1385...
推荐答案
创建2dim向量并填充每一行.例如:
Create a 2dim vector and fill each row. E.g:
Mat iP=Mat::zeros(10, 20, CV_8UC1);
vector<vector<int>> ptvec;
for (int i = 0; i < iP.rows; i++)
{
vector<int> row;
iP.row(i).copyTo(row);
ptvec.push_back(row);
}
这篇关于将openCV矩阵转换为向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!