First time here? Check out the FAQ!

Ask Your Question
0

Convert vector 2D of Mat type to vector 1D ?

asked Jun 26 '14

abir gravatar image

updated Jun 26 '14

Hello I have a picture of Mat-type so it is a 2D vector. I want to convert it into a 1D vector. how can I do it knowing that I have already used this function it does not work?? please help me ..

/// Convert  mat Grad X  to vector
    vector< float> Vgrad_x;

    Vgrad_x.assignTo(smallImages_gradx,-1);
Preview: (hide)

1 answer

Sort by » oldest newest most voted
4

answered Jun 26 '14

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.

Preview: (hide)

Comments

thank you :)

abir gravatar imageabir (Jun 30 '14)edit

Question Tools

Stats

Asked: Jun 26 '14

Seen: 9,103 times

Last updated: Jun 26 '14