Ask Your Question
0

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

asked 2014-06-26 06:45:45 -0600

abir gravatar image

updated 2014-06-26 07:13:16 -0600

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);
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2014-06-26 08:31:51 -0600

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.

edit flag offensive delete link more

Comments

thank you :)

abir gravatar imageabir ( 2014-06-30 03:17:04 -0600 )edit

Question Tools

Stats

Asked: 2014-06-26 06:45:45 -0600

Seen: 8,868 times

Last updated: Jun 26 '14