|
| 1 | +// This file is part of OpenCV project. |
| 2 | +// It is subject to the license terms in the LICENSE file found in the top-level directory |
| 3 | +// of this distribution and at http://opencv.org/license.html |
| 4 | + |
| 5 | +#include "test_precomp.hpp" |
| 6 | + |
| 7 | +namespace testUtil |
| 8 | +{ |
| 9 | + |
| 10 | +cv::RNG rng(/*std::time(0)*/0); |
| 11 | + |
| 12 | +const float sigma = 1.f; |
| 13 | +const float pointsMaxX = 500.f; |
| 14 | +const float pointsMaxY = 500.f; |
| 15 | +const int testRun = 5000; |
| 16 | + |
| 17 | +void generatePoints(cv::Mat points); |
| 18 | +void addNoise(cv::Mat points); |
| 19 | + |
| 20 | +cv::Mat generateTransform(const cv::videostab::MotionModel model); |
| 21 | + |
| 22 | +double performTest(const cv::videostab::MotionModel model, int size); |
| 23 | + |
| 24 | +} |
| 25 | + |
| 26 | +void testUtil::generatePoints(cv::Mat points) |
| 27 | +{ |
| 28 | + CV_Assert(!points.empty()); |
| 29 | + for(int i = 0; i < points.cols; ++i) |
| 30 | + { |
| 31 | + points.at<float>(0, i) = rng.uniform(0.f, pointsMaxX); |
| 32 | + points.at<float>(1, i) = rng.uniform(0.f, pointsMaxY); |
| 33 | + points.at<float>(2, i) = 1.f; |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +void testUtil::addNoise(cv::Mat points) |
| 38 | +{ |
| 39 | + CV_Assert(!points.empty()); |
| 40 | + for(int i = 0; i < points.cols; i++) |
| 41 | + { |
| 42 | + points.at<float>(0, i) += static_cast<float>(rng.gaussian(sigma)); |
| 43 | + points.at<float>(1, i) += static_cast<float>(rng.gaussian(sigma)); |
| 44 | + |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +cv::Mat testUtil::generateTransform(const cv::videostab::MotionModel model) |
| 50 | +{ |
| 51 | + /*----------Params----------*/ |
| 52 | + const float minAngle = 0.f, maxAngle = static_cast<float>(CV_PI); |
| 53 | + const float minScale = 0.5f, maxScale = 2.f; |
| 54 | + const float maxTranslation = 100.f; |
| 55 | + const float affineCoeff = 3.f; |
| 56 | + /*----------Params----------*/ |
| 57 | + |
| 58 | + cv::Mat transform = cv::Mat::eye(3, 3, CV_32F); |
| 59 | + |
| 60 | + if(model != cv::videostab::MM_ROTATION) |
| 61 | + { |
| 62 | + transform.at<float>(0,2) = rng.uniform(-maxTranslation, maxTranslation); |
| 63 | + transform.at<float>(1,2) = rng.uniform(-maxTranslation, maxTranslation); |
| 64 | + } |
| 65 | + |
| 66 | + if(model != cv::videostab::MM_AFFINE) |
| 67 | + { |
| 68 | + |
| 69 | + if(model != cv::videostab::MM_TRANSLATION_AND_SCALE && |
| 70 | + model != cv::videostab::MM_TRANSLATION) |
| 71 | + { |
| 72 | + const float angle = rng.uniform(minAngle, maxAngle); |
| 73 | + |
| 74 | + transform.at<float>(1,1) = transform.at<float>(0,0) = std::cos(angle); |
| 75 | + transform.at<float>(0,1) = std::sin(angle); |
| 76 | + transform.at<float>(1,0) = -transform.at<float>(0,1); |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | + if(model == cv::videostab::MM_TRANSLATION_AND_SCALE || |
| 81 | + model == cv::videostab::MM_SIMILARITY) |
| 82 | + { |
| 83 | + const float scale = rng.uniform(minScale, maxScale); |
| 84 | + |
| 85 | + transform.at<float>(0,0) *= scale; |
| 86 | + transform.at<float>(1,1) *= scale; |
| 87 | + |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + transform.at<float>(0,0) = rng.uniform(-affineCoeff, affineCoeff); |
| 94 | + transform.at<float>(0,1) = rng.uniform(-affineCoeff, affineCoeff); |
| 95 | + transform.at<float>(1,0) = rng.uniform(-affineCoeff, affineCoeff); |
| 96 | + transform.at<float>(1,1) = rng.uniform(-affineCoeff, affineCoeff); |
| 97 | + } |
| 98 | + |
| 99 | + return transform; |
| 100 | +} |
| 101 | + |
| 102 | + |
| 103 | +double testUtil::performTest(const cv::videostab::MotionModel model, int size) |
| 104 | +{ |
| 105 | + cv::Ptr<cv::videostab::MotionEstimatorRansacL2> estimator = cv::makePtr<cv::videostab::MotionEstimatorRansacL2>(model); |
| 106 | + |
| 107 | + estimator->setRansacParams(cv::videostab::RansacParams(size, 3.f*testUtil::sigma /*3 sigma rule*/, 0.5f, 0.5f)); |
| 108 | + |
| 109 | + double disparity = 0.; |
| 110 | + |
| 111 | + for(int attempt = 0; attempt < testUtil::testRun; attempt++) |
| 112 | + { |
| 113 | + const cv::Mat transform = testUtil::generateTransform(model); |
| 114 | + |
| 115 | + const int pointsNumber = testUtil::rng.uniform(10, 100); |
| 116 | + |
| 117 | + cv::Mat points(3, pointsNumber, CV_32F); |
| 118 | + |
| 119 | + testUtil::generatePoints(points); |
| 120 | + |
| 121 | + cv::Mat transformedPoints = transform * points; |
| 122 | + |
| 123 | + testUtil::addNoise(transformedPoints); |
| 124 | + |
| 125 | + const cv::Mat src = points.rowRange(0,2).t(); |
| 126 | + const cv::Mat dst = transformedPoints.rowRange(0,2).t(); |
| 127 | + |
| 128 | + bool isOK = false; |
| 129 | + const cv::Mat estTransform = estimator->estimate(src.reshape(2), dst.reshape(2), &isOK); |
| 130 | + |
| 131 | + CV_Assert(isOK); |
| 132 | + const cv::Mat testPoints = estTransform * points; |
| 133 | + |
| 134 | + const double norm = cv::norm(testPoints, transformedPoints, cv::NORM_INF); |
| 135 | + |
| 136 | + disparity = std::max(disparity, norm); |
| 137 | + } |
| 138 | + |
| 139 | + return disparity; |
| 140 | + |
| 141 | +} |
| 142 | + |
| 143 | +TEST(Regression, MM_TRANSLATION) |
| 144 | +{ |
| 145 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_TRANSLATION, 2), 7.f); |
| 146 | +} |
| 147 | + |
| 148 | +TEST(Regression, MM_TRANSLATION_AND_SCALE) |
| 149 | +{ |
| 150 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_TRANSLATION_AND_SCALE, 3), 7.f); |
| 151 | +} |
| 152 | + |
| 153 | +TEST(Regression, MM_ROTATION) |
| 154 | +{ |
| 155 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_ROTATION, 2), 7.f); |
| 156 | +} |
| 157 | + |
| 158 | +TEST(Regression, MM_RIGID) |
| 159 | +{ |
| 160 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_RIGID, 3), 7.f); |
| 161 | +} |
| 162 | + |
| 163 | +TEST(Regression, MM_SIMILARITY) |
| 164 | +{ |
| 165 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_SIMILARITY, 4), 7.f); |
| 166 | +} |
| 167 | + |
| 168 | +TEST(Regression, MM_AFFINE) |
| 169 | +{ |
| 170 | + EXPECT_LT(testUtil::performTest(cv::videostab::MM_AFFINE, 6), 9.f); |
| 171 | +} |
0 commit comments