Ask Your Question
0

cv::sortIdx opencv 3.2

asked 2017-07-31 06:56:54 -0600

Grillteller gravatar image

I want to sort a matrix' row and get the indexes as a result. Therefor I use the function cv::sortIdx.

For a row like [40, 30, 100, 110, 10] I would expect the index vector to be [2, 1, 3, 4, 0]. This is also what I want to get as a result. Is this a correct assumption? In my code I am getting [4, 1, 0, 2, 3].

Mat unsorted(1, 5, CV_32F);
unsorted.at<int>(0, 0) = 40;
unsorted.at<int>(0, 1) = 30;
unsorted.at<int>(0, 2) = 100;
unsorted.at<int>(0, 3) = 110;
unsorted.at<int>(0, 4) = 10;

Mat sorted;
cv::sortIdx(unsorted, sorted, SORT_EVERY_ROW + SORT_ASCENDING);

cout << sorted.at<int>(0, 0) << " " << sorted.at<int>(0, 1) << " " << sorted.at<int>(0, 2) << " " << sorted.at<int>(0, 3) << " " << sorted.at<int>(0, 4) << " " << endl;

Is this a bug (referring to https://github.com/opencv/opencv/issu...) or do I have a lack of understanding of sortIdx?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-07-31 07:20:49 -0600

berak gravatar image

it's just a misassumption, i guess. admittedly, the example is not helpful, too.

but what you get here, are the indices of the original, not the sorted array.

10 is the smallest element, and 4 is the index of it in your original Mat, so that goes front, while 110 at index 3 is the largest, so that goes to the end of the indices.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-07-31 06:56:54 -0600

Seen: 2,308 times

Last updated: Jul 31 '17