Path: blob/master/modules/calib3d/test/test_reproject_image_to_3d.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// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"43#include "opencv2/calib3d/calib3d_c.h"4445namespace opencv_test { namespace {4647template<class T> double thres() { return 1.0; }48template<> double thres<float>() { return 1e-5; }4950class CV_ReprojectImageTo3DTest : public cvtest::BaseTest51{52public:53CV_ReprojectImageTo3DTest() {}54~CV_ReprojectImageTo3DTest() {}55protected:565758void run(int)59{60ts->set_failed_test_info(cvtest::TS::OK);61int progress = 0;62int caseId = 0;6364progress = update_progress( progress, 1, 14, 0 );65runCase<float, float>(++caseId, -100.f, 100.f);66progress = update_progress( progress, 2, 14, 0 );67runCase<int, float>(++caseId, -100, 100);68progress = update_progress( progress, 3, 14, 0 );69runCase<short, float>(++caseId, -100, 100);70progress = update_progress( progress, 4, 14, 0 );71runCase<unsigned char, float>(++caseId, 10, 100);72progress = update_progress( progress, 5, 14, 0 );7374runCase<float, int>(++caseId, -100.f, 100.f);75progress = update_progress( progress, 6, 14, 0 );76runCase<int, int>(++caseId, -100, 100);77progress = update_progress( progress, 7, 14, 0 );78runCase<short, int>(++caseId, -100, 100);79progress = update_progress( progress, 8, 14, 0 );80runCase<unsigned char, int>(++caseId, 10, 100);81progress = update_progress( progress, 10, 14, 0 );8283runCase<float, short>(++caseId, -100.f, 100.f);84progress = update_progress( progress, 11, 14, 0 );85runCase<int, short>(++caseId, -100, 100);86progress = update_progress( progress, 12, 14, 0 );87runCase<short, short>(++caseId, -100, 100);88progress = update_progress( progress, 13, 14, 0 );89runCase<unsigned char, short>(++caseId, 10, 100);90progress = update_progress( progress, 14, 14, 0 );91}9293template<class U, class V> double error(const Vec<U, 3>& v1, const Vec<V, 3>& v2) const94{95double tmp, sum = 0;96double nsum = 0;97for(int i = 0; i < 3; ++i)98{99tmp = v1[i];100nsum += tmp * tmp;101102tmp = tmp - v2[i];103sum += tmp * tmp;104105}106return sqrt(sum)/(sqrt(nsum)+1.);107}108109template<class InT, class OutT> void runCase(int caseId, InT min, InT max)110{111typedef Vec<OutT, 3> out3d_t;112113bool handleMissingValues = (unsigned)theRNG() % 2 == 0;114115Mat_<InT> disp(Size(320, 240));116randu(disp, Scalar(min), Scalar(max));117118if (handleMissingValues)119disp(disp.rows/2, disp.cols/2) = min - 1;120121Mat_<double> Q(4, 4);122randu(Q, Scalar(-5), Scalar(5));123124Mat_<out3d_t> _3dImg(disp.size());125126CvMat cvdisp = cvMat(disp); CvMat cv_3dImg = cvMat(_3dImg); CvMat cvQ = cvMat(Q);127cvReprojectImageTo3D( &cvdisp, &cv_3dImg, &cvQ, handleMissingValues );128129if (std::numeric_limits<OutT>::max() == std::numeric_limits<float>::max())130reprojectImageTo3D(disp, _3dImg, Q, handleMissingValues);131132for(int y = 0; y < disp.rows; ++y)133for(int x = 0; x < disp.cols; ++x)134{135InT d = disp(y, x);136137double from[4] = {138static_cast<double>(x),139static_cast<double>(y),140static_cast<double>(d),1411.0,142};143Mat_<double> res = Q * Mat_<double>(4, 1, from);144res /= res(3, 0);145146out3d_t pixel_exp = *res.ptr<Vec3d>();147out3d_t pixel_out = _3dImg(y, x);148149const int largeZValue = 10000; /* see documentation */150151if (handleMissingValues && y == disp.rows/2 && x == disp.cols/2)152{153if (pixel_out[2] == largeZValue)154continue;155156ts->printf(cvtest::TS::LOG, "Missing values are handled improperly\n");157ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );158return;159}160else161{162double err = error(pixel_out, pixel_exp), t = thres<OutT>();163if ( err > t )164{165ts->printf(cvtest::TS::LOG, "case %d. too big error at (%d, %d): %g vs expected %g: res = (%g, %g, %g, w=%g) vs pixel_out = (%g, %g, %g)\n",166caseId, x, y, err, t, res(0,0), res(1,0), res(2,0), res(3,0),167(double)pixel_out[0], (double)pixel_out[1], (double)pixel_out[2]);168ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ACCURACY );169return;170}171}172}173}174};175176TEST(Calib3d_ReprojectImageTo3D, accuracy) { CV_ReprojectImageTo3DTest test; test.safe_run(); }177178}} // namespace179180181