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 #include "opencv2/stitching/warpers.hpp"
7 
8 namespace opencv_test { namespace {
9 class ReprojectionTest : public ::testing::Test {
10 
11 protected:
12     const size_t TEST_COUNT = 15;
13     Mat K, R;
14     RNG rng = RNG(0);
ReprojectionTest()15     ReprojectionTest()
16     {
17         K = Mat::eye(3, 3, CV_32FC1);
18         float angle = (float)(30.0 * CV_PI / 180.0);
19         float rotationMatrix[9] = {
20                 (float)cos(angle), (float)sin(angle), 0,
21                 (float)-sin(angle), (float)cos(angle), 0,
22                 0, 0, 1
23         };
24         Mat(3, 3, CV_32FC1, rotationMatrix).copyTo(R);
25     }
TestReprojection(Ptr<detail::RotationWarper> warper,Point2f pt)26     void TestReprojection(Ptr<detail::RotationWarper> warper, Point2f pt) {
27         Point2f projected_pt = warper->warpPoint(pt, K, R);
28         Point2f reprojected_pt = warper->warpPointBackward(projected_pt, K, R);
29         EXPECT_NEAR(pt.x, reprojected_pt.x, float( 1e-5));
30         EXPECT_NEAR(pt.y, reprojected_pt.y, float( 1e-5));
31     }
32 };
33 
34 
TEST_F(ReprojectionTest,PlaneWarper)35 TEST_F(ReprojectionTest, PlaneWarper)
36 {
37     Ptr<WarperCreator> creator = makePtr<PlaneWarper>();
38     for (size_t i = 0; i < TEST_COUNT; ++i) {
39         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
40     }
41 }
42 
TEST_F(ReprojectionTest,AffineWarper)43 TEST_F(ReprojectionTest, AffineWarper)
44 {
45     Ptr<WarperCreator> creator = makePtr<AffineWarper>();
46     for (size_t i = 0; i < TEST_COUNT; ++i) {
47         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
48     }
49 }
50 
TEST_F(ReprojectionTest,CylindricalWarper)51 TEST_F(ReprojectionTest, CylindricalWarper)
52 {
53     Ptr<WarperCreator> creator = makePtr<CylindricalWarper>();
54     for (size_t i = 0; i < TEST_COUNT; ++i) {
55         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
56     }
57 }
58 
TEST_F(ReprojectionTest,SphericalWarper)59 TEST_F(ReprojectionTest, SphericalWarper)
60 {
61     Ptr<WarperCreator> creator = makePtr<SphericalWarper>();
62     for (size_t i = 0; i < TEST_COUNT; ++i) {
63         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
64     }
65 }
66 
TEST_F(ReprojectionTest,FisheyeWarper)67 TEST_F(ReprojectionTest, FisheyeWarper)
68 {
69     Ptr<WarperCreator> creator = makePtr<FisheyeWarper>();
70     for (size_t i = 0; i < TEST_COUNT; ++i) {
71         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
72     }
73 }
74 
TEST_F(ReprojectionTest,StereographicWarper)75 TEST_F(ReprojectionTest, StereographicWarper)
76 {
77     Ptr<WarperCreator> creator = makePtr<StereographicWarper>();
78     for (size_t i = 0; i < TEST_COUNT; ++i) {
79         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
80     }
81 }
82 
TEST_F(ReprojectionTest,CompressedRectilinearWarper)83 TEST_F(ReprojectionTest, CompressedRectilinearWarper)
84 {
85     Ptr<WarperCreator> creator = makePtr<CompressedRectilinearWarper>(1.5f, 1.0f);
86     for (size_t i = 0; i < TEST_COUNT; ++i) {
87         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
88     }
89 }
90 
TEST_F(ReprojectionTest,CompressedRectilinearPortraitWarper)91 TEST_F(ReprojectionTest, CompressedRectilinearPortraitWarper)
92 {
93     Ptr<WarperCreator> creator = makePtr<CompressedRectilinearPortraitWarper>(1.5f, 1.0f);
94     for (size_t i = 0; i < TEST_COUNT; ++i) {
95         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
96     }
97 }
98 
TEST_F(ReprojectionTest,PaniniWarper)99 TEST_F(ReprojectionTest, PaniniWarper)
100 {
101     Ptr<WarperCreator> creator = makePtr<PaniniWarper>(1.5f, 1.0f);
102     for (size_t i = 0; i < TEST_COUNT; ++i) {
103         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
104     }
105 }
106 
TEST_F(ReprojectionTest,PaniniPortraitWarper)107 TEST_F(ReprojectionTest, PaniniPortraitWarper)
108 {
109     Ptr<WarperCreator> creator = makePtr<PaniniPortraitWarper>(1.5f, 1.0f);
110     for (size_t i = 0; i < TEST_COUNT; ++i) {
111         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
112     }
113 }
114 
TEST_F(ReprojectionTest,MercatorWarper)115 TEST_F(ReprojectionTest, MercatorWarper)
116 {
117     Ptr<WarperCreator> creator = makePtr<MercatorWarper>();
118     for (size_t i = 0; i < TEST_COUNT; ++i) {
119         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
120     }
121 }
122 
TEST_F(ReprojectionTest,TransverseMercatorWarper)123 TEST_F(ReprojectionTest, TransverseMercatorWarper)
124 {
125     Ptr<WarperCreator> creator = makePtr<TransverseMercatorWarper>();
126     for (size_t i = 0; i < TEST_COUNT; ++i) {
127         TestReprojection(creator->create(1), Point2f(rng.uniform(-1.f, 1.f), rng.uniform(-1.f, 1.f)));
128     }
129 }
130 
131 }} // namespace
132