Ask Your Question
0

How can I sort Mat with col order

asked 2015-01-27 22:42:21 -0600

dinl gravatar image

I have a Mat element, that I want to sort with col oder, so for example I have:

Mat A ={[2 1], [0 2], [1 2], [0 1]}

So, If I use:

cv::sort(A, dst, CV_SORT_EVERY_COLUMN + CV_SORT_ASCENDING);

They will order each column independently, but the expected result is to order the second column first, then if a draw happens it will sort by the first column:

Actual:

{[0 1], [0 1], [1 2], [2 2]}

Expected:

{[0 1], [2 1], [0 2], [1 2]}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-01-28 14:08:50 -0600

Guanta gravatar image

The result of OpenCV is absolutely correct! Each column is sorted individually.

If you want to sort by the second column you need to use cv::sortIdx() on the second column only ( you can use .col(1)) and then use these indices, i.e. create a new matrix and copy each row from A to the new matrix according to this index.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-01-27 22:42:21 -0600

Seen: 4,061 times

Last updated: Jan 28 '15