Affine transformation and perspective transformation __OPENCV

Source: Internet
Author: User
Tags rotate image

Affine transformation my understanding is that the image is rotated or stretched at different degrees on a two-dimensional plane, but no matter how it changes, his image is parallelogram, and its transformation function is a 2*3 matrix.

Perspective transformation is an observer who observes the image in a 3-D space from different angles, so the image is projected on the observer's two-dimensional plane, and its transformation function is 3*3, or 4*4 matrix.

The affine transformation code is as follows:

#include <cv.h> #include The pivot transformation code is as follows:

#include <cv.h> #include  


Using OPENCV2 to realize affine transformation

#include <opencv2/opencv.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using
namespace CV;
using namespace Std;
	int main () {point2f srctriangle[3];
	POINT2F Dsttriangle[3];
	Mat Rotmat (2,3,CV_32FC1);
	Mat Warpmat (2,3,CV_32FC1);
	Mat srcimage,dstimage_warp,dstimage_warp_rotate;
	Srcimage=imread ("1.jpg", 1);
	Dstimage_warp=mat::zeros (Srcimage.rows,srcimage.cols,srcimage.type ());  Affine transformations require only 3 points to determine, because the radial transformation is a parallelogram srctriangle[0]=point2f (0,0);  The lower left corner of the image is srctriangle[1]=point2f (static_cast<float> (srcimage.cols-1), 0);  The lower right corner of the image is srctriangle[2]=point2f (0,static_cast<float> (srcimage.rows-1)); The upper-left corner of the image is dsttriangle[0]=point2f (static_cast<float> (srcimage.cols*0.0),static_cast<float> ( srcimage.rows*0.33)); (0,0) The coordinate dsttriangle[1]=point2f (static_cast<float> (srcimage.cols*0.65),static_cast<float> ( srcimage.rows*0.35)); DSTTRIANGLE[2]=POINT2F (static_cast<float> (srcimage.cols*0.15),static_cast<float> (srcImage.rows*0.6));  Warpmat=getaffinetransform (Srctriangle,dsttriangle);  In fact, the transformation matrix is determined by the position before and after the four-angle transformation (the position of the Four Corners is given) warpaffine (Srcimage,dstimage_warp,warpmat,dstimage_warp.size ());  Using this transformed matrix to transform the image////Here you can imagine that the image is first arranged in a given rule (here is a point of four corners), and then the others are arranged according to this rule imshow ("Xiaoguo", Dstimage_warp); Show effect chart//----------------The second way-------------------------------point Center=point (Dstimage_warp.cols/2,dstimage_
	WARP.ROWS/2);
	Double angle=-30.0;
	Double scale=0.8; rotmat=getrotationmatrix2d (Center,angle,scale); The rotation matrix Warpaffine (Dstimage_warp,dstimage_warp_rotate,rotmat,dstimage_warp_rotate.size ()) is reduced by the center point, the rotation angle, and the contraction amplification small structure.
	Same as the previous face Imshow ("Xiaoguo2", dstimage_warp_rotate);
	Waitkey (0);
return 0; }




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.