There are two common geometric transformations: one is based on the 2x3 matrix, the other is affine, and the other is based on the 3x3 matrix transformation, also called perspective transformation. The perspective transformation can be treated as a computational method of a three-dimensional plane perceived by a particular observer, who may not be observing the plane vertically. 1. Affine transformation
An affine transformation represents a mapping relationship between two images, which can be expressed as a matrix multiplied by a vector, usually using 2x3 matrices to represent affine transformations.
An affine transformation can be expressed as a form of y=axx+b, which is equivalent to extending the vector x to X ' and simply multiplying the X ' left by T, namely:
An affine transformation can be used to form the following form. The arbitrary parallelogram ABCD in a plane can be mapped to another parallelogram a ' B ' C ' D ' by affine transformation. If the area of these parallelogram is not equal to 0, this implied affine transformation is uniquely defined by the two parallelogram. The affine transformation can be imagined as drawing a picture onto a plastic plate, and pushing and pulling at any angle of the rubber plate to change the shape to obtain different types of parallelogram. 2. Perspective Transformation
Affine transformations can transform images into parallelogram, and perspective transforms provide greater flexibility, and a perspective transformation can transform a rectangle into a trapezoid, and a parallelogram is a trapezoid, so the affine transformation is a subset of perspective transformations. To set the pivot transformation matrix:
Then, the mapping relationship between the original image and the target image of the perspective transformation is:
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> using
namespace CV;
using namespace Std; #define WINDOW_NAME1 "Original Image" #define WINDOW_NAME2 "affine transformation" #define Window_name3
"Perspective Transformation" int main () {bool affine=true;
BOOL Perspective=true;
Mat srcimage, Dstimage_aff, Dstimage_per;
Load the source image and make some initialization srcimage = Imread ("Lena.png", 1);
if (!srcimage.data) {printf (read picture error \ n); return false;}
Dstimage_aff = Mat::zeros (Srcimage.rows, Srcimage.cols, Srcimage.type ());
Dstimage_per = Mat::zeros (Srcimage.rows, Srcimage.cols, Srcimage.type ());
if (affine) {//affine transformation Three-point mapping relationship point2f srctriangle[3];
POINT2F Dsttriangle[3]; Mat Affmat (2, 3, CV_32FC1); Affine transformation matrix//set three points on source image and target image to compute affine transformation srctriangle[0] = point2f (0,0); The upper left point of the original image srctriangle[1] = point2f ((srcimage.cols), 0); The upper right point of the original image srctriangle[2] = point2f (0, (srcimage.rows)); The lower left point of the original image DSttriangle[0] = point2f ((srcimage.cols*0.0), (srcimage.rows*0.5));
DSTTRIANGLE[1] = point2f ((srcimage.cols*0.5), (srcimage.rows*0.0));
DSTTRIANGLE[2] = point2f ((srcimage.cols*0.5), (srcimage.rows*1.0));
The affine transformation matrix is obtained and the affine transformation Affmat = Getaffinetransform (Srctriangle, Dsttriangle) is computed.
Output affine transformation matrix cout<< "affine transformation matrix:\n" <<AffMat<<endl;
Warpaffine (Srcimage, Dstimage_aff, Affmat,dstimage_aff.size ());
Imshow (window_name2, Dstimage_aff);
} if (Perspective) {//projection transforms four points of mapping relationship point2f srcquadrilateral[4];
POINT2F Dstquadrilateral[4]; Define some Mat variables Mat permat (3, 3, CV_32FC1); Perspective transformation matrix//set four points on source image and target image to compute perspective transform srcquadrilateral[0] = point2f (0, 0); The upper left point of the original image srcquadrilateral[1] = point2f ((srcimage.cols), 0); The upper right point of the original image srcquadrilateral[2] = point2f (0, (srcimage.rows)); The lower left point of the original image srcquadrilateral[3] = point2f ((srcimage.cols), (srcimage.rows)); The lower left point of the original image dstquadrilateRal[0] = point2f ((srcimage.cols*0.0), (srcimage.rows*0.5));
DSTQUADRILATERAL[1] = point2f ((srcimage.cols*0.5), (srcimage.rows*0.0));
DSTQUADRILATERAL[2] = point2f ((srcimage.cols*0.5), (srcimage.rows*1.0));
DSTQUADRILATERAL[3] = point2f ((srcimage.cols*1.0), (srcimage.rows*0.5));
To obtain the Perspective transformation matrix and calculate the perspective transformation Permat = Getperspectivetransform (srcquadrilateral, dstquadrilateral);
Output Perspective transformation Matrix Mat PERMAT2;
Permat.convertto (PERMAT2, cv_32f);
cout<< "Perspective Transformation matrix:\n" <<PerMat2<<endl;
Warpperspective (Srcimage, Dstimage_per, Permat,dstimage_per.size ());
Imshow (Window_name3, Dstimage_per);
///Display results imshow (window_name1, srcimage);
Waitkey (0);
return 0;
}
More resources: http://blog.csdn.net/xiaowei_cqu/article/details/26471527 http://m.blog.csdn.net/article/details?id=51355600
http://blog.csdn.net/u012380663/article/details/43273527