OpenCV Perspective Transformation

Source: Internet
Author: User
Tags scalar

OpenCV Perspective TransformationImplementing Perspective transformations Target:In this tutorial you will learn: 1, how to make perspective changes 2, how to survive perspective transformation matrix Theory:What is Perspective transformation: 1,The Perspective Transformation (Perspective transformation) is the projection of a picture into a new view plane (viewing Plane), also known as a projection map (projective Mapping). 2. Conversion Formula

U,v is the left side of the original picture, corresponding to the image coordinates x, y, which are transformed .
transformation matrices can be divided into 4 parts, represents linear transformations, such as scaling,shearing and ratotion. used for panning to create a perspective transform. So it can be understood that affine is a special form of perspective transformation. Images after a perspective transform are usually not parallelogram (unless you map the view plane to the original plane in parallel).

Rewrite the previous transformation formula to get:


Therefore, the transformation formula can be obtained by several points of the known transformation. Conversely, a specific transformation formula can also be a new transformed image. Simply look at the transformation of a square to a four-sided shape:
The 4 sets of corresponding points of the transformation can be expressed as:

According to the transformation formula, we get:


Define several helper variables:


All for 0 o'clock the transformation plane is parallel to the original and can be obtained:


Not for 0 o'clock, get:


The solved transformation matrix can transform a square into a quadrilateral. Conversely, the quadrilateral transforms to a square is the same. So, we pass two transformations: the quadrilateral transformation to the Square + square to the quadrilateral can transform any one quadrilateral to another quadrilateral.

Code: #include"OPENCV2/HIGHGUI.HPP"
#include"OPENCV2/IMGPROC.HPP"
#include<iostream>
#include<stdio.h>
usingnamespaceCv
usingnamespaceStd
/** @function Main */
intMainintargcChar* * argv)
{
Cv::mat src= cv::imread ("Test.jpg", 0);
if(!src.data)
return0;
Vector<point> Not_a_rect_shape;
Not_a_rect_shape.push_back (Point (122,0));
Not_a_rect_shape.push_back (Point (814,0));
Not_a_rect_shape.push_back (Point (22,540));
Not_a_rect_shape.push_back (Point (910,540));
For debugging purposes, draw green lines connecting those points
and save it on disk
Constpoint* point = &not_a_rect_shape[0];
intn = (int) Not_a_rect_shape.size ();
Mat draw = Src.clone ();
Polylines (Draw, &point, &n, 1,true, Scalar (0, 255, 0), 3, CV_AA);
Imwrite ("Draw.jpg", draw);
TopLeft, TopRight, BottomRight, Bottomleft
CV::P oint2f src_vertices[4];
Src_vertices[0] = not_a_rect_shape[0];
SRC_VERTICES[1] = not_a_rect_shape[1];
SRC_VERTICES[2] = not_a_rect_shape[2];
SRC_VERTICES[3] = not_a_rect_shape[3];

POINT2F Dst_vertices[4];
Dst_vertices[0] = point (0, 0);
DST_VERTICES[1] = point (960,0);
DST_VERTICES[2] = point (0,540);
DST_VERTICES[3] = point (960,540);
Mat Warpmatrix = Getperspectivetransform (src_vertices, dst_vertices);
Cv::mat rotated;
Warpperspective (SRC, rotated, Warpmatrix, Rotated.size (), inter_linear, border_constant);
Display the image
Cv::namedwindow ("Original Image");
Cv::imshow ("Original Image", SRC);
Cv::namedwindow ("warp Perspective");
Cv::imshow ("Warp Perspective", rotated);
Imwrite ("result.jpg", SRC);
Cv::waitkey ();
return0;
}
Code Explanation: 1, get the picture, if the input path is empty, the program exits directly   cv::Mat src= cv::imread( "test.jpg",0);
                 if (!src.data)
                                 return 0;
2, define the boundary point, input into the std::vector data structure. Note that the order here is as follows.   vector<Point> not_a_rect_shape;
                not_a_rect_shape.push_back(Point(122,0));
                not_a_rect_shape.push_back(Point(814,0));
                not_a_rect_shape.push_back(Point(22,540));
                not_a_rect_shape.push_back(Point(910,540));
and Mark these points out.         const Point* point = &not_a_rect_shape[0];
                int n = (int )not_a_rect_shape.size();
                Mat draw = src.clone();
                polylines(draw, &point, &n, 1, true, Scalar(0, 255, 0), 3, CV_AA);
                imwrite( "draw.jpg", draw);
3. Create Perspective Transformation Matrix CV::P oint2f src_vertices[4];
Src_vertices[0] = not_a_rect_shape[0];
SRC_VERTICES[1] = not_a_rect_shape[1];
SRC_VERTICES[2] = not_a_rect_shape[2];
SRC_VERTICES[3] = not_a_rect_shape[3];

POINT2F Dst_vertices[4];
Dst_vertices[0] = point (0, 0);
DST_VERTICES[1] = point (960,0);
DST_VERTICES[2] = point (0,540);
DST_VERTICES[3] = point (960,540);
Mat Warpmatrix = Getperspectivetransform (src_vertices, dst_vertices);
4. Perform the conversion     cv::Mat rotated;
                warpPerspective(src, rotated, warpMatrix, rotated.size(), INTER_LINEAR, BORDER_CONSTANT);
5. Display and save the results        // Display the image
                cv::namedWindow( "Original Image");
                cv::imshow( "Original Image",src);
                cv::namedWindow( "warp perspective");
                cv::imshow( "warp perspective",rotated);
                imwrite( "result.jpg",src);
Results: Original picture callout four boundary points picture after perspective transform It is important to note that the changed image here is missing some of the border details, which should be noted when implemented.

From for notes (Wiz)

OpenCV Perspective 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.