OpenCV Geometric transformations-image mirroring

Source: Internet
Author: User
Tags scalar

Image mirroring is one of the basic geometric transformations of the image, it is also very simple to implement, first paste the source code:

#include <opencv/highgui.h> #include <time.h> #include <opencv2/opencv.hpp> #include &LT;OPENCV/CV  
.h> #include <iostream> using namespace std; 

using namespace CV;
    int main () {Mat srcimage = Imread ("1.jpg", 0);
    Imshow ("original", Srcimage);    
    Mat DST;    
    Dst.create (Srcimage.size (), Srcimage.type ());    
    Mat map_x;    
    Mat map_y;    
    Map_x.create (Srcimage.size (), CV_32FC1);    
    Map_y.create (Srcimage.size (), CV_32FC1);    
            for (int i = 0, i < srcimage.rows; i++) {for (int j = 0; J < Srcimage.cols; J + +) {    
            Map_x.at<float> (i, j) = (float) (srcimage.cols-j-1);  Map_y.at<float> (i, j) = (float) i;    
            Horizontal map_x.at<float> (i, j) = (float) j;  Map_y.at<float> (i, j) = (float) (srcimage.rows-i-1);    
     Vertical}} Remap (Srcimage, DST, map_x, map_y, cv_inter_linear);   Imshow ("Mirror map", DST);
        Waitkey (0);
return 0; }

The image mirroring function mainly uses the remap () function in OpenCV, which is prototyped as follows:

Cv_exports_w void Remap ( 
inputarray src, 
outputarray DST,
Inputarray map1, 
inputarray map2,
int interpolation, 
int bordermode=border_constant,
const scalar& bordervalue=scalar ());

The function is to remap the input image src based on the MAP1 and map2 two matrices, and to output the mapping results in DST. So the parameters are straightforward:
First parameter: Enter an image
Second parameter: Output image
Third parameter: the coordinates of each pixel point in the input image are mapped to which x (column) of the target image
Fourth parameter: the coordinates of each pixel point in the input image are mapped to which X (row) of the target image
The fifth parameter: image interpolation method, you can refer to: common interpolation methods
Sixth parameter: Boundary mode with default value of Border_constant.
The seventh parameter: the const scalar& type of Bordervalue, which has a default value of Scalar () when there is a constant boundary, that is, the default value is 0.

So, exactly how the image or remapping is determined by the MAP1 and map2 two matrix, we go back to the above code to see how to implement:

for (int i = 0, i < srcimage.rows; i++)    
    {for    
        (int j = 0; J < Srcimage.cols; J + +)    
        {    
            //MAP_X.AT<FL Oat> (i, j) = (float) (srcimage.cols-j-1);    
            Map_y.at<float> (i, j) = (float) i;  Horizontal  
            map_x.at<float> (i, j) = (float) j;    
            Map_y.at<float> (i, j) = (float) (srcimage.rows-i-1);  Vertical  
        }    
    }    

The assignment of a matrix is implemented here:
Assuming that the original image is 6*3 size, when traversing to i=0,j=0, and
map_x.at (0, 0) = 0
map_y.at (0, 0) = 3-0-1=2
That is, the original image (0, 0) is mapped to the target image (0, 2), in the same vein:
map_x.at (0, 2) = 0
map_y.at (0, 2) = 3-2-1=0
The original image (0, 2) is mapped to the target image (0,0), so vertical mirroring is achieved. It is important to note that the determinant of the image in the OpenCV is starting from 0, so (srcimage.rows-i-1), otherwise the image after mirroring will have a black edge.

Note: In fact, there is a little do not know that we are not aware of, we only use a single channel, and did not use Image.at<vec3b> (I,J) [0] Such a way of writing, why can mirror color map it.
because the map_x and map_y matrices are not the grayscale of the pixel, but the position before and after the mapping.

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.