Perspective transformation of Image geometric transformation

Source: Internet
Author: User

1. Fundamentals

The essence of Perspective Transformation (Perspective Transformation) is to project the image into a new view plane, and its general transformation formula is:

(U,V) is the original image pixel coordinate, (x=x '/w ', y=y '/w ') is the image pixel coordinates after the transformation. The Perspective transformation matrix is illustrated as follows:

Affine transformations (affine transformation) can be understood as a special form of perspective transformations. The mathematical expressions for perspective transformations are:

Therefore, the perspective transformation matrix can be obtained by the corresponding four pairs pixel coordinates of the perspective transform, and the perspective transformation matrix can be completed by the given perspective transform matrices, as shown in the following:

2. OPENCV Perspective Transform function

Mat getperspectivetransform (const point2f* SRC, const point2f* DST)//Calculate a perspective transform from four pairs of The corresponding points.//src–coordinates of quadrangle vertices in the source image.//dst–coordinates of the Corr  Esponding quadrangle vertices in the destination image.void warpperspective (inputarray src, outputarray DST, Inputarray M, Size dsize, int flags=inter_linear, int bordermode=border_constant, const scalar& bordervalue=scalar ())//Apply a per Spective transform to a image.//src–source image.//dst–destination image that has the size dsize and the same type As src.//m–3*3 transformation matrix.//dsize–size of the destination image.//flags–combination of interpolation M Ethods and the optional flag warp_inverse_map that means that's M is the inverse transformation (DST?SRC).//Bordermode–pi Xel extrapolation method. When bordermode=border_transparent, it means so the pixels in the destination image of corresponds to the "outliers" In the source image is not modified by the function.//Bordervalue–value used in case of a constant border. By default, it is 0.

3. Program

#include <iostream> #include "highgui.h" #include "opencv2/imgproc/imgproc.hpp" int main () {//Get original Image.cv::mat originalimage = Cv::imread ("Road.png");//Perspective Image.cv::mat perspectiveimage;//Perspective TRANSFORMCV::P oint2f objectivepoints[4], imagepoints[4];//original image points.imagepoints[0].x = 10.0; Imagepoints[0].y = 457.0;imagepoints[1].x = 395.0; Imagepoints[1].y = 291.0;imagepoints[2].x = 624.0; Imagepoints[2].y = 291.0;imagepoints[3].x = 1000.0; Imagepoints[3].y = 457.0;//Objective points of perspective image.//move up the perspective image:objectivepoints.y-v Alue.//Move left the perspective image:objectivepoints.x-value.double Movevaluex = 0.0;double Movevaluey = 0.0;objec tivepoints[0].x = 46.0 + Movevaluex; OBJECTIVEPOINTS[0].Y = 920.0 + movevaluey;objectivepoints[1].x = 46.0 + Movevaluex; OBJECTIVEPOINTS[1].Y = 100.0 + movevaluey;objectivepoints[2].x = 600.0 + Movevaluex; OBJECTIVEPOINTS[2].Y = 100.0 + movevaluey;objectivepoints[3].x = 600.0 + Movevaluex; OBJECTIVEPOINTS[3].Y = 920.0 + Movevaluey;cv::mat transform = Cv::getperspectivetransform (objectivepoints, imagePoints  );//Perspective.cv::warpperspective (Originalimage, Perspectiveimage,transform,cv::size (OriginalImage.rows, originalimage.cols), Cv::inter_linear | CV::WARP_INVERSE_MAP);//Cv::imshow ("Perspective image", perspectiveimage);//Cvwaitkey (0); Cv::imwrite (" Perspectiveimage.png ", perspectiveimage); return 0;}

Original image and its perspective transformation results:

Perspective transformation of Image geometric 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.