Convert openCV matrix to vector

Looks deceptively. In the end, we know that the std or openCV vector can be easily transformed into a matrix as follows:

vector<Point> iptvec(10); Mat iP(iptvec); 

The openCV cheatSheet suggests the opposite:

 vector<Point2f> ptvec = Mat_ <Point2f>(iP); 

However, there is one caveat: the matrix should have only one row or one column. To convert an arbitrary matrix, you must change:

 int sz = iP.cols*iP.rows; vector<Point2f> ptvec = Mat <Point2f>(iP.reshape(1, sz)); 

Otherwise, you will receive an error message:

* OpenCV error: statement failed (dims == 2 && (sizes [0] == 1 || sizes [1] == 1 || sizes [0] * sizes [1] == 0)) in the create / file file / home /.../ OpenCV-2.4.2 / modules / core / src / matrix.cpp, line 1385 ...

+4
source share
1 answer

Create a 2dim vector and fill each line. For instance:

 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); } 
+1
source

Source: https://habr.com/ru/post/1480017/


All Articles