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 
40 #include <opencv2/imgproc.hpp>
41 #include "opencv2/reg/mapshift.hpp"
42 
43 
44 namespace cv {
45 namespace reg {
46 
47 
48 ////////////////////////////////////////////////////////////////////////////////////////////////////
MapShift()49 MapShift::MapShift() : shift_()
50 {
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////////////////////////
MapShift(InputArray shift)54 MapShift::MapShift(InputArray shift)
55 {
56     Mat shiftMat = shift.getMat();
57     shiftMat.copyTo(shift_);
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////////////////////////
~MapShift()61 MapShift::~MapShift()
62 {
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////////////////////////
inverseWarp(InputArray _img1,OutputArray img2) const66 void MapShift::inverseWarp(InputArray _img1, OutputArray img2) const
67 {
68     Mat img1 = _img1.getMat();
69     // Rows and columns in destination
70     Mat dest_r, dest_c;
71     dest_r.create(img1.size(), CV_32FC1);
72     dest_c.create(img1.size(), CV_32FC1);
73     for(int r_i = 0; r_i < img1.rows; ++r_i)
74     {
75         for(int c_i = 0; c_i < img1.cols; ++c_i)
76         {
77             dest_c.at<float>(r_i, c_i) = float(c_i + shift_(0));
78             dest_r.at<float>(r_i, c_i) = float(r_i + shift_(1));
79         }
80     }
81 
82     //remap(img1, img2, dest_c, dest_r, INTER_CUBIC, BORDER_REPLICATE);
83     // Parts that cannot be interpolated will be as in img1 (BORDER_TRANSPARENT means that
84     // remap will not touch them).
85     img1.copyTo(img2);
86     remap(img1, img2, dest_c, dest_r, INTER_CUBIC, BORDER_TRANSPARENT);
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////////////////////////
inverseMap(void) const90 Ptr<Map> MapShift::inverseMap(void) const
91 {
92     Vec<double, 2> invShift = -shift_;
93     return Ptr<Map>(new MapShift(invShift));
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////////////////////////
compose(cv::Ptr<Map> map)97 void MapShift::compose(cv::Ptr<Map> map)
98 {
99     // Composition of transformations T and T' is (T o T') = b + b'
100     const MapShift& mapShift = static_cast<const MapShift&>(*map);
101     shift_ += mapShift.getShift();
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////////////////////////
scale(double factor)105 void MapShift::scale(double factor)
106 {
107     shift_ *= factor;
108 }
109 
110 
111 }}  // namespace cv::reg
112