Ask Your Question
0

Access values from Mat

asked 2013-02-12 07:59:30 -0600

UserOpenCV gravatar image

updated 2013-02-12 08:15:54 -0600

Hi
For example, I have a matrix M of size 10x10 and I have a column matrix ind of length 5
How can I assign A(ind,:) to a new matrix B in C++ with OpenCV?

Below is how I do in Matlab:

A = [ 41     8    33    36    22    14    38    43    18     4
    46    49     2     2    20    34    13    13    42     3
     7    48    43    14    39    33    26    41    30    27
    46    25    47     3    40     9    35    13    28    39
    32    41    34     5    10     6    45    47    46    47
     5     8    38    42    25    25    48    18    15     7
    14    22    38    35    23    48    28    10    38    29
    28    46    20    16    33    18     7    13    38    24
    48    40    33    48    36    30     8    31    20     1
    49    48     9     2    38    12    13    24    29    17]

ind = [2; 8; 4; 6; 2]  

B = A(ind, :);

B = [ 46    49     2     2    20    34    13    13    42     3
    28    46    20    16    33    18     7    13    38    24
    46    25    47     3    40     9    35    13    28    39
     5     8    38    42    25    25    48    18    15     7
    46    49     2     2    20    34    13    13    42     3]

Can anyone tell me how to do this in C++ with OpenCV without using for loop

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2013-02-12 08:50:52 -0600

Guanta gravatar image

updated 2013-02-12 08:52:06 -0600

If I have understood you correctly you'd like to have the rows specified in your ind-matrix in a new matrix. You need to use the 'row()' and 'col()'-Functions and copy them to the B-matrix which need to be initialized beforehand.

Taking your example above and assuming ind-matrix is a nx1-matrix with your row-indices of type int:

cv::Mat B(ind.rows, A.cols, A.type());
for( int i = 0; i < ind.rows; i++ ){
  cv::Mat from = A.row(ind.at<int>(i,0));
  cv::Mat dest = B.row(i);
  from.copyTo(dest);
}

Note: if you know your rows/ columns are continous you can use rowRange(), colRange().

edit flag offensive delete link more

Question Tools

Stats

Asked: 2013-02-12 07:59:30 -0600

Seen: 529 times

Last updated: Feb 12 '13