I am trying to create a matrix of the following in OpenCV by using the push_back function.

[0, 0, 0;
 0, 0, 0;
 1, 1, 1;
 1, 1, 1]

This following code works.

Code I

Mat matrix( 0, 3, CV_32F ); 
Mat zero  = Mat::zeros( 2, 3, CV_32F ); 
Mat one   = Mat::ones( 2, 3, CV_32F ); 
matrix.push_back( zero ); 
matrix.push_back( one ); 

But the following will result in crashes.

Code II

Mat matrix( 0, 3, CV_32F ); 
matrix.push_back( Mat::zeros( 2, 3, CV_32F ) ); 
matrix.push_back( Mat::ones( 2, 3, CV_32F ) ); 

According to OpenCV documentation about push_back,

template<typename T> void Mat::push_back(const T& elem)

The only requirement is "the type of elem and the number of columns must be the same as in the container matrix". And I think Code II meets the requirement. Can anybody explain why Code II will result in crashes?

有帮助吗?

解决方案

that's interesting, maybe even a bug.

  • Code1 calls void Mat::push_back(const Mat& elems),

  • Code2 calls void Mat::push_back(const _Tp& elem) , which is clearly broken. it seems to get the MatExpr returned from Mat::zeros() wrong ( because it's non-const ? )

well, this one works, too ( but it's clearly not , what you wanted )

    Mat matrix( 0, 3, CV_32F ); 
    matrix.push_back( Mat( Mat::zeros( 2, 3, CV_32F) ) ); 
    matrix.push_back( Mat( Mat::ones( 2, 3, CV_32F ) ) ); 
    cerr << matrix << endl;

maybe make an issue here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top