garak
garak

Reputation: 4813

Random order shuffle cv::Mat in OpenCV

Is there no function in OpenCV to randomly shuffle a matrx (ordered by rows)?

Input:

1 2 3
4 5 6
7 8 9

Output:

4 5 6
7 8 9
1 2 3

cv::randShuffle function only seems to randomly order the elements in the entire array I'm using the newer C++ API

Upvotes: 3

Views: 3679

Answers (1)

Jose F. Velez
Jose F. Velez

Reputation: 529

Code to shuffle the rows of a matrix:

cv::Mat shuffleRows(const cv::Mat &matrix)
{
  std::vector <int> seeds;
  for (int cont = 0; cont < matrix.rows; cont++)
    seeds.push_back(cont);

  cv::randShuffle(seeds);

  cv::Mat output;
  for (int cont = 0; cont < matrix.rows; cont++)
    output.push_back(matrix.row(seeds[cont]));

  return output;
}

Upvotes: 8

Related Questions