OpenCV  4.5.2
Open Source Computer Vision
Remapping

Prev Tutorial: Hough Circle Transform
Next Tutorial: Affine Transformations

Original author Ana Huamán
Compatibility OpenCV >= 3.0

Goal

In this tutorial you will learn how to:

a. Use the OpenCV function 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 \((x,y)\) as:

    \[g(x,y) = f ( h(x,y) )\]

    where \(g()\) is the remapped image, \(f()\) the source image and \(h(x,y)\) is the mapping function that operates on \((x,y)\).

  • Let's think in a quick example. Imagine that we have an image \(I\) and, say, we want to do a remap such that:

    \[h(x,y) = (I.cols - x, y )\]

    What would happen? It is easily seen that the image would flip in the \(x\) direction. For instance, consider the input image:

    observe how the red circle changes positions with respect to x (considering \(x\) the horizontal direction):

  • In OpenCV, the function cv::remap offers a simple remapping implementation.

Code

  • What does this program do?
    • 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

Explanation

  • Load an image:
  • Create the destination image and the two mapping matrices (for x and y )
  • Create a window to display results
  • Establish a loop. Each 1000 ms we update our mapping matrices (mat_x and mat_y) and apply them to our source image:
  • The function that applies the remapping is cv::remap . We give the following arguments:

    • 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 \(h(i,j)\)
    • 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:

  • Updating the mapping matrices: We are going to perform 4 different mappings:
    1. Reduce the picture to half its size and will display it in the middle:

      \[h(i,j) = ( 2 \times i - src.cols/2 + 0.5, 2 \times j - src.rows/2 + 0.5)\]

      for all pairs \((i,j)\) such that: \(\dfrac{src.cols}{4}<i<\dfrac{3 \cdot src.cols}{4}\) and \(\dfrac{src.rows}{4}<j<\dfrac{3 \cdot src.rows}{4}\)
    2. Turn the image upside down: \(h( i, j ) = (i, src.rows - j)\)
    3. Reflect the image from left to right: \(h(i,j) = ( src.cols - i, j )\)
    4. Combination of b and c: \(h(i,j) = ( src.cols - i, src.rows - j )\)

This is expressed in the following snippet. Here, map_x represents the first coordinate of h(i,j) and map_y the second coordinate.

Result

  1. After compiling the code above, you can execute it giving as argument an image path. For instance, by using the following image:

  2. This is the result of reducing it to half the size and centering it:

  3. Turning it upside down:

  4. Reflecting it in the x direction:

  5. Reflecting it in both directions:

cv::Mat::rows
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2096
cv::String
std::string String
Definition: cvstd.hpp:150
cv::BORDER_CONSTANT
@ BORDER_CONSTANT
iiiiii|abcdefgh|iiiiiii with some specified i
Definition: base.hpp:269
cv::samples::findFile
cv::String findFile(const cv::String &relative_path, bool required=true, bool silentMode=false)
Try to find requested data file.
cv::Mat::at
_Tp & at(int i0=0)
Returns a reference to the specified array element.
cv::waitKey
int waitKey(int delay=0)
Waits for a pressed key.
highgui.hpp
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
cv::imread
Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
cv::Mat::cols
int cols
Definition: mat.hpp:2096
cv::INTER_LINEAR
@ INTER_LINEAR
Definition: imgproc.hpp:249
imgcodecs.hpp
cv::dnn::print
static void print(const MatShape &shape, const String &name="")
Definition: shape_utils.hpp:198
cv::imshow
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
cv::Scalar
Scalar_< double > Scalar
Definition: types.hpp:669
cv::remap
void remap(InputArray src, OutputArray dst, InputArray map1, InputArray map2, int interpolation, int borderMode=BORDER_CONSTANT, const Scalar &borderValue=Scalar())
Applies a generic geometrical transformation to an image.
cv::Mat
n-dimensional dense array class
Definition: mat.hpp:801
cv::imshow
void imshow(const String &winname, const ogl::Texture2D &tex)
Displays OpenGL 2D texture in the specified window.
cv::CommandLineParser
Designed for command line parsing.
Definition: utility.hpp:799
cv::WINDOW_AUTOSIZE
@ WINDOW_AUTOSIZE
the user cannot resize the window, the size is constrainted by the image displayed.
Definition: highgui.hpp:188
cv::IMREAD_COLOR
@ IMREAD_COLOR
If set, always convert image to the 3 channel BGR color image.
Definition: imgcodecs.hpp:72
cv
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
imgproc.hpp
CV_32FC1
#define CV_32FC1
Definition: interface.h:118