//input matrix must be of type CV_8UC1
void MatrixSort(Mat matrix)
{
//matrix that stores the indices from sortIdx
Mat matrixRowIndices(matrix.rows, matrix.cols, CV_8UC1);
sortIdx(matrix, matrixRowIndices, SORT_EVERY_ROW + SORT_ASCENDING);
//reconstruate the sorted matrix from the row indices
Mat sortedRowmatrix(matrix.rows, matrix.cols, CV_8UC1);
for (int row = 0; row < matrixRowIndices.rows; row++)
{
for (int col = 0; col < matrixRowIndices.cols; col++)
{
int pukIDIndex = matrixRowIndices.at<int>(row, col);
int distance = matrix.at<uchar>(row, pukIDIndex);
sortedRowmatrix.at<uchar>(row, col) = distance;
}
}
cout << "sorted row indices = " << endl << matrixRowIndices << endl << endl;
cout << "sorted row matrix = " << endl << sortedRowmatrix << endl << endl;
Mat matrixColIndices(matrix.rows, matrix.cols, CV_8UC1);
sortIdx(sortedRowmatrix, matrixColIndices, SORT_EVERY_COLUMN + SORT_ASCENDING);
cout << "sorted col indices= " << endl << matrixColIndices << endl << endl;
Mat sortedColmatrix(matrix.rows, matrix.cols, CV_8UC1);
for (int row = 0; row < sortedColmatrix.rows; row++)
{
for (int col = 0; col < sortedColmatrix.cols; col++)
{
int pukIDIndex = matrixColIndices.at<int>(row, col);
int pukIDIndex2 = matrixRowIndices.at<int>(row, col);
int distance = matrix.at<uchar>(pukIDIndex, pukIDIndex2);
sortedColmatrix.at<uchar>(row, col) = distance;
}
}
cout << "sorted matrix indices = " << endl << sortedColmatrix << endl << endl;
Mat sortedDistanceMat(matrix.rows, matrix.cols, CV_8UC1);
cv::sort(matrix, sortedDistanceMat, SORT_EVERY_ROW + SORT_ASCENDING);
cv::sort(sortedDistanceMat, sortedDistanceMat, SORT_EVERY_COLUMN + SORT_ASCENDING);
cout << "correctly sorted matrix = " << endl << sortedDistanceMat << endl << endl;
}
void MatrixSort(Mat matrix)
{
//matrix that stores the indices from sortIdx
Mat matrixRowIndices(matrix.rows, matrix.cols, CV_8UC1);
sortIdx(matrix, matrixRowIndices, SORT_EVERY_ROW + SORT_ASCENDING);
//reconstruate the sorted matrix from the row indices
Mat sortedRowmatrix(matrix.rows, matrix.cols, CV_8UC1);
for (int row = 0; row < matrixRowIndices.rows; row++)
{
for (int col = 0; col < matrixRowIndices.cols; col++)
{
int pukIDIndex = matrixRowIndices.at<int>(row, col);
int distance = matrix.at<uchar>(row, pukIDIndex);
sortedRowmatrix.at<uchar>(row, col) = distance;
}
}
cout << "sorted row indices = " << endl << matrixRowIndices << endl << endl;
cout << "sorted row matrix = " << endl << sortedRowmatrix << endl << endl;
Mat matrixColIndices(matrix.rows, matrix.cols, CV_8UC1);
sortIdx(sortedRowmatrix, matrixColIndices, SORT_EVERY_COLUMN + SORT_ASCENDING);
cout << "sorted col indices= " << endl << matrixColIndices << endl << endl;
Mat sortedColmatrix(matrix.rows, matrix.cols, CV_8UC1);
for (int row = 0; row < sortedColmatrix.rows; row++)
{
for (int col = 0; col < sortedColmatrix.cols; col++)
{
int pukIDIndex = matrixColIndices.at<int>(row, col);
int pukIDIndex2 = matrixRowIndices.at<int>(row, col);
int distance = matrix.at<uchar>(pukIDIndex, pukIDIndex2);
sortedColmatrix.at<uchar>(row, col) = distance;
}
}
cout << "sorted matrix indices = " << endl << sortedColmatrix << endl << endl;
Mat sortedDistanceMat(matrix.rows, matrix.cols, CV_8UC1);
cv::sort(matrix, sortedDistanceMat, SORT_EVERY_ROW + SORT_ASCENDING);
cv::sort(sortedDistanceMat, sortedDistanceMat, SORT_EVERY_COLUMN + SORT_ASCENDING);
cout << "correctly sorted matrix = " << endl << sortedDistanceMat << endl << endl;
}