Access values ​​from matrix in OpenCV

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

The following shows how I do this 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 someone tell me how to do this in C ++ with OpenCV without using for loop

+4
source share
2 answers

There is no direct way to extract random ordering of rows / columns without any iteration. The easiest way is to extract the rows and place them in the target matrix one by one. If you have the declared matrix A and its data set:

 cv::Mat B; B.push_back(A(cv::Range(2,3),cv::Range::all())); B.push_back(A(cv::Range(8,9),cv::Range::all())); B.push_back(A(cv::Range(4,5),cv::Range::all())); B.push_back(A(cv::Range(6,7),cv::Range::all())); B.push_back(A(cv::Range(2,3),cv::Range::all())); 

should do what you want. operator()(cv::rowRange, cv::colRange) used to retrieve selected rows.

+4
source

I don't think this is possible without using a for loop, but the fastest way to do this is to use memcpy . You can see the full code here.

0
source

All Articles