Path: blob/master/modules/calib3d/test/test_affine2d_estimator.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// By downloading, copying, installing or using the software you agree to this license.3// If you do not agree to this license, do not download, install,4// copy or use the software.5//6//7// License Agreement8// For Open Source Computer Vision Library9// (3-clause BSD License)10//11// Copyright (C) 2015-2016, OpenCV Foundation, all rights reserved.12// Third party copyrights are property of their respective owners.13//14// Redistribution and use in source and binary forms, with or without modification,15// are permitted provided that the following conditions are met:16//17// * Redistributions of source code must retain the above copyright notice,18// this list of conditions and the following disclaimer.19//20// * Redistributions in binary form must reproduce the above copyright notice,21// this list of conditions and the following disclaimer in the documentation22// and/or other materials provided with the distribution.23//24// * Neither the names of the copyright holders nor the names of the contributors25// may be used to endorse or promote products derived from this software26// without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall copyright holders or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39//M*/4041#include "test_precomp.hpp"4243namespace opencv_test { namespace {4445CV_ENUM(Method, RANSAC, LMEDS)46typedef TestWithParam<Method> EstimateAffine2D;4748static float rngIn(float from, float to) { return from + (to-from) * (float)theRNG(); }4950TEST_P(EstimateAffine2D, test3Points)51{52// try more transformations53for (size_t i = 0; i < 500; ++i)54{55Mat aff(2, 3, CV_64F);56cv::randu(aff, 1., 3.);5758Mat fpts(1, 3, CV_32FC2);59Mat tpts(1, 3, CV_32FC2);6061// setting points that are not in the same line62fpts.at<Point2f>(0) = Point2f( rngIn(1,2), rngIn(5,6) );63fpts.at<Point2f>(1) = Point2f( rngIn(3,4), rngIn(3,4) );64fpts.at<Point2f>(2) = Point2f( rngIn(1,2), rngIn(3,4) );6566transform(fpts, tpts, aff);6768vector<uchar> inliers;69Mat aff_est = estimateAffine2D(fpts, tpts, inliers, GetParam() /* method */);7071EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);7273// all must be inliers74EXPECT_EQ(countNonZero(inliers), 3);75}76}7778TEST_P(EstimateAffine2D, testNPoints)79{80// try more transformations81for (size_t i = 0; i < 500; ++i)82{83Mat aff(2, 3, CV_64F);84cv::randu(aff, -2., 2.);85const int method = GetParam();86const int n = 100;87int m;88// LMEDS can't handle more than 50% outliers (by design)89if (method == LMEDS)90m = 3*n/5;91else92m = 2*n/5;93const float shift_outl = 15.f;94const float noise_level = 20.f;9596Mat fpts(1, n, CV_32FC2);97Mat tpts(1, n, CV_32FC2);9899randu(fpts, 0., 100.);100transform(fpts, tpts, aff);101102/* adding noise to some points */103Mat outliers = tpts.colRange(m, n);104outliers.reshape(1) += shift_outl;105106Mat noise (outliers.size(), outliers.type());107randu(noise, 0., noise_level);108outliers += noise;109110vector<uchar> inliers;111Mat aff_est = estimateAffine2D(fpts, tpts, inliers, method);112113EXPECT_FALSE(aff_est.empty()) << "estimation failed, unable to estimate transform";114115EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-4);116117bool inliers_good = count(inliers.begin(), inliers.end(), 1) == m &&118m == accumulate(inliers.begin(), inliers.begin() + m, 0);119120EXPECT_TRUE(inliers_good);121}122}123124// test conversion from other datatypes than float125TEST_P(EstimateAffine2D, testConversion)126{127Mat aff(2, 3, CV_32S);128cv::randu(aff, 1., 3.);129130std::vector<Point> fpts(3);131std::vector<Point> tpts(3);132133// setting points that are not in the same line134fpts[0] = Point2f( rngIn(1,2), rngIn(5,6) );135fpts[1] = Point2f( rngIn(3,4), rngIn(3,4) );136fpts[2] = Point2f( rngIn(1,2), rngIn(3,4) );137138transform(fpts, tpts, aff);139140vector<uchar> inliers;141Mat aff_est = estimateAffine2D(fpts, tpts, inliers, GetParam() /* method */);142143ASSERT_FALSE(aff_est.empty());144145aff.convertTo(aff, CV_64F); // need to convert before compare146EXPECT_NEAR(0., cvtest::norm(aff_est, aff, NORM_INF), 1e-3);147148// all must be inliers149EXPECT_EQ(countNonZero(inliers), 3);150}151152INSTANTIATE_TEST_CASE_P(Calib3d, EstimateAffine2D, Method::all());153154}} // namespace155156157