Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);
std::cout<<A<<std::endl;
std::cout<<A.rows<<", "<<A.cols<<std::endl; //4 rows 1 col

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"?

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);
std::cout<<A<<std::endl;
std::cout<<A.rows<<", "<<A.cols<<std::endl; //4 rows 1 col

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

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

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

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

Don't know where is the error occur, should I treat this as a bug and report it?

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.

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

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?

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.

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;
}

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"?

EditEdit : 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;
}