Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV Mat crashes while push_back

Tags:

c++

c

opencv

mat

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?

like image 210
Yuchen Avatar asked Aug 18 '26 21:08

Yuchen


1 Answers

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

like image 121
berak Avatar answered Aug 20 '26 13:08

berak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!