1
votes

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] 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.

1

1 Answers

1
votes

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;
  }