OpenCV notes (20)--affine Transformations affine transformation

Source: Internet
Author: User
Tags scalar

The effect of affine transformation is to make the image rotate and stretch.

The affine transformation is the position of the pixel of the source image to be transformed to the specified target image by an intermediate matrix, similar to the remapping above.

So affine transformations are also an application of matrices.

So affine transformation is generally divided into two steps: first, to find the intermediate matrix of Transformation, and second, to transform.

To find the intermediate matrix of the transformation, we usually use three points to find it, because we can show the scale and the angle of the transformation with the triangle. In OpenCV, we use Getaffinetransform.

When transforming, we use the Warpaffine function.

Mat getaffinetransform(inputarray src, inputarray DST)

SRC and DST use point2f to be arrays (size 3).

void warpaffine(inputarray src, outputarray DST, Inputarray M, Size C12>dsize, int flags=inter_linear, intbordermode=border_constant, const scalar& bordervalue=scalar ())

The M here is the return value obtained from the above getaffinetransform.

Dsize is the size of the output, take the DST size on the line.

In addition, you can use getrotationmatrix2d to get M.

Mat getrotationmatrix2d(point2f Center, double angle, double scale)

Center of the image after the rotation

Angle is the angle of rotation, it is counterclockwise and negative is clockwise.

Scale is a factor for scaling, for example, it can be 0.5

Examples of OpenCV:

1 /**2 * @function geometric_transforms_demo.cpp3 * @brief Demo code for geometric Transforms4 * @author OpenCV team5  */6 7#include"opencv2/highgui/highgui.hpp"8#include"opencv2/imgproc/imgproc.hpp"9#include <iostream>Ten#include <stdio.h> One  A using namespaceCV; - using namespacestd; -  the ///Global Variables - Const Char* Source_window ="Source Image"; - Const Char* Warp_window ="Warp"; - Const Char* Warp_rotate_window ="Warp + Rotate"; +  - /** + * @function Main A  */ at intMainint,Char**argv) - { -POINT2F srctri[3]; -POINT2F dsttri[3]; -  -Mat Rot_mat (2,3, CV_32FC1); inMat Warp_mat (2,3, CV_32FC1); - Mat src, warp_dst, warp_rotate_dst; to  +   ///Load the image -src = imread (argv[1],1 ); the  *   ///Set the DST image the same type and size as src $WARP_DST =Mat::zeros (Src.rows, Src.cols, Src.type ());Panax Notoginseng  -   ///Set your 3 points to calculate the affine Transform thesrctri[0] = POINT2F (0,0 ); +srctri[1] = point2f (Src.cols-1. F,0 ); Asrctri[2] = POINT2F (0, Src.rows-1. f); the  +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 the affine Transform -Warp_mat =Getaffinetransform (Srctri, Dsttri); the  -   ///Apply the affine Transform just found to the SRC imageWuyi warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ()); the  -   /** Rotating the image after Warp*/ Wu  -   ///Compute a rotation matrix with respect to the center of the image AboutPoint Center = point (warp_dst.cols/2, warp_dst.rows/2 ); $   DoubleAngle =-50.0; -   DoubleScale =0.6; -  -   ///Get the rotation matrix with the specifications above ARot_mat =getrotationmatrix2d (center, angle, scale); +  the   ///Rotate the warped image - warpaffine (WARP_DST, WARP_ROTATE_DST, Rot_mat, Warp_dst.size ()); $  the  the   ///Show You got the Namedwindow (Source_window, window_autosize); the imshow (Source_window, SRC); -  in Namedwindow (Warp_window, window_autosize); the imshow (Warp_window, WARP_DST); the  About Namedwindow (Warp_rotate_window, window_autosize); the imshow (Warp_rotate_window, WARP_ROTATE_DST); the  the   ///Wait until user exits the program +Waitkey (0); -  the   return 0;Bayi}

OpenCV notes (20)--affine Transformations 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.