From 34ad9b8a42e185d2db61099af4cbc4ef55f3c9b6 Mon Sep 17 00:00:00 2001 From: branka-plateiq Date: Tue, 26 Jun 2018 06:40:28 -0700 Subject: [PATCH] Pass RANSAC parameters as function input (#10569) * Pass RANSAC parameters as function input * Clean up unnecessary code * Keep the original function signature * Clean up based on PR comments Replace array with vector. Correct naming convention for input variables. Add checks on input variables. * Use vector instead of array for dynamic size * Revert change. * Use dynamic array * Fix wrong syntax in array allocation * Undo change * Fix variable name * Use vector and not array * fixed compile warning on Windows --- modules/video/include/opencv2/video/tracking.hpp | 4 +- modules/video/src/lkpyramid.cpp | 47 ++++++++++++++---------- 2 files changed, 31 insertions(+), 20 deletions(-) diff --git a/modules/video/include/opencv2/video/tracking.hpp b/modules/video/include/opencv2/video/tracking.hpp index d397ac7..8e01d16 100644 --- a/modules/video/include/opencv2/video/tracking.hpp +++ b/modules/video/include/opencv2/video/tracking.hpp @@ -250,7 +250,9 @@ when fullAffine=false. @sa estimateAffine2D, estimateAffinePartial2D, getAffineTransform, getPerspectiveTransform, findHomography */ -CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst, bool fullAffine ); +CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst, bool fullAffine); +CV_EXPORTS_W Mat estimateRigidTransform( InputArray src, InputArray dst, bool fullAffine, int ransacMaxIters, double ransacGoodRatio, + int ransacSize0); enum diff --git a/modules/video/src/lkpyramid.cpp b/modules/video/src/lkpyramid.cpp index 48f896d..353e356 100644 --- a/modules/video/src/lkpyramid.cpp +++ b/modules/video/src/lkpyramid.cpp @@ -1412,7 +1412,7 @@ namespace cv { static void -getRTMatrix( const Point2f* a, const Point2f* b, +getRTMatrix( const std::vector a, const std::vector b, int count, Mat& M, bool fullAffine ) { CV_Assert( M.isContinuous() ); @@ -1489,15 +1489,18 @@ getRTMatrix( const Point2f* a, const Point2f* b, cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullAffine ) { + return estimateRigidTransform(src1, src2, fullAffine, 500, 0.5, 3); +} + +cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullAffine, int ransacMaxIters, double ransacGoodRatio, + const int ransacSize0) +{ CV_INSTRUMENT_REGION() Mat M(2, 3, CV_64F), A = src1.getMat(), B = src2.getMat(); const int COUNT = 15; const int WIDTH = 160, HEIGHT = 120; - const int RANSAC_MAX_ITERS = 500; - const int RANSAC_SIZE0 = 3; - const double RANSAC_GOOD_RATIO = 0.5; std::vector pA, pB; std::vector good_idx; @@ -1509,6 +1512,12 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA RNG rng((uint64)-1); int good_count = 0; + if( ransacSize0 < 3 ) + CV_Error( Error::StsBadArg, "ransacSize0 should have value bigger than 2."); + + if( ransacGoodRatio > 1 || ransacGoodRatio < 0) + CV_Error( Error::StsBadArg, "ransacGoodRatio should have value between 0 and 1"); + if( A.size() != B.size() ) CV_Error( Error::StsUnmatchedSizes, "Both input images must have the same size" ); @@ -1597,23 +1606,23 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA good_idx.resize(count); - if( count < RANSAC_SIZE0 ) + if( count < ransacSize0 ) return Mat(); Rect brect = boundingRect(pB); + std::vector a(ransacSize0); + std::vector b(ransacSize0); + // RANSAC stuff: // 1. find the consensus - for( k = 0; k < RANSAC_MAX_ITERS; k++ ) + for( k = 0; k < ransacMaxIters; k++ ) { - int idx[RANSAC_SIZE0]; - Point2f a[RANSAC_SIZE0]; - Point2f b[RANSAC_SIZE0]; - - // choose random 3 non-coplanar points from A & B - for( i = 0; i < RANSAC_SIZE0; i++ ) + std::vector idx(ransacSize0); + // choose random 3 non-complanar points from A & B + for( i = 0; i < ransacSize0; i++ ) { - for( k1 = 0; k1 < RANSAC_MAX_ITERS; k1++ ) + for( k1 = 0; k1 < ransacMaxIters; k1++ ) { idx[i] = rng.uniform(0, count); @@ -1633,7 +1642,7 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA if( j < i ) continue; - if( i+1 == RANSAC_SIZE0 ) + if( i+1 == ransacSize0 ) { // additional check for non-complanar vectors a[0] = pA[idx[0]]; @@ -1657,11 +1666,11 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA break; } - if( k1 >= RANSAC_MAX_ITERS ) + if( k1 >= ransacMaxIters ) break; } - if( i < RANSAC_SIZE0 ) + if( i < ransacSize0 ) continue; // estimate the transformation using 3 points @@ -1675,11 +1684,11 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA good_idx[good_count++] = i; } - if( good_count >= count*RANSAC_GOOD_RATIO ) + if( good_count >= count*ransacGoodRatio ) break; } - if( k >= RANSAC_MAX_ITERS ) + if( k >= ransacMaxIters ) return Mat(); if( good_count < count ) @@ -1692,7 +1701,7 @@ cv::Mat cv::estimateRigidTransform( InputArray src1, InputArray src2, bool fullA } } - getRTMatrix( &pA[0], &pB[0], good_count, M, fullAffine ); + getRTMatrix( pA, pB, good_count, M, fullAffine ); M.at(0, 2) /= scale; M.at(1, 2) /= scale; -- 2.7.4