From: Matthew Self Date: Sat, 27 Aug 2016 03:39:38 +0000 (-0700) Subject: Improve perfomance of median calculation in LMedS algorithm X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~1612^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7de2e1e7957075ebbd504b2c9aea256e61b2d547;p=platform%2Fupstream%2Fopencv.git Improve perfomance of median calculation in LMedS algorithm * Use `nth_element()` to find the median instead of `sort()` in `LMeDSPointSetRegistrator::run()` * Improves performance of this part of LMedS from `n log(n)` to `n` by avoiding doing a full sort. * Makes LMedS 2x faster for 100 points, 4x faster for 5,000 points in `EstimateAffine2D()`. * LMedS is now never more than 2x slower than RANSAC and is faster in some cases. --- diff --git a/modules/calib3d/src/ptsetreg.cpp b/modules/calib3d/src/ptsetreg.cpp index 830fa8a..5e652d4 100644 --- a/modules/calib3d/src/ptsetreg.cpp +++ b/modules/calib3d/src/ptsetreg.cpp @@ -344,10 +344,8 @@ public: else errf = err; CV_Assert( errf.isContinuous() && errf.type() == CV_32F && (int)errf.total() == count ); - std::sort(errf.ptr(), errf.ptr() + count); - - double median = count % 2 != 0 ? - errf.at(count/2) : (errf.at(count/2-1) + errf.at(count/2))*0.5; + std::nth_element(errf.ptr(), errf.ptr() + count/2, errf.ptr() + count); + double median = errf.at(count/2); if( median < minMedian ) {