简体   繁体   中英

Sorting cv::Mat in OpenCv

Is there an equivalent function in OpenCV similar to [srtd,srtdinds] = sort(dst,'ascend'); in Matlab? I have tried cv::sortIdx(source, dst, cv::SORT_ASCENDING); but it doesn't work. My source Mat contains a single column.

From the documentation of cv::sortIdx() :

Instead of reordering the elements themselves, it stores the indices of sorted elements in the output array.

This means that you will need to call cv::sort() to sort the elements themselves after calling cv::sortIdx() to replicate the behavior of Matlab's sort() function:

cv::Mat source = cv::Mat::eye(3,3,CV_32F), dst;
cv::sortIdx(source, dst, CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);
cv::sort(source, source, CV_SORT_EVERY_ROW + CV_SORT_ASCENDING);

Now dst contains the permuted indices, and source contains the sorted data itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM