OpenCV2.4.10 's Samples_cpp_tutorial-code_learn-----Imgtrans (affine transformation)

Source: Internet
Author: User
Tags rotate image

This series of learning notes is referenced from the OpenCV2.4.10 Opencv\sources\samples\cpp\tutorial_code and http://www.opencv.org.cn/opencvdoc/2.3.2/html/genindex.html


This blog post will continue to learn about the Imgtrans in Opencv-tutorial-code, which is mainly about affine transformations. Affine transformation is a kind of Cartesian coordinate system, which describes the transformation relationship of a picture to another picture. It represents a graph multiplied by a vector plus a transform of a vector. Affine transformations maintain straightness and parallelism, in which the straightness refers to a straight line after the transformation, and parallelism refers to the parallel lines that are still parallel to the transformation before they are transformed.

We can describe this kind of radiative transformation relationship. Assuming that the coordinates before the affine transformation are (x, y), the coordinates are (X1,Y1)

{x1 = ax+by+k1, y1 = cx+dy+k2}

Vector can be expressed as: X1 a B x K1

= x +

Y1 C D y K2


where the matrix: represents a linear transformation, a vector represents panning.

An affine transformation can also be represented by a matrix of the following 2x3:

| A B K1 |

| C D K2 |

The radial transformation formula is expressed as:

X1 a b k1 x

= x y

Y1 C D K2 1


Affine transformation can realize the translation, rotation, zooming, flipping and other operations of the image.


Geometric_transforms_demo.cpp

Demo source and comments are as follows:

#include "stdafx.h"//Precompiled Header file/** a simple demo code for the @ geometric transform */#include "opencv2/highgui/highgui.hpp" #include "OPENCV2/IMGPROC/IMGP Roc.hpp "#include <iostream> #include <stdio.h>using namespace cv;using namespace std;///global variable const char* Source_window = "Source image"; Const char* Warp_window = "warp"; const char* Warp_rotate_window = "warp + rotate";/** main function  */int Main (int, char** argv) {point2f srctri[3];  POINT2F Dsttri[3];  Mat Rot_mat (2, 3, CV_32FC1);  Mat Warp_mat (2, 3, CV_32FC1);  Mat src, warp_dst, warp_rotate_dst;  Load image src = imread ("D:\\opencv\\lena.png", 1);  Generate a DST image of the same size and type as the src image warp_dst = Mat::zeros (Src.rows, Src.cols, Src.type ());  Set your 3 points to calculate affine transformations srctri[0] = point2f (0,0);  SRCTRI[1] = point2f (src.cols-1.f, 0);  SRCTRI[2] = point2f (0, SRC.ROWS-1.F);  Dsttri[0] = point2f (src.cols*0.0f, src.rows*0.33f);  DSTTRI[1] = point2f (src.cols*0.85f, src.rows*0.25f);  DSTTRI[2] = point2f (src.cols*0.15f, src.rows*0.7f); Get affine transform Warp_mat = geTaffinetransform (Srctri, Dsttri);  Affine transformation Warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ());  /** rotate image////////////////////Compute the center of the image rotation  Double angle =-50.0;  Double scale = 0.6;  Get rotation Matrix Rot_mat = getrotationmatrix2d (center, angle, scale);  Select Image Warpaffine (WARP_DST, WARP_ROTATE_DST, Rot_mat, Warp_dst.size ());  Display Image Namedwindow (Source_window, window_autosize);  Imshow (Source_window, SRC);  Namedwindow (Warp_window, window_autosize);  Imshow (Warp_window, WARP_DST);  Namedwindow (Warp_rotate_window, window_autosize);  Imshow (Warp_rotate_window, WARP_ROTATE_DST);  Waitkey (0); return 0;}
Run as follows:


function Prototypes for Getaffinetransform: C + +: Mat getaffinetransform(const point2f* src, const point2f* DST) Two parameters are the coordinates of the source image and three points on the target image, respectively, and return an affine matrix of 2x3.

the function prototype for Warpaffine is:C + +: voidWarpaffine(Inputarraysrc, OutputarrayDST, InputarrayM, Sizedsize, intFlags=inter_linear, intBordermode=border_constant, const scalar&Bordervalue=scalar () ) The parameter src is the source image, DST is the affine transform, and M is the affine transformation of the 2x3. Size is the DST image. Several other parameters have default values, which are ignored for the time being.

the function prototype for getrotationmatrix2d is:C + +: Matgetrotationmatrix2d(point2fCenter, doubleAngle, double Scale where center is the source image rotation Center, angle is the rotation angle, the angle is negative when the counter-clockwise rotation, scale for image scaling small.


OpenCV2.4.10 's Samples_cpp_tutorial-code_learn-----Imgtrans (affine transformation)

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.