Home | History | Annotate | Download | only in test
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                           License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
     14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
     15 // Third party copyrights are property of their respective owners.
     16 //
     17 // Redistribution and use in source and binary forms, with or without modification,
     18 // are permitted provided that the following conditions are met:
     19 //
     20 //   * Redistribution's of source code must retain the above copyright notice,
     21 //     this list of conditions and the following disclaimer.
     22 //
     23 //   * Redistribution's in binary form must reproduce the above copyright notice,
     24 //     this list of conditions and the following disclaimer in the documentation
     25 //     and/or other materials provided with the distribution.
     26 //
     27 //   * The name of the copyright holders may not be used to endorse or promote products
     28 //     derived from this software without specific prior written permission.
     29 //
     30 // This software is provided by the copyright holders and contributors "as is" and
     31 // any express or implied warranties, including, but not limited to, the implied
     32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     33 // In no event shall the Intel Corporation or contributors be liable for any direct,
     34 // indirect, incidental, special, exemplary, or consequential damages
     35 // (including, but not limited to, procurement of substitute goods or services;
     36 // loss of use, data, or profits; or business interruption) however caused
     37 // and on any theory of liability, whether in contract, strict liability,
     38 // or tort (including negligence or otherwise) arising in any way out of
     39 // the use of this software, even if advised of the possibility of such damage.
     40 //
     41 //M*/
     42 
     43 #include "test_precomp.hpp"
     44 
     45 #if defined HAVE_CUDA && defined HAVE_OPENCV_CALIB3D
     46 
     47 #include "opencv2/calib3d.hpp"
     48 
     49 using namespace cvtest;
     50 
     51 ///////////////////////////////////////////////////////////////////////////////////////////////////////
     52 // transformPoints
     53 
     54 struct TransformPoints : testing::TestWithParam<cv::cuda::DeviceInfo>
     55 {
     56     cv::cuda::DeviceInfo devInfo;
     57 
     58     virtual void SetUp()
     59     {
     60         devInfo = GetParam();
     61 
     62         cv::cuda::setDevice(devInfo.deviceID());
     63     }
     64 };
     65 
     66 CUDA_TEST_P(TransformPoints, Accuracy)
     67 {
     68     cv::Mat src = randomMat(cv::Size(1000, 1), CV_32FC3, 0, 10);
     69     cv::Mat rvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
     70     cv::Mat tvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
     71 
     72     cv::cuda::GpuMat dst;
     73     cv::cuda::transformPoints(loadMat(src), rvec, tvec, dst);
     74 
     75     ASSERT_EQ(src.size(), dst.size());
     76     ASSERT_EQ(src.type(), dst.type());
     77 
     78     cv::Mat h_dst(dst);
     79 
     80     cv::Mat rot;
     81     cv::Rodrigues(rvec, rot);
     82 
     83     for (int i = 0; i < h_dst.cols; ++i)
     84     {
     85         cv::Point3f res = h_dst.at<cv::Point3f>(0, i);
     86 
     87         cv::Point3f p = src.at<cv::Point3f>(0, i);
     88         cv::Point3f res_gold(
     89                 rot.at<float>(0, 0) * p.x + rot.at<float>(0, 1) * p.y + rot.at<float>(0, 2) * p.z + tvec.at<float>(0, 0),
     90                 rot.at<float>(1, 0) * p.x + rot.at<float>(1, 1) * p.y + rot.at<float>(1, 2) * p.z + tvec.at<float>(0, 1),
     91                 rot.at<float>(2, 0) * p.x + rot.at<float>(2, 1) * p.y + rot.at<float>(2, 2) * p.z + tvec.at<float>(0, 2));
     92 
     93         ASSERT_POINT3_NEAR(res_gold, res, 1e-5);
     94     }
     95 }
     96 
     97 INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, TransformPoints, ALL_DEVICES);
     98 
     99 ///////////////////////////////////////////////////////////////////////////////////////////////////////
    100 // ProjectPoints
    101 
    102 struct ProjectPoints : testing::TestWithParam<cv::cuda::DeviceInfo>
    103 {
    104     cv::cuda::DeviceInfo devInfo;
    105 
    106     virtual void SetUp()
    107     {
    108         devInfo = GetParam();
    109 
    110         cv::cuda::setDevice(devInfo.deviceID());
    111     }
    112 };
    113 
    114 CUDA_TEST_P(ProjectPoints, Accuracy)
    115 {
    116     cv::Mat src = randomMat(cv::Size(1000, 1), CV_32FC3, 0, 10);
    117     cv::Mat rvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
    118     cv::Mat tvec = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
    119     cv::Mat camera_mat = randomMat(cv::Size(3, 3), CV_32F, 0.5, 1);
    120     camera_mat.at<float>(0, 1) = 0.f;
    121     camera_mat.at<float>(1, 0) = 0.f;
    122     camera_mat.at<float>(2, 0) = 0.f;
    123     camera_mat.at<float>(2, 1) = 0.f;
    124 
    125     cv::cuda::GpuMat dst;
    126     cv::cuda::projectPoints(loadMat(src), rvec, tvec, camera_mat, cv::Mat(), dst);
    127 
    128     ASSERT_EQ(1, dst.rows);
    129     ASSERT_EQ(MatType(CV_32FC2), MatType(dst.type()));
    130 
    131     std::vector<cv::Point2f> dst_gold;
    132     cv::projectPoints(src, rvec, tvec, camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)), dst_gold);
    133 
    134     ASSERT_EQ(dst_gold.size(), static_cast<size_t>(dst.cols));
    135 
    136     cv::Mat h_dst(dst);
    137 
    138     for (size_t i = 0; i < dst_gold.size(); ++i)
    139     {
    140         cv::Point2f res = h_dst.at<cv::Point2f>(0, (int)i);
    141         cv::Point2f res_gold = dst_gold[i];
    142 
    143         ASSERT_LE(cv::norm(res_gold - res) / cv::norm(res_gold), 1e-3f);
    144     }
    145 }
    146 
    147 INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, ProjectPoints, ALL_DEVICES);
    148 
    149 ///////////////////////////////////////////////////////////////////////////////////////////////////////
    150 // SolvePnPRansac
    151 
    152 struct SolvePnPRansac : testing::TestWithParam<cv::cuda::DeviceInfo>
    153 {
    154     cv::cuda::DeviceInfo devInfo;
    155 
    156     virtual void SetUp()
    157     {
    158         devInfo = GetParam();
    159 
    160         cv::cuda::setDevice(devInfo.deviceID());
    161     }
    162 };
    163 
    164 CUDA_TEST_P(SolvePnPRansac, Accuracy)
    165 {
    166     cv::Mat object = randomMat(cv::Size(5000, 1), CV_32FC3, 0, 100);
    167     cv::Mat camera_mat = randomMat(cv::Size(3, 3), CV_32F, 0.5, 1);
    168     camera_mat.at<float>(0, 1) = 0.f;
    169     camera_mat.at<float>(1, 0) = 0.f;
    170     camera_mat.at<float>(2, 0) = 0.f;
    171     camera_mat.at<float>(2, 1) = 0.f;
    172 
    173     std::vector<cv::Point2f> image_vec;
    174     cv::Mat rvec_gold;
    175     cv::Mat tvec_gold;
    176     rvec_gold = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
    177     tvec_gold = randomMat(cv::Size(3, 1), CV_32F, 0, 1);
    178     cv::projectPoints(object, rvec_gold, tvec_gold, camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)), image_vec);
    179 
    180     cv::Mat rvec, tvec;
    181     std::vector<int> inliers;
    182     cv::cuda::solvePnPRansac(object, cv::Mat(1, (int)image_vec.size(), CV_32FC2, &image_vec[0]),
    183                             camera_mat, cv::Mat(1, 8, CV_32F, cv::Scalar::all(0)),
    184                             rvec, tvec, false, 200, 2.f, 100, &inliers);
    185 
    186     ASSERT_LE(cv::norm(rvec - rvec_gold), 1e-3);
    187     ASSERT_LE(cv::norm(tvec - tvec_gold), 1e-3);
    188 }
    189 
    190 INSTANTIATE_TEST_CASE_P(CUDA_Calib3D, SolvePnPRansac, ALL_DEVICES);
    191 
    192 #endif // HAVE_CUDA
    193