Ask Your Question
0

How to use push_back

asked 2018-04-04 12:20:51 -0600

yode gravatar image

updated 2018-04-05 00:09:11 -0600

berak gravatar image

The forum have some related post for how to use push_back. I can use it like

Mat mat = (Mat_<uchar>(2, 4) << 5, 6, 0, 4, 0, 1, 9, 9);
Mat mat2 = (Mat_<uchar>(2, 4) << 5, 77, 0, 4, 2, 3, 9, 6);
//success
mat.push_back(mat2);

But I note there are other two usage when I use F12 to see the definition. Such as

/** @brief Adds elements to the bottom of the matrix.
The methods add one or more elements to the bottom of the matrix. They emulate the corresponding
method of the STL vector class. When elem is Mat , its type and the number of columns must be the
same as in the container matrix.
@Param elem Added element(s).
 */
template<typename _Tp> void push_back(const _Tp& elem);

/** @overload
@Param elem Added element(s).
*/
template<typename _Tp> void push_back(const Mat_<_Tp>& elem);

/** @overload
@Param elem Added element(s).
*/
template<typename _Tp> void push_back(const std::vector<_Tp>& elem);

/** @overload
@Param m Added line(s).
*/
void push_back(const Mat& m);

I think it supports push_back a vector into Mat, but I can not run it normally

//fail
vector<vector<uchar>> v1 = { { 5, 77, 0, 4 },{ 2, 3, 9, 6 } };
mat.push_back(v1);
//fail
vector<uchar> v2 = { 2, 3, 9, 6 };
mat.push_back(v2);

Can anybody tell me something? What have I missed something?

edit retag flag offensive close merge delete

Comments

please, NO SCREENSHOTS OF TEXT.

berak gravatar imageberak ( 2018-04-04 23:55:15 -0600 )edit

@berak I will keep that in mind.

yode gravatar imageyode ( 2018-04-05 03:49:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-04-05 00:06:18 -0600

berak gravatar image

updated 2018-04-05 00:12:03 -0600

fail case 2:

vector<uchar> v2 = { 2, 3, 9, 6 };
mat.push_back(v2);

there is a conversion operator to Mat for your vector, but it makes a single column (4 rows) of it, while you would need a single row (the transposed version of it) for push_back:

// clumsy, but works:
mat.push_back(Mat(v2).t());

fail case 1:

// will never  work
vector<vector<uchar>> v1 = { { 5, 77, 0, 4 },{ 2, 3, 9, 6 } };
mat.push_back(v1);

yes, you can have a vector<T> here, but not a vector<vector<T>>

(there needs to be a defined DataType for everything, that can be in a Mat)

edit flag offensive delete link more

Comments

Hi, thanks for answer. But your Mat(v2).t() is a Mat but not a vector? It is same with my first success example

yode gravatar imageyode ( 2018-04-05 03:51:18 -0600 )edit

And it's seem 2d-vector cannot be convert into Mat? such as the code will fail vector<vector<uchar>> v = { { 5, 6, 0, 4 },{ 0, 1, 9, 9 } }; Mat t = Mat(v);

yode gravatar imageyode ( 2018-04-05 03:57:48 -0600 )edit
  1. yes, same as your 1st success example
  2. no way to do so. rather use cv::Mat in the 1st place, not vectors of vectors.
berak gravatar imageberak ( 2018-04-05 04:15:11 -0600 )edit

push_back() with a vector (w/o converting to Mat) will only work, if your Mat has a single column only.

berak gravatar imageberak ( 2018-04-05 04:18:09 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-04-04 12:20:51 -0600

Seen: 4,992 times

Last updated: Apr 05 '18