OpenCV
Open Source Computer Vision
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Remapping

Prev Tutorial: Object detection with Generalized Ballard and Guil Hough 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.colsx,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
  • The tutorial code is shown lines below. You can also download it from here
    #include <iostream>
    using namespace cv;
    void update_map( int &ind, Mat &map_x, Mat &map_y );
    int main(int argc, const char** argv)
    {
    CommandLineParser parser(argc, argv, "{@image |chicky_512.png|input image name}");
    std::string filename = parser.get<std::string>(0);
    Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
    if (src.empty())
    {
    std::cout << "Cannot read image: " << filename << std::endl;
    return -1;
    }
    Mat dst(src.size(), src.type());
    Mat map_x(src.size(), CV_32FC1);
    Mat map_y(src.size(), CV_32FC1);
    const char* remap_window = "Remap demo";
    namedWindow( remap_window, WINDOW_AUTOSIZE );
    int ind = 0;
    for(;;)
    {
    update_map(ind, map_x, map_y);
    remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
    imshow( remap_window, dst );
    char c = (char)waitKey( 1000 );
    if( c == 27 )
    {
    break;
    }
    }
    return 0;
    }
    void update_map( int &ind, Mat &map_x, Mat &map_y )
    {
    for( int i = 0; i < map_x.rows; i++ )
    {
    for( int j = 0; j < map_x.cols; j++ )
    {
    switch( ind )
    {
    case 0:
    if( j > map_x.cols*0.25 && j < map_x.cols*0.75 && i > map_x.rows*0.25 && i < map_x.rows*0.75 )
    {
    map_x.at<float>(i, j) = 2*( j - map_x.cols*0.25f ) + 0.5f;
    map_y.at<float>(i, j) = 2*( i - map_x.rows*0.25f ) + 0.5f;
    }
    else
    {
    map_x.at<float>(i, j) = 0;
    map_y.at<float>(i, j) = 0;
    }
    break;
    case 1:
    map_x.at<float>(i, j) = (float)j;
    map_y.at<float>(i, j) = (float)(map_x.rows - i);
    break;
    case 2:
    map_x.at<float>(i, j) = (float)(map_x.cols - j);
    map_y.at<float>(i, j) = (float)i;
    break;
    case 3:
    map_x.at<float>(i, j) = (float)(map_x.cols - j);
    map_y.at<float>(i, j) = (float)(map_x.rows - i);
    break;
    default:
    break;
    } // end of switch
    }
    }
    ind = (ind+1) % 4;
    }
    Designed for command line parsing.
    Definition utility.hpp:890
    n-dimensional dense array class
    Definition mat.hpp:829
    _Tp & at(int i0=0)
    Returns a reference to the specified array element.
    int cols
    Definition mat.hpp:2155
    int rows
    the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
    Definition mat.hpp:2155
    #define CV_32FC1
    Definition interface.h:118
    int main(int argc, char *argv[])
    Definition highgui_qt.cpp:3
    Definition core.hpp:107

Explanation

  • Load an image:

    Mat src = imread( samples::findFile( filename ), IMREAD_COLOR );
    if (src.empty())
    {
    std::cout << "Cannot read image: " << filename << std::endl;
    return -1;
    }
  • Create the destination image and the two mapping matrices (for x and y )

    Mat dst(src.size(), src.type());
    Mat map_x(src.size(), CV_32FC1);
    Mat map_y(src.size(), CV_32FC1);
  • Create a window to display results

    const char* remap_window = "Remap demo";
    namedWindow( remap_window, WINDOW_AUTOSIZE );
  • Establish a loop. Each 1000 ms we update our mapping matrices (mat_x and mat_y) and apply them to our source image:

    int ind = 0;
    for(;;)
    {
    update_map(ind, map_x, map_y);
    remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
    imshow( remap_window, dst );
    char c = (char)waitKey( 1000 );
    if( c == 27 )
    {
    break;
    }
    }
  • 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×isrc.cols/2+0.5,2×jsrc.rows/2+0.5)

      for all pairs (i,j) such that: src.cols4<i<3src.cols4 and src.rows4<j<3src.rows4
    2. Turn the image upside down: h(i,j)=(i,src.rowsj)
    3. Reflect the image from left to right: h(i,j)=(src.colsi,j)
    4. Combination of b and c: h(i,j)=(src.colsi,src.rowsj)

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

void update_map( int &ind, Mat &map_x, Mat &map_y )
{
for( int i = 0; i < map_x.rows; i++ )
{
for( int j = 0; j < map_x.cols; j++ )
{
switch( ind )
{
case 0:
if( j > map_x.cols*0.25 && j < map_x.cols*0.75 && i > map_x.rows*0.25 && i < map_x.rows*0.75 )
{
map_x.at<float>(i, j) = 2*( j - map_x.cols*0.25f ) + 0.5f;
map_y.at<float>(i, j) = 2*( i - map_x.rows*0.25f ) + 0.5f;
}
else
{
map_x.at<float>(i, j) = 0;
map_y.at<float>(i, j) = 0;
}
break;
case 1:
map_x.at<float>(i, j) = (float)j;
map_y.at<float>(i, j) = (float)(map_x.rows - i);
break;
case 2:
map_x.at<float>(i, j) = (float)(map_x.cols - j);
map_y.at<float>(i, j) = (float)i;
break;
case 3:
map_x.at<float>(i, j) = (float)(map_x.cols - j);
map_y.at<float>(i, j) = (float)(map_x.rows - i);
break;
default:
break;
} // end of switch
}
}
ind = (ind+1) % 4;
}

Result

  1. After compiling the code above, you can execute it giving as argument an image path. For instance, by using the following image:
  1. This is the result of reducing it to half the size and centering it:
  1. Turning it upside down:
  1. Reflecting it in the x direction:
  1. Reflecting it in both directions: