convert openCV matrix into vector

大城市里の小女人 提交于 2020-01-01 06:11:10

问题


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);

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 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...


回答1:


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);
}


来源:https://stackoverflow.com/questions/16473621/convert-opencv-matrix-into-vector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!