1Remapping {#tutorial_remap}
2=========
3
4Goal
5----
6
7In this tutorial you will learn how to:
8
9a.  Use the OpenCV function @ref cv::remap to implement simple remapping routines.
10
11Theory
12------
13
14### What is remapping?
15
16-   It is the process of taking pixels from one place in the image and locating them in another
17    position in a new image.
18-   To accomplish the mapping process, it might be necessary to do some interpolation for
19    non-integer pixel locations, since there will not always be a one-to-one-pixel correspondence
20    between source and destination images.
21-   We can express the remap for every pixel location \f$(x,y)\f$ as:
22
23    \f[g(x,y) = f ( h(x,y) )\f]
24
25    where \f$g()\f$ is the remapped image, \f$f()\f$ the source image and \f$h(x,y)\f$ is the mapping function
26    that operates on \f$(x,y)\f$.
27
28-   Let's think in a quick example. Imagine that we have an image \f$I\f$ and, say, we want to do a
29    remap such that:
30
31    \f[h(x,y) = (I.cols - x, y )\f]
32
33    What would happen? It is easily seen that the image would flip in the \f$x\f$ direction. For
34    instance, consider the input image:
35
36    ![](images/Remap_Tutorial_Theory_0.jpg)
37
38    observe how the red circle changes positions with respect to x (considering \f$x\f$ the horizontal
39    direction):
40
41    ![](images/Remap_Tutorial_Theory_1.jpg)
42
43-   In OpenCV, the function @ref cv::remap offers a simple remapping implementation.
44
45Code
46----
47
48-#  **What does this program do?**
49    -   Loads an image
50    -   Each second, apply 1 of 4 different remapping processes to the image and display them
51        indefinitely in a window.
52    -   Wait for the user to exit the program
53
54-#  The tutorial code's is shown lines below. You can also download it from
55    [here](https://github.com/Itseez/opencv/tree/master/samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp)
56    @include samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp
57
58Explanation
59-----------
60
61-#  Create some variables we will use:
62    @code{.cpp}
63    Mat src, dst;
64    Mat map_x, map_y;
65    char* remap_window = "Remap demo";
66    int ind = 0;
67    @endcode
68-#  Load an image:
69    @code{.cpp}
70    src = imread( argv[1], 1 );
71    @endcode
72-#  Create the destination image and the two mapping matrices (for x and y )
73    @code{.cpp}
74    dst.create( src.size(), src.type() );
75    map_x.create( src.size(), CV_32FC1 );
76    map_y.create( src.size(), CV_32FC1 );
77    @endcode
78-#  Create a window to display results
79    @code{.cpp}
80    namedWindow( remap_window, WINDOW_AUTOSIZE );
81    @endcode
82-#  Establish a loop. Each 1000 ms we update our mapping matrices (*mat_x* and *mat_y*) and apply
83    them to our source image:
84    @code{.cpp}
85    while( true )
86    {
87      /// Each 1 sec. Press ESC to exit the program
88      int c = waitKey( 1000 );
89
90      if( (char)c == 27 )
91        { break; }
92
93      /// Update map_x & map_y. Then apply remap
94      update_map();
95      remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );
96
97      /// Display results
98      imshow( remap_window, dst );
99    }
100    @endcode
101    The function that applies the remapping is @ref cv::remap . We give the following arguments:
102
103    -   **src**: Source image
104    -   **dst**: Destination image of same size as *src*
105    -   **map_x**: The mapping function in the x direction. It is equivalent to the first component
106        of \f$h(i,j)\f$
107    -   **map_y**: Same as above, but in y direction. Note that *map_y* and *map_x* are both of
108        the same size as *src*
109    -   **INTER_LINEAR**: The type of interpolation to use for non-integer pixels. This is by
110        default.
111    -   **BORDER_CONSTANT**: Default
112
113    How do we update our mapping matrices *mat_x* and *mat_y*? Go on reading:
114
115-#  **Updating the mapping matrices:** We are going to perform 4 different mappings:
116    -#  Reduce the picture to half its size and will display it in the middle:
117        \f[h(i,j) = ( 2*i - src.cols/2  + 0.5, 2*j - src.rows/2  + 0.5)\f]
118        for all pairs \f$(i,j)\f$ such that: \f$\dfrac{src.cols}{4}<i<\dfrac{3 \cdot src.cols}{4}\f$ and
119        \f$\dfrac{src.rows}{4}<j<\dfrac{3 \cdot src.rows}{4}\f$
120    -#  Turn the image upside down: \f$h( i, j ) = (i, src.rows - j)\f$
121    -#  Reflect the image from left to right: \f$h(i,j) = ( src.cols - i, j )\f$
122    -#  Combination of b and c: \f$h(i,j) = ( src.cols - i, src.rows - j )\f$
123
124This is expressed in the following snippet. Here, *map_x* represents the first coordinate of
125*h(i,j)* and *map_y* the second coordinate.
126@code{.cpp}
127for( int j = 0; j < src.rows; j++ )
128{ for( int i = 0; i < src.cols; i++ )
129{
130      switch( ind )
131  {
132    case 0:
133      if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
134            {
135          map_x.at<float>(j,i) = 2*( i - src.cols*0.25 ) + 0.5 ;
136          map_y.at<float>(j,i) = 2*( j - src.rows*0.25 ) + 0.5 ;
137         }
138      else
139    { map_x.at<float>(j,i) = 0 ;
140          map_y.at<float>(j,i) = 0 ;
141            }
142              break;
143    case 1:
144          map_x.at<float>(j,i) = i ;
145          map_y.at<float>(j,i) = src.rows - j ;
146      break;
147        case 2:
148          map_x.at<float>(j,i) = src.cols - i ;
149          map_y.at<float>(j,i) = j ;
150      break;
151        case 3:
152          map_x.at<float>(j,i) = src.cols - i ;
153          map_y.at<float>(j,i) = src.rows - j ;
154      break;
155      } // end of switch
156}
157  }
158 ind++;
159}
160@endcode
161
162Result
163------
164
165-#  After compiling the code above, you can execute it giving as argument an image path. For
166    instance, by using the following image:
167
168    ![](images/Remap_Tutorial_Original_Image.jpg)
169
170-#  This is the result of reducing it to half the size and centering it:
171
172    ![](images/Remap_Tutorial_Result_0.jpg)
173
174-#  Turning it upside down:
175
176    ![](images/Remap_Tutorial_Result_1.jpg)
177
178-#  Reflecting it in the x direction:
179
180    ![](images/Remap_Tutorial_Result_2.jpg)
181
182-#  Reflecting it in both directions:
183
184    ![](images/Remap_Tutorial_Result_3.jpg)
185