*
Geometric transformation of image —— stretching , shrink , Twist , rotate (stretch,shrink,distortion,rotation)

        
stretching , shrink , Twist , Rotation is the geometric transformation of image , These transformations are widely used in 3D vision technology , It is also divided into affine transformation and perspective transformation . Affine transformation usually uses Homography (homography) modeling , utilize cvWarpAffine Solving dense affine transformation , use cvTransform Solving sparse affine transformation . Affine transformation can transform rectangle into parallelogram , It can flatten the edges of a rectangle, but it must keep the edges parallel , You can also rotate or scale the rectangle . Perspective transformation provides more flexibility , A perspective transformation can transform a matrix into a trapezoid . of course , Parallelogram is also trapezoid , So affine transformation is a subset of perspective transformation .

1, affine transformation (Affine transformation)

               

          

                   

       
therefore , Affine transformation in space transformation corresponds to five transformations , translation , zoom , rotate , Flip , Crosscutting . And the process of these five changes from the original image to the transformed image , It can be described by affine transformation matrix . And this transformation process can use a 2*3 By multiplying the matrix of .

2, Perspective transformation (Perspective transformation)

            

          

It can be concluded from the principle , In addition to the transformation matrix, perspective transformation is a 3*3 The matrix and the need for four control points outside , Perspective transformation is similar to affine transformation in other aspects .

3, OpenCV inside API Introduction of

3.1 affine transformation

         OpenCV Affine transformation is realized by the combination of two functions , Two steps of affine transformation :

Step 1 : Get affine mapping matrix ( two types ):
(1)getAffineTransform // Three point method Mat M1=getAffineTransform(const Point2f* src,
const Point2f* dst) // parameter const Point2f* src: Three fixed vertices of the original image // parameter const Point2f*
dst: Three fixed vertices of the target image // Return value :Mat Type transformation matrix , Can be used directly warpAffine() function
// be careful , Vertex array length exceeds 3 individual , Will automatically 3 Transform vertices ; Array available Point2f[] or Point2f* express (2)getRotationMatrix2D
// Specify scale and angle directly Mat M2=getRotationMatrix2D (CvPoint2D32f center,double angle,double
scale) // parameter CvPoint2D32f center, Represents the rotation center of the source image // parameter double angle, Rotation angle , A positive value indicates counterclockwise rotation
// parameter double scale, Scaling factor
Step 2 : Perform affine transformation
C++ void warpAffine(InputArray src, OutputArray dst, InputArray M, Size dsize,
int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar&
borderValue=Scalar()) // parameter InputArray src: Input pre transform image // parameter OutputArray
dst: Output transformed image , An empty matrix needs to be initialized to hold the results , Do not set matrix size // parameter InputArray
M: Transformation matrix , Use another function getAffineTransform() calculation // parameter Size dsize: Set output image size // parameter int flags =
INTER_LINEAR: Set interpolation method , The default is linear interpolation ( Another WARP_FILL_OUTLIERS) // parameter int
borderMode=BORDER_CONSTANT: Boundary pixel mode , Default BORDER_CONSTANT // parameter const Scalar&
borderValue=Scalar(), Value under constant boundary condition , The default value is Scalar(), Namely 0
Fifth parameter interpolation mode optional : 

         

Sample program :
// affine transformation — translation , rotate , zoom , Flip , Crosscutting #include "stdafx.h" #include<opencv2/opencv.hpp>
#include<iostream> #include<math.h> using namespace cv; using namespace std;
int main(int argc, char* argv) { Mat src, dst; src =
imread("C:/Users/59235/Desktop/image/girl5.jpg"); if (!src.data) {
printf("could not load image...\n"); return -1; } namedWindow("original image",
CV_WINDOW_AUTOSIZE); imshow("original image", src); Mat dst_warp,
dst_warpRotateScale, dst_warpTransformation, dst_warpFlip; Point2f
srcPoints[3];// Three points in the original picture , One contains 3D points (x,y) Array of , among x,y Is a floating point number Point2f
dstPoints[3];// Three points in the target graph // The first call mode of affine transformation : Three point method
// Values of three point pairs , It also says , As long as you know the coordinates of the three points of the transformed graph , Then affine transformation can be realized srcPoints[0] = Point2f(0, 0);
srcPoints[1] = Point2f(0, src.rows); srcPoints[2] = Point2f(src.cols, 0);
// Three coordinate values after mapping dstPoints[0] = Point2f(0, src.rows*0.3); dstPoints[1] =
Point2f(src.cols*0.25, src.rows*0.75); dstPoints[2] = Point2f(src.cols*0.75,
src.rows*0.25); Mat M1 = getAffineTransform(srcPoints, dstPoints);// Calculating transformation matrix from three pairs of points
warpAffine(src, dst_warp, M1, src.size());// affine transformation // The second call mode of affine transformation : Specify angle and scale directly
// Rotation plus scaling Point2f center(src.cols / 2, src.rows / 2);// Center of rotation double angle =
45;// Counter clockwise rotation 45 degree double scale = 0.5;// Scaling Mat M2 = getRotationMatrix2D(center,
angle, scale);// Calculating the transformation matrix of rotation plus scaling warpAffine(src, dst_warpRotateScale, M2,
Size(src.cols, src.rows), INTER_LINEAR);// affine transformation // affine transformation — translation Point2f srcPoints1[3];
Point2f dstPoints1[3]; srcPoints1[0] = Point2i(0, 0); srcPoints1[1] =
Point2i(0, src.rows); srcPoints1[2] = Point2i(src.cols, 0); dstPoints1[0] =
Point2i(src.cols / 3, 0); dstPoints1[1] = Point2i(src.cols / 3, src.rows);
dstPoints1[2] = Point2i(src.cols + src.cols / 3, 0); Mat M3 =
getAffineTransform(srcPoints1, dstPoints1); warpAffine(src,
dst_warpTransformation, M3, Size(src.cols + src.cols / 3, src.rows));
// affine transformation — Flip , image Point2f srcPoints2[3]; Point2f dstPoints2[3]; srcPoints2[0] =
Point2i(0, 0); srcPoints2[1] = Point2i(0, src.rows); srcPoints2[2] =
Point2i(src.cols, 0); dstPoints2[0] = Point2i(src.cols, 0); dstPoints2[1] =
Point2i(src.cols, src.rows); dstPoints2[2] = Point2i(0, 0); Mat M4 =
getAffineTransform(srcPoints2, dstPoints2); warpAffine(src, dst_warpFlip, M4,
Size(src.cols, src.rows)); //flip(src, dst_warpFlip, 1);// flipCode:= 0 Image flip down
//> 0 Image flip right //< 0 Image flipped down and right at the same time imshow("affine transformation1( Three point method )", dst_warp);
imshow("affine transfoemation2( Specify scale and angle )", dst_warpRotateScale); imshow("affine
transfoemation3( Affine transformation translation )", dst_warpTransformation); imshow("affine
transformation4( Affine transformation image )", dst_warpFlip); waitKey(0); return 0; }
design sketch :

                                                             ( Original )

   

                                                                             
          ( Two methods of affine transformation rotation )

                                ( Affine transformation translation )                                      
                                          ( Affine transformation flip )

analysis :
Five translation of affine transformation , rotate , zoom , Flip ( In addition to using the three-point method to find the affine transformation matrix, flip image can also be used flip Function implementation ) And crosscutting , It is mainly about the setting of an affine mapping matrix , Flexible setting of different transformation matrix can get different transformation effects , The affine transformation here wants to get a good effect, such as the size of the image does not change after rotation , Image still in the center of the window , You need to resize the window and rotate it , Image Translation .

3.2 Perspective transformation

        Perspective transformed API And affine transformation API Very similar , Same principle , The difference is that the former three points are changed into four points to find the perspective transformation matrix , The transformation matrix is also determined by 2*3 Change to 3*3;
(1)MatgetPerspectiveTransform(const Point2f* src, const Point2f* dst)
// parameter const Point2f* src: Four fixed vertices of the original image // parameter const Point2f* dst: Four fixed vertices of the target image
// Return value :Mat Type transformation matrix , Can be used directly warpAffine() function
// be careful , Vertex array length exceeds 4 individual , Will automatically 4 Transform vertices ; Array available Point2f[] or Point2f* express // be careful : The point selection of perspective transformation becomes 4 individual (2)C++
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize,
int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar&
borderValue=Scalar()) // parameter InputArray src: Input pre transform image // parameter OutputArray
dst: Output transformed image , An empty matrix needs to be initialized to hold the results , Do not set matrix size // parameter InputArray
M: Transformation matrix , Use another function getAffineTransform() calculation // parameter Size dsize: Set output image size // parameter int flags =
INTER_LINEAR: Set interpolation method , The default is linear interpolation ( Another WARP_FILL_OUTLIERS)
Sample program :
// Perspective transformation #include "stdafx.h" #include<opencv2/opencv.hpp> #include<iostream>
#include<math.h> using namespace cv; using namespace std; int main(int argc,
char* argv) { Mat src, dst; src =
imread("C:/Users/59235/Desktop/image/girl5.jpg"); if (!src.data) {
printf("could not load image...\n"); return -1; } namedWindow("original image",
CV_WINDOW_AUTOSIZE); imshow("original image", src); Mat dst_warp,
dst_warpRotateScale, dst_warpTransformation, dst_warpFlip; Point2f
srcPoints[4];// Four points in the original picture , One contains 3D points (x,y) Array of , among x,y Is a floating point number Point2f
dstPoints[4];// Four points in the target graph srcPoints[0] = Point2f(0, 0); srcPoints[1] = Point2f(0,
src.rows); srcPoints[2] = Point2f(src.cols, 0); srcPoints[3] =
Point2f(src.cols, src.rows); // Four coordinate values after mapping dstPoints[0] = Point2f(src.cols*0.1,
src.rows*0.1); dstPoints[1] = Point2f(0, src.rows); dstPoints[2] =
Point2f(src.cols, 0); dstPoints[3] = Point2f(src.cols*0.7, src.rows*0.8); Mat
M1 = getPerspectiveTransform(srcPoints, dstPoints);// Calculating perspective transformation matrix from four point pairs
warpPerspective(src, dst_warp, M1, src.size());// affine transformation imshow("perspective
transformation1( Four point method )", dst_warp); waitKey(0); return 0; }
design sketch :                                                         ( Perspective transformation )

                    

Technology
©2019-2020 Toolsou All rights reserved,
JS How to operate java Realize the function of grabbing red packets C Language programming to find a student's grade The United Nations 《 Glory of Kings 》 Please go to the studio : To save the earth Dialogue between apple and Nissan suspended ,Apple Car How's it going ?CSS architecture design China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour First knowledge MySQL Comprehensive review ( dried food )2021 year 1 Monthly programmer salary statistics , average 14915 element How to use it quickly html and css Write static page