Path: blob/master/modules/calib3d/test/test_modelest.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// Intel License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of Intel Corporation may not be used to endorse or promote products26// derived from this software 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 the Intel Corporation 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"4243#if 044#include "_modelest.h"4546using namespace std;47using namespace cv;4849class BareModelEstimator : public CvModelEstimator250{51public:52BareModelEstimator(int modelPoints, CvSize modelSize, int maxBasicSolutions);5354virtual int runKernel( const CvMat*, const CvMat*, CvMat* );55virtual void computeReprojError( const CvMat*, const CvMat*,56const CvMat*, CvMat* );5758bool checkSubsetPublic( const CvMat* ms1, int count, bool checkPartialSubset );59};6061BareModelEstimator::BareModelEstimator(int _modelPoints, CvSize _modelSize, int _maxBasicSolutions)62:CvModelEstimator2(_modelPoints, _modelSize, _maxBasicSolutions)63{64}6566int BareModelEstimator::runKernel( const CvMat*, const CvMat*, CvMat* )67{68return 0;69}7071void BareModelEstimator::computeReprojError( const CvMat*, const CvMat*,72const CvMat*, CvMat* )73{74}7576bool BareModelEstimator::checkSubsetPublic( const CvMat* ms1, int count, bool checkPartialSubset )77{78checkPartialSubsets = checkPartialSubset;79return checkSubset(ms1, count);80}8182class CV_ModelEstimator2_Test : public cvtest::ArrayTest83{84public:85CV_ModelEstimator2_Test();8687protected:88void get_test_array_types_and_sizes( int test_case_idx, vector<vector<Size> >& sizes, vector<vector<int> >& types );89void fill_array( int test_case_idx, int i, int j, Mat& arr );90double get_success_error_level( int test_case_idx, int i, int j );91void run_func();92void prepare_to_validation( int test_case_idx );9394bool checkPartialSubsets;95int usedPointsCount;9697bool checkSubsetResult;98int generalPositionsCount;99int maxPointsCount;100};101102CV_ModelEstimator2_Test::CV_ModelEstimator2_Test()103{104generalPositionsCount = get_test_case_count() / 2;105maxPointsCount = 100;106107test_array[INPUT].push_back(NULL);108test_array[OUTPUT].push_back(NULL);109test_array[REF_OUTPUT].push_back(NULL);110}111112void CV_ModelEstimator2_Test::get_test_array_types_and_sizes( int /*test_case_idx*/,113vector<vector<Size> > &sizes, vector<vector<int> > &types )114{115RNG &rng = ts->get_rng();116checkPartialSubsets = (cvtest::randInt(rng) % 2 == 0);117118int pointsCount = cvtest::randInt(rng) % maxPointsCount;119usedPointsCount = pointsCount == 0 ? 0 : cvtest::randInt(rng) % pointsCount;120121sizes[INPUT][0] = cvSize(1, pointsCount);122types[INPUT][0] = CV_64FC2;123124sizes[OUTPUT][0] = sizes[REF_OUTPUT][0] = cvSize(1, 1);125types[OUTPUT][0] = types[REF_OUTPUT][0] = CV_8UC1;126}127128void CV_ModelEstimator2_Test::fill_array( int test_case_idx, int i, int j, Mat& arr )129{130if( i != INPUT )131{132cvtest::ArrayTest::fill_array( test_case_idx, i, j, arr );133return;134}135136if (test_case_idx < generalPositionsCount)137{138//generate points in a general position (i.e. no three points can lie on the same line.)139140bool isGeneralPosition;141do142{143ArrayTest::fill_array(test_case_idx, i, j, arr);144145//a simple check that the position is general:146// for each line check that all other points don't belong to it147isGeneralPosition = true;148for (int startPointIndex = 0; startPointIndex < usedPointsCount && isGeneralPosition; startPointIndex++)149{150for (int endPointIndex = startPointIndex + 1; endPointIndex < usedPointsCount && isGeneralPosition; endPointIndex++)151{152153for (int testPointIndex = 0; testPointIndex < usedPointsCount && isGeneralPosition; testPointIndex++)154{155if (testPointIndex == startPointIndex || testPointIndex == endPointIndex)156{157continue;158}159160CV_Assert(arr.type() == CV_64FC2);161Point2d tangentVector_1 = arr.at<Point2d>(endPointIndex) - arr.at<Point2d>(startPointIndex);162Point2d tangentVector_2 = arr.at<Point2d>(testPointIndex) - arr.at<Point2d>(startPointIndex);163164const float eps = 1e-4f;165//TODO: perhaps it is better to normalize the cross product by norms of the tangent vectors166if (fabs(tangentVector_1.cross(tangentVector_2)) < eps)167{168isGeneralPosition = false;169}170}171}172}173}174while(!isGeneralPosition);175}176else177{178//create points in a degenerate position (there are at least 3 points belonging to the same line)179180ArrayTest::fill_array(test_case_idx, i, j, arr);181if (usedPointsCount <= 2)182{183return;184}185186RNG &rng = ts->get_rng();187int startPointIndex, endPointIndex, modifiedPointIndex;188do189{190startPointIndex = cvtest::randInt(rng) % usedPointsCount;191endPointIndex = cvtest::randInt(rng) % usedPointsCount;192modifiedPointIndex = checkPartialSubsets ? usedPointsCount - 1 : cvtest::randInt(rng) % usedPointsCount;193}194while (startPointIndex == endPointIndex || startPointIndex == modifiedPointIndex || endPointIndex == modifiedPointIndex);195196double startWeight = cvtest::randReal(rng);197CV_Assert(arr.type() == CV_64FC2);198arr.at<Point2d>(modifiedPointIndex) = startWeight * arr.at<Point2d>(startPointIndex) + (1.0 - startWeight) * arr.at<Point2d>(endPointIndex);199}200}201202203double CV_ModelEstimator2_Test::get_success_error_level( int /*test_case_idx*/, int /*i*/, int /*j*/ )204{205return 0;206}207208void CV_ModelEstimator2_Test::prepare_to_validation( int test_case_idx )209{210test_mat[OUTPUT][0].at<uchar>(0) = checkSubsetResult;211test_mat[REF_OUTPUT][0].at<uchar>(0) = test_case_idx < generalPositionsCount || usedPointsCount <= 2;212}213214void CV_ModelEstimator2_Test::run_func()215{216//make the input continuous217Mat input = test_mat[INPUT][0].clone();218CvMat _input = input;219220RNG &rng = ts->get_rng();221int modelPoints = cvtest::randInt(rng);222CvSize modelSize = cvSize(2, modelPoints);223int maxBasicSolutions = cvtest::randInt(rng);224BareModelEstimator modelEstimator(modelPoints, modelSize, maxBasicSolutions);225checkSubsetResult = modelEstimator.checkSubsetPublic(&_input, usedPointsCount, checkPartialSubsets);226}227228TEST(Calib3d_ModelEstimator2, accuracy) { CV_ModelEstimator2_Test test; test.safe_run(); }229230#endif231232233