OpenCV  3.0.0-rc1
Open Source Computer Vision
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Remapping

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?

Code

  1. 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
  2. The tutorial code's is shown lines below. You can also download it from here
    1 
    7 #include "opencv2/imgcodecs.hpp"
    10 #include <iostream>
    11 #include <stdio.h>
    12 
    13 using namespace cv;
    14 
    16 Mat src, dst;
    17 Mat map_x, map_y;
    18 const char* remap_window = "Remap demo";
    19 int ind = 0;
    20 
    22 void update_map( void );
    23 
    27 int main( int, char** argv )
    28 {
    30  src = imread( argv[1], 1 );
    31 
    33  dst.create( src.size(), src.type() );
    34  map_x.create( src.size(), CV_32FC1 );
    35  map_y.create( src.size(), CV_32FC1 );
    36 
    38  namedWindow( remap_window, WINDOW_AUTOSIZE );
    39 
    41  for(;;)
    42  {
    44  int c = waitKey( 1000 );
    45 
    46  if( (char)c == 27 )
    47  { break; }
    48 
    50  update_map();
    51  remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0, 0, 0) );
    52 
    53  // Display results
    54  imshow( remap_window, dst );
    55  }
    56  return 0;
    57 }
    58 
    63 void update_map( void )
    64 {
    65  ind = ind%4;
    66 
    67  for( int j = 0; j < src.rows; j++ )
    68  { for( int i = 0; i < src.cols; i++ )
    69  {
    70  switch( ind )
    71  {
    72  case 0:
    73  if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
    74  {
    75  map_x.at<float>(j,i) = 2*( i - src.cols*0.25f ) + 0.5f ;
    76  map_y.at<float>(j,i) = 2*( j - src.rows*0.25f ) + 0.5f ;
    77  }
    78  else
    79  { map_x.at<float>(j,i) = 0 ;
    80  map_y.at<float>(j,i) = 0 ;
    81  }
    82  break;
    83  case 1:
    84  map_x.at<float>(j,i) = (float)i ;
    85  map_y.at<float>(j,i) = (float)(src.rows - j) ;
    86  break;
    87  case 2:
    88  map_x.at<float>(j,i) = (float)(src.cols - i) ;
    89  map_y.at<float>(j,i) = (float)j ;
    90  break;
    91  case 3:
    92  map_x.at<float>(j,i) = (float)(src.cols - i) ;
    93  map_y.at<float>(j,i) = (float)(src.rows - j) ;
    94  break;
    95  } // end of switch
    96  }
    97  }
    98  ind++;
    99 }
    T & at(int idx, std::vector< T > &items)
    Definition: ring_buffer.hpp:57
    Scalar_< double > Scalar
    Definition: types.hpp:597
    Mat imread(const String &filename, int flags=IMREAD_COLOR)
    Loads an image from a file.
    void imshow(const String &winname, InputArray mat)
    Displays an image in the specified window.
    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.
    void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
    Creates a window.
    for i
    Definition: modelConvert.m:63
    #define CV_32FC1
    Definition: cvdef.h:146
    Definition: imgproc.hpp:251
    Definition: highgui.hpp:138
    iiiiii|abcdefgh|iiiiiii with some specified i
    Definition: base.hpp:234
    int main(int argc, const char *argv[])
    Definition: facerec_demo.cpp:67
    n-dimensional dense array class
    Definition: mat.hpp:726
    int waitKey(int delay=0)
    Waits for a pressed key.

Explanation

  1. Create some variables we will use:
    Mat src, dst;
    Mat map_x, map_y;
    char* remap_window = "Remap demo";
    int ind = 0;
  2. Load an image:
    src = imread( argv[1], 1 );
  3. Create the destination image and the two mapping matrices (for x and y )
    dst.create( src.size(), src.type() );
    map_x.create( src.size(), CV_32FC1 );
    map_y.create( src.size(), CV_32FC1 );
  4. Create a window to display results
    namedWindow( remap_window, WINDOW_AUTOSIZE );
  5. Establish a loop. Each 1000 ms we update our mapping matrices (mat_x and mat_y) and apply them to our source image:

    while( true )
    {
    int c = waitKey( 1000 );
    if( (char)c == 27 )
    { break; }
    update_map();
    remap( src, dst, map_x, map_y, INTER_LINEAR, BORDER_CONSTANT, Scalar(0,0, 0) );
    imshow( remap_window, dst );
    }

    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:

  6. 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*i - src.cols/2 + 0.5, 2*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.

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

Result

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

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

    Remap_Tutorial_Result_0.jpg
  3. Turning it upside down:

    Remap_Tutorial_Result_1.jpg
  4. Reflecting it in the x direction:

    Remap_Tutorial_Result_2.jpg
  5. Reflecting it in both directions:

    Remap_Tutorial_Result_3.jpg