OpenCV notes (19)--Remapping the image

Source: Internet
Author: User
Tags scalar

What is remapping? The translation is a remapping. Because each image itself is f (x, y), it is a function of the pixel value of the pixel position as an argument. Then we re-transform the values of the pixels on these locations,

g (x, Y) = f (h (x, y)), h (x, y) is the remap function, and g (x, y) is the result of the remap.

Imagine what would happen if H (x, y) = f (i.cols-x, y).

Obviously, the new image is the original image about the x-axis of the flip.

First look at the definition of remapping operation in OpenCV

void Remap(inputarray src, outputarray DST, Inputarray map1, Inputarray map2, int interpolation, intbordermode=border_constant, const scalar& Bordervalue=scalar ())

SRC is an input image with no special requirements

DST is the output image, consistent with the SRC dimension type. The document says remap cannot be operated in place, it is recommended that DST not be the same as SRC.

MAP1 is the map on the x-axis, MAP1 and SRC are of the same size, and the type is generally cv_32fc1. e.g. Map1.at<float> (i, j) = (src.cols-i);

MAP2 is a mapping on the y-axis

Interpolation is typically inter_linear here, using the bilinear interpolation method to convert a pair of floating-point coordinates (after mapping) to integer coordinates. See:

Originating From: Http://en.wikipedia.org/wiki/Bilinear_interpolation

Bordermode General default is border_constant, here the border I also not very clear meaning.

OpenCV's example of using remap is clear and easy to understand, note how MAP1 and MAP2 are set up:

1 /**2 * @function remap_demo.cpp3 * @brief Demo code for Remap4 * @author Ana Huaman5  */6 7#include"opencv2/highgui/highgui.hpp"8#include"opencv2/imgproc/imgproc.hpp"9#include <iostream>Ten#include <stdio.h> One  A using namespaceCV; -  - ///Global Variables the Mat src, DST; - Mat map_x, map_y; - Const Char* Remap_window ="Remap Demo"; - intIND =0; +  - ///Function Headers + voidUpdate_map (void ); A  at /** - * @function Main -  */ - intMainint,Char**argv) - { -   ///Load the image insrc = imread (argv[1],1 ); -  to   ///Create DST, map_x and map_y with the same size as SRC: + dst.create (Src.size (), Src.type ()); - map_x.create (Src.size (), CV_32FC1); the map_y.create (Src.size (), CV_32FC1); *  $   ///Create windowPanax Notoginseng Namedwindow (Remap_window, window_autosize); -  the   ///Loop +    for(;;) A   { the     ///Each of 1 sec. Press ESC to exit the program +     intc = Waitkey ( + ); -  $     if( (Char) c = = - ) ${ Break; } -  -     ///Update map_x & map_y. Then apply remap the Update_map (); -Remap (src, DST, map_x, map_y, Cv_inter_linear, Border_constant, Scalar (0,0,0) );Wuyi  the     //Display Results - imshow (Remap_window, DST); Wu   } -   return 0; About } $  - /** - * @function Update_map - * @brief Fill the map_x and map_y matrices with 4 types of mappings A  */ + voidUpdate_map (void ) the { -IND = ind%4; $  the    for(intj =0; J < Src.rows; J + + ) the{ for(inti =0; i < Src.cols; i++ ) the      { the            Switch(Ind) -          { in           Case 0: the            if(I > src.cols*0.25&& I < src.cols*0.75&& J > src.rows*0.25&& J < src.rows*0.75 ) the                  { Aboutmap_x.at<float> (j,i) =2* (i-src.cols*0.25f) +0.5f ; themap_y.at<float> (j,i) =2* (j-src.rows*0.25f) +0.5f ; the               } the            Else +{map_x.at<float> (j,i) =0 ; -map_y.at<float> (j,i) =0 ; the                  }Bayi                     Break; the           Case 1: themap_x.at<float> (j,i) = (float) I; -map_y.at<float> (j,i) = (float) (Src.rows-j); -             Break; the               Case 2: themap_x.at<float> (j,i) = (float) (Src.cols-i); themap_y.at<float> (j,i) = (float) J; the             Break; -               Case 3: themap_x.at<float> (j,i) = (float) (Src.cols-i); themap_y.at<float> (j,i) = (float) (Src.rows-j); the             Break;94}//End of Switch the      } the     } theinqd++;98}
View Code

OpenCV notes (19)--Remapping the image

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.