Ask Your Question
0

OpenCV: append different vectors as one row

asked 2017-03-16 05:41:59 -0600

lovaj gravatar image

I have a cv::Mat1f vector which size is kxd. How can I fill it by appending k different 1xd vectors?

I want to do something like:

    int k = 3, d = 3;
    cv::Mat1f testMat(1,k*d);
    for(int i=0; i<k;i++){
        cv::Mat1f partial(1,d,i);
        testMat.push_back(partial);
    }
    std::cout<<testMat<<std::endl;

Notice that the example above is much simpler that my real case.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-03-16 05:55:54 -0600

berak gravatar image

you can only push_back elements with the same row-size of the original Mat. also note, that if you pre-allocate your testMat, new elements will get inserted at the bottom (which is probably not what you wanted)

if you wanted an 1d Mat as the outcome, try like this:

// push partial column vecs:
int k = 3, d = 3;
cv::Mat1f testMat;
for(int i=0; i<k;i++){
    cv::Mat1f partial(d,1,i);
    testMat.push_back(partial);
}
std::cout<<testMat.reshape(1,1)<<std::endl; //reshape() to make row-vec
// [0, 0, 0, 1, 1, 1, 2, 2, 2]

if you instead u wanted a 2d Mat, it goes like this:

// push partial row vecs:
cv::Mat1f testMat2;
for(int i=0; i<k;i++){
    cv::Mat1f partial(1,d,i);
    testMat2.push_back(partial);
}
std::cout<<testMat2<<std::endl; 
// [0, 0, 0;
//  1, 1, 1;
//  2, 2, 2]
edit flag offensive delete link more

Comments

thanks for your answer, I figured out the same solution by myself in the meantime, now I'm sure it's the best approach! :D

lovaj gravatar imagelovaj ( 2017-03-16 09:06:53 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-16 05:41:59 -0600

Seen: 504 times

Last updated: Mar 16 '17