Affine transformation (AffineTransform)
Main functions:
// Get transformation matrix M=[a B] Warp_mat = Getaffinetransform (Srctri, Dsttri);
// Get rotation matrix Rot_mat = getrotationmatrix2d (center, angle, scale);
Important functions:
Warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ());
The code is as follows, and the explanations are all in the comments:
1#include <opencv2\opencv.hpp>2#include <iostream>3#include <string>4 5 #pragmaComment (linker, "/subsystem:\" windows\ "/entry:\" Maincrtstartup\ "")6 7 using namespacestd;8 using namespaceCV;9 Ten voidShowimg (Const string&win_name,ConstMat &img) One { A Namedwindow (Win_name, cv_window_autosize); - imshow (Win_name, IMG); - } the - intMainvoid) - { -POINT2F srctri[3]; +POINT2F dsttri[3]; - +Mat Rot_mat (2,3, CV_32FC1); AMat Warp_mat (2,3, CV_32FC1); at Mat src, warp_dst, rotate_dst; - -src = imread ("lena.jpg"); - if(Src.empty ()) - return-1; -Showimg ("src", SRC); in -WARP_DST =Mat::zeros (Src.rows, Src.cols, Src.type ()); to + //Original Sample Point -srctri[0] = POINT2F (0,0); thesrctri[1] = point2f (Src.cols-1,0); *srctri[2] = POINT2F (0, Src.rows-1); $ Panax Notoginseng //Building Map Points -dsttri[0] = POINT2F (src.cols*0.0, src.rows*0.33); thedsttri[1] = POINT2F (src.cols*0.85, src.rows*0.25); +dsttri[2] = POINT2F (src.cols*0.15, src.rows*0.7); A the //Get transformation matrix M=[a B] +Warp_mat =Getaffinetransform (Srctri, Dsttri); - $ warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ()); $ - //Rotation Center -Point Center = point (Src.cols/2, Src.rows/2); the //Rotation Angle - DoubleAngle =-50.0;Wuyi //scaling in rotation (because the square image will overflow if it does not scale), to ensure that it does not overflow, the scale should be under the square root of the two points below 2 (about 0.7) the DoubleScale =0.7; - Wu //Get rotation matrix -Rot_mat =getrotationmatrix2d (center, angle, scale); About $ warpaffine (SRC, rotate_dst, Rot_mat, Src.size ()); - -Showimg ("WARP_DST", WARP_DST); -Showimg ("WARP_ROTATE_DST", ROTATE_DST); A Waitkey (); + return 0; the}
Result Picture:
Affine:
Rotate zoom:
Please refer to the relevant mathematical data for the main instructions.
Above.
OPENCV Official Document Learning record (20)