2014-11-27 20:39:05 +08:00
|
|
|
Remapping {#tutorial_remap}
|
|
|
|
=========
|
|
|
|
|
2020-12-08 00:13:54 +08:00
|
|
|
@tableofcontents
|
|
|
|
|
2022-09-10 23:40:31 +08:00
|
|
|
@prev_tutorial{tutorial_generalized_hough_ballard_guil}
|
2018-08-03 02:22:58 +08:00
|
|
|
@next_tutorial{tutorial_warp_affine}
|
|
|
|
|
2020-12-05 06:46:00 +08:00
|
|
|
| | |
|
|
|
|
| -: | :- |
|
|
|
|
| Original author | Ana Huamán |
|
|
|
|
| Compatibility | OpenCV >= 3.0 |
|
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Goal
|
|
|
|
----
|
|
|
|
|
|
|
|
In this tutorial you will learn how to:
|
|
|
|
|
|
|
|
a. Use the OpenCV function @ref cv::remap to implement simple remapping routines.
|
|
|
|
|
|
|
|
Theory
|
|
|
|
------
|
|
|
|
|
|
|
|
### What is remapping?
|
|
|
|
|
|
|
|
- It is the process of taking pixels from one place in the image and locating them in another
|
|
|
|
position in a new image.
|
|
|
|
- To accomplish the mapping process, it might be necessary to do some interpolation for
|
|
|
|
non-integer pixel locations, since there will not always be a one-to-one-pixel correspondence
|
|
|
|
between source and destination images.
|
|
|
|
- We can express the remap for every pixel location \f$(x,y)\f$ as:
|
|
|
|
|
|
|
|
\f[g(x,y) = f ( h(x,y) )\f]
|
|
|
|
|
|
|
|
where \f$g()\f$ is the remapped image, \f$f()\f$ the source image and \f$h(x,y)\f$ is the mapping function
|
|
|
|
that operates on \f$(x,y)\f$.
|
|
|
|
|
|
|
|
- Let's think in a quick example. Imagine that we have an image \f$I\f$ and, say, we want to do a
|
|
|
|
remap such that:
|
|
|
|
|
|
|
|
\f[h(x,y) = (I.cols - x, y )\f]
|
|
|
|
|
|
|
|
What would happen? It is easily seen that the image would flip in the \f$x\f$ direction. For
|
|
|
|
instance, consider the input image:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Theory_0.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
observe how the red circle changes positions with respect to x (considering \f$x\f$ the horizontal
|
|
|
|
direction):
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Theory_1.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
- In OpenCV, the function @ref cv::remap offers a simple remapping implementation.
|
|
|
|
|
|
|
|
Code
|
|
|
|
----
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
- **What does this program do?**
|
2014-11-27 20:39:05 +08:00
|
|
|
- Loads an image
|
|
|
|
- Each second, apply 1 of 4 different remapping processes to the image and display them
|
|
|
|
indefinitely in a window.
|
|
|
|
- Wait for the user to exit the program
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
@add_toggle_cpp
|
|
|
|
- The tutorial code's is shown lines below. You can also download it from
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp)
|
2015-04-29 15:31:53 +08:00
|
|
|
@include samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp
|
2018-05-19 01:51:34 +08:00
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
- The tutorial code's is shown lines below. You can also download it from
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java)
|
2018-05-19 01:51:34 +08:00
|
|
|
@include samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
- The tutorial code's is shown lines below. You can also download it from
|
2021-12-22 21:01:26 +08:00
|
|
|
[here](https://github.com/opencv/opencv/tree/4.x/samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py)
|
2018-05-19 01:51:34 +08:00
|
|
|
@include samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
Explanation
|
|
|
|
-----------
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
- Load an image:
|
|
|
|
|
|
|
|
@add_toggle_cpp
|
|
|
|
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Load
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Load
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Load
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
- Create the destination image and the two mapping matrices (for x and y )
|
|
|
|
|
|
|
|
@add_toggle_cpp
|
|
|
|
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Create
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Create
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Create
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
- Create a window to display results
|
|
|
|
|
|
|
|
@add_toggle_cpp
|
|
|
|
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Window
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Window
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Window
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
- Establish a loop. Each 1000 ms we update our mapping matrices (*mat_x* and *mat_y*) and apply
|
2014-11-27 20:39:05 +08:00
|
|
|
them to our source image:
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
@add_toggle_cpp
|
|
|
|
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Loop
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
@add_toggle_java
|
|
|
|
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Loop
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
@add_toggle_python
|
|
|
|
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Loop
|
|
|
|
@end_toggle
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
- The function that applies the remapping is @ref cv::remap . We give the following arguments:
|
2014-11-27 20:39:05 +08:00
|
|
|
- **src**: Source image
|
|
|
|
- **dst**: Destination image of same size as *src*
|
|
|
|
- **map_x**: The mapping function in the x direction. It is equivalent to the first component
|
|
|
|
of \f$h(i,j)\f$
|
|
|
|
- **map_y**: Same as above, but in y direction. Note that *map_y* and *map_x* are both of
|
|
|
|
the same size as *src*
|
|
|
|
- **INTER_LINEAR**: The type of interpolation to use for non-integer pixels. This is by
|
|
|
|
default.
|
|
|
|
- **BORDER_CONSTANT**: Default
|
|
|
|
|
|
|
|
How do we update our mapping matrices *mat_x* and *mat_y*? Go on reading:
|
|
|
|
|
2018-05-19 01:51:34 +08:00
|
|
|
- **Updating the mapping matrices:** We are going to perform 4 different mappings:
|
2014-11-28 00:54:13 +08:00
|
|
|
-# Reduce the picture to half its size and will display it in the middle:
|
2018-05-19 01:51:34 +08:00
|
|
|
\f[h(i,j) = ( 2 \times i - src.cols/2 + 0.5, 2 \times j - src.rows/2 + 0.5)\f]
|
2014-11-27 20:39:05 +08:00
|
|
|
for all pairs \f$(i,j)\f$ such that: \f$\dfrac{src.cols}{4}<i<\dfrac{3 \cdot src.cols}{4}\f$ and
|
|
|
|
\f$\dfrac{src.rows}{4}<j<\dfrac{3 \cdot src.rows}{4}\f$
|
2014-11-28 00:54:13 +08:00
|
|
|
-# Turn the image upside down: \f$h( i, j ) = (i, src.rows - j)\f$
|
|
|
|
-# Reflect the image from left to right: \f$h(i,j) = ( src.cols - i, j )\f$
|
|
|
|
-# Combination of b and c: \f$h(i,j) = ( src.cols - i, src.rows - j )\f$
|
2014-11-27 20:39:05 +08:00
|
|
|
|
|
|
|
This is expressed in the following snippet. Here, *map_x* represents the first coordinate of
|
|
|
|
*h(i,j)* and *map_y* the second coordinate.
|
2018-05-19 01:51:34 +08:00
|
|
|
|
|
|
|
@add_toggle_cpp
|
|
|
|
@snippet samples/cpp/tutorial_code/ImgTrans/Remap_Demo.cpp Update
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_java
|
|
|
|
@snippet samples/java/tutorial_code/ImgTrans/remap/RemapDemo.java Update
|
|
|
|
@end_toggle
|
|
|
|
|
|
|
|
@add_toggle_python
|
|
|
|
@snippet samples/python/tutorial_code/ImgTrans/remap/Remap_Demo.py Update
|
|
|
|
@end_toggle
|
2014-11-28 21:21:28 +08:00
|
|
|
|
2014-11-27 20:39:05 +08:00
|
|
|
Result
|
|
|
|
------
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# After compiling the code above, you can execute it giving as argument an image path. For
|
2014-11-27 20:39:05 +08:00
|
|
|
instance, by using the following image:
|
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Original_Image.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# This is the result of reducing it to half the size and centering it:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Result_0.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# Turning it upside down:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Result_1.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# Reflecting it in the x direction:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Result_2.jpg)
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
-# Reflecting it in both directions:
|
2014-11-27 20:39:05 +08:00
|
|
|
|
2014-11-28 21:21:28 +08:00
|
|
|
![](images/Remap_Tutorial_Result_3.jpg)
|