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 // Copyright (C) 2013, Alfonso Sanchez-Beato, all rights reserved.
10 // Third party copyrights are property of their respective owners.
11 //
12 // Redistribution and use in source and binary forms, with or without modification,
13 // are permitted provided that the following conditions are met:
14 //
15 //   * Redistribution's of source code must retain the above copyright notice,
16 //     this list of conditions and the following disclaimer.
17 //
18 //   * Redistribution's in binary form must reproduce the above copyright notice,
19 //     this list of conditions and the following disclaimer in the documentation
20 //     and/or other materials provided with the distribution.
21 //
22 //   * The name of the copyright holders may not be used to endorse or promote products
23 //     derived from this software without specific prior written permission.
24 //
25 // This software is provided by the copyright holders and contributors "as is" and
26 // any express or implied warranties, including, but not limited to, the implied
27 // warranties of merchantability and fitness for a particular purpose are disclaimed.
28 // In no event shall the contributors be liable for any direct,
29 // indirect, incidental, special, exemplary, or consequential damages
30 // (including, but not limited to, procurement of substitute goods or services;
31 // loss of use, data, or profits; or business interruption) however caused
32 // and on any theory of liability, whether in contract, strict liability,
33 // or tort (including negligence or otherwise) arising in any way out of
34 // the use of this software, even if advised of the possibility of such damage.
35 //
36 //M*/
37 
38 #include "precomp.hpp"
39 #include <opencv2/imgproc.hpp>
40 #include "opencv2/reg/mapprojec.hpp"
41 
42 
43 namespace cv {
44 namespace reg {
45 
46 
47 ////////////////////////////////////////////////////////////////////////////////////////////////////
MapProjec()48 MapProjec::MapProjec()
49     : projTr_(Matx<double, 3, 3>::eye())
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////////////////////////
MapProjec(InputArray projTr)54 MapProjec::MapProjec(InputArray projTr)
55     : projTr_(projTr.getMat())
56 {
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////////////////////////
~MapProjec()60 MapProjec::~MapProjec()
61 {
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////////////////////////
inverseWarp(InputArray _img1,OutputArray img2) const65 void MapProjec::inverseWarp(InputArray _img1, OutputArray img2) const
66 {
67     Mat img1 = _img1.getMat();
68     // Rows and columns in destination
69     Mat dest_r, dest_c;
70     dest_r.create(img1.size(), CV_32FC1);
71     dest_c.create(img1.size(), CV_32FC1);
72     for(int r_i = 0; r_i < img1.rows; ++r_i)
73     {
74         for(int c_i = 0; c_i < img1.cols; ++c_i)
75         {
76             double z = c_i*projTr_(2, 0) + r_i*projTr_(2, 1) + projTr_(2, 2);
77             dest_c.at<float>(r_i, c_i) =
78                 float((c_i*projTr_(0, 0) + r_i*projTr_(0, 1) + projTr_(0, 2))/z);
79             dest_r.at<float>(r_i, c_i) =
80                 float((c_i*projTr_(1, 0) + r_i*projTr_(1, 1) + projTr_(1, 2))/z);
81         }
82     }
83 
84     //remap(img1, img2, dest_c, dest_r, INTER_CUBIC, BORDER_REPLICATE);
85     // Parts that cannot be interpolated will be as in img1 (BORDER_TRANSPARENT means that
86     // remap will not touch them).
87     img1.copyTo(img2);
88     remap(img1, img2, dest_c, dest_r, INTER_CUBIC, BORDER_TRANSPARENT);
89 }
90 
91 ////////////////////////////////////////////////////////////////////////////////////////////////////
inverseMap(void) const92 Ptr<Map> MapProjec::inverseMap(void) const
93 {
94     Matx<double, 3, 3> invProjTr = projTr_.inv(DECOMP_LU);
95     return Ptr<Map>(new MapProjec(invProjTr));
96 }
97 
98 ////////////////////////////////////////////////////////////////////////////////////////////////////
compose(Ptr<Map> map)99 void MapProjec::compose(Ptr<Map> map)
100 {
101     // Composition of homographies H and H' is (H o H') = H'*H
102     const MapProjec& mapProj = static_cast<const MapProjec&>(*map);
103     Matx<double, 3, 3> compProjTr = mapProj.getProjTr()*projTr_;
104     projTr_ = compProjTr;
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////////////////////////
scale(double factor)108 void MapProjec::scale(double factor)
109 {
110     // Shift is multiplied, projective factors are divided
111     projTr_(0, 2) *= factor;
112     projTr_(1, 2) *= factor;
113     projTr_(2, 0) /= factor;
114     projTr_(2, 1) /= factor;
115 }
116 
117 
118 }}  // namespace cv::reg
119