简体   繁体   中英

copy column vector from a matrix into each col of another matrix in opencv

This may be simple, but definetly I couldn't find an efficient answer, sorry ...

Lets say that I have a matrix A, and I want to copy its first column several times into the matrix B; ie;

A = [1 2 3; 2 3 4; 5 6 7] A = [1 2 3; 2 3 4; 5 6 7] and I want to extract

A(:,1);

and copy this vector into another matrix, B. thus leading us to

B = [1 2 3; 1 2 3; 1 2 3];

after the next loop, then, B will be:

B = [2 3 4; 2 3 4; 2 3 4];

and so on until I get sequentially all the A's columns in matrix B

Is there a practical solution for this in opencv? i've tried with copyTo(), Range, and row, col, but I get nothing. I'll really appreciate your help.

regards;

jenn.

Simply use Mat::row to access each row of your Matrix in a loop and use Mat::copyTo to copy selected row to new Mat

See example

Mat A=(Mat_<uchar>(3,3)<< 1,2,3,\
                          2,3,4,\
                          5,6,7);

Mat B(A.rows,A.cols,CV_8UC1);


for(int i=0;i<A.rows;i++) {
  for(int j=0;j<A.rows;j++){
    A.row(i).copyTo(B.row(j));
  }
 cout<<B<<endl;
}

You can do this using Mat::push_back as well.

See example

Mat A=(Mat_<uchar>(3,3)<< 1,2,3,\
                          2,3,4,\
                          5,6,7);

 for(int i=0;i<A.rows;i++) {
    Mat B;
    for(int j=0;j<A.rows;j++){
      B.push_back(A.row(i));
     }
     cout<<B<<endl;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM