push back element to the column of cv::Mat
How could I push back the floating point value to the column of cv::Mat?
cv::Mat_<float> A;
A.push_back(0);
A.push_back(1);
A.push_back(2);
A.push_back(3);
Suppose I want to make the matrix become 2 rows 2 cols like this [0, 1; 2, 3].How could I push the value into the column of A with type "float"?
Edit : exception thrown
A = A.reshape(1,2); //this line would crash in openCV 2.4.5
cv::Mat_<float> B = A.reshape(1, 2); //ok, wouldn't crash, but the buffer are different
std::cout<<(B.data == A.data)<<std::endl; // answer is 0
Exception : "Assertion failed (!fixedType() || ((Mat)obj)->type() == mtype) in create, file /Users/yyyy/Downloads/opencv-2.4.5/modules/core/src/matrix.cpp, line 1345"*
Don't know where is the error occur, should I treat this as a bug and report it?
Edit 2 : unsafe codes?
I found something interesting, in the class Mat there are a flag
/*! includes several bit-fields:
- the magic signature
- continuity flag
- depth
- number of channels
*/
int flags;
The flags is int, that means the behavior of the sign bit may not work as the programmer expect. When we call reshape, the Mat will do some operation on the flag
hdr.flags = (hdr.flags & ~CV_MAT_CN_MASK) | ((new_cn-1) << CV_CN_SHIFT);
And the function fixedType() and type() use the flags to check the type of Mat
core/include/opencv2/core/mat.inl.hpp
inline
int Mat::type() const
{
return CV_MAT_TYPE(flags);
}
modules/core/src/matrix.cpp
bool _OutputArray::fixedType() const
{
return (flags & FIXED_TYPE) == FIXED_TYPE;
}
you could just call
A = A.reshape(1,2);
(that's not copying data, just adjusting rows/cols)Thanks, I tried it but A = A.reshape(1, 2) would throw exception; cv::Mat_<float> B = A.reshape(1, 2) wouldn't(memory will reallocate).If I change cv::Mat_<float> to cv::Mat, A = A.reshape(1, 2) is work too.The error message is "Assertion failed (!fixedType() || ((Mat*)obj)->type() == mtype) in create, file /Users/yyyy/Downloads/opencv-2.4.5/modules/core/src/matrix.cpp, line 1345"
interesting ( sorry, i did not actually test it )
funny thing is, that Mat and Mat_<float> behave differently here, with plain Mat it just works, Mat_<float> throws the mentionend exception.
and it's about the type, not the reshaped rows/cols. (what?)
also, why do you think, Mat B=A.reshape(1,2) is reallocating ?
wait, we have not solved, why Mat_<float> is behaving different from plain Mat, or did i miss something ?
(that's definitely worth inquiring, isn't it ?)
Mat_<float> m = ...
Mat_<float> m2 = m.reshape(1,2); // works
m = m.reshape(1,2); // above problem, if m is Mat_<float>
no problem, again, if it's just plain Mat. bug or something ?
what's puzzling me most here, - why is it throwing a type exception ?
Don't know, maybe the function Mat_ operator=(const Mat& m); has some problem when transform, finding the location of the source codes, please tell me where is it if you know, thanks.
I'm having the same problem. Did this ever get resolved?