Ask Your Question
0

How can I get index of the gratest element in a Mat

asked 2017-03-28 02:23:08 -0600

ririgo gravatar image

I know that opencv has sort method, but I want to get something like argmax method. How can I get indices of the gratest element, and second, an third, and so on, from a 2D Mat?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-28 02:53:06 -0600

LBerger gravatar image

updated 2017-03-28 02:53:26 -0600

You can use sortIdx :

    Mat x = (Mat_<float>(5, 1) << 1, 4, 2, 5, 0);
    cout << x;;
    Mat key;
    sortIdx(x, key, SORT_EVERY_COLUMN);
    for (int i = 0; i < key.rows; i++)
        cout << x.at<float>(key.at<int>(i, 0)) << "\t";

with results :

[1;
 4;
 2;
 5;
 0]

0    1       2       4       5
edit flag offensive delete link more

Comments

Should I reshape a 2D Mat into a 1D Mat to use this solution?

ririgo gravatar imageririgo ( 2017-03-28 03:43:41 -0600 )edit

Yes flags are mutual exclusive

LBerger gravatar imageLBerger ( 2017-03-28 04:00:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-28 02:23:08 -0600

Seen: 464 times

Last updated: Mar 28 '17