Ask Your Question

Revision history [back]

A matrix is not necessary 2D, it could be 1D, 3D or anything else. To transform a 2D matrix into a 1D matrix (aka a vector), you could use the reshape function:

Mat vec = myImage.reshape(0,1);

According to the doc, nothing is copied, so if you need to copy the data use the clone method on the result.

If your question is how to convert your data into a STL vector, and guessing your images are in CV_32FC1, it would be:

std::vector< float > vgradx;
// reserve the size for your data
vgradx.reserve( vec.cols );
// copy data
memcpy( &(vgradx[0]), vec.data, vec.cols * sizeof( float ) );

Your matrix need to be continuous in memory to do that, check it with isContinuous(). If your image isn't in float, use the convertTo function before the copy.