How to do it...

In order to use the OpenCV remap function, first you simply have to define the map to be used in the remapping process. Second, you have to apply this map on an input image. Obviously, it is the way you define your map that will determine the effect that will be produced. In our example, we define a transformation function that will create a wavy effect on the image. To do it, follow the next steps:

  1. Create our wave function with two params, an input image, and an output image result:
// remapping an image by creating wave effects 
void wave(const cv::Mat &image, cv::Mat &result) { 
 
  1. Create the two mapping variables, one for x positions and another for y positions, which store the new positions for remapping:
  // the map functions 
  cv::Mat srcX(image.rows,image.cols,CV_32F); 
  cv::Mat srcY(image.rows,image.cols,CV_32F); 
  1. Loop over every pixel:
  // creating the mapping 
  for (int i=0; i<image.rows; i++) { 
    for (int j=0; j<image.cols; j++) {
  1. Assign to the map the x position the actual position, j:
srcX.at<float>(i,j)= j; // remain on same column
  1. Calculate the new position for y using a sinusoid function using the x value as an input of sin:
srcY.at<float>(i,j)= i+5*sin(j/10.0); 
  1. Close the loop for calculating map variables and apply the remap function to the input image:
    } 
  } 
 
  // applying the mapping 
  cv::remap(image, result, srcX, srcY, cv::INTER_LINEAR); 
} 

The result is as follows:

Let's see how the preceding instructions work when we execute them.