Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the Perspective Transformation function of C++ with OpenCV

Shulou Source: shulou.com Published: 2022-06-02 05:17:19 10月03日 Update

This article mainly explains "C++ how to use OpenCV to achieve perspective transformation function", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ uses OpenCV to achieve perspective transformation.

Preface

When we take a picture, we can't guarantee that the picture is taken vertically at the bottom, so when we get the image, it will prevent us from extracting the correct image, so we need to use perspective transformation here. Today we will try the perspective transformation in OpenCV.

Realize the effect

The above picture is our direct shooting of the mobile phone, we can find that the image is not taken vertically above.

The above is the new picture effect generated by the perspective transformation.

API function

GetPerspectiveTransform

Calculation of transmission transformation from four pairs of points

CvMat* cvGetPerspectiveTransform (const CvPoint2D32f* src, const CvPoint2D32f* dst, CvMat*map_matrix)

Detailed description of parameters:

Src enters the quadrilateral vertex coordinates of the image.

Dst outputs the corresponding quadrilateral vertex coordinates of the image.

Map_matrix A pointer to the 3 × 3 output matrix.

The function cvGetPerspectiveTransform calculates the transmission transformation matrix that satisfies the following relations:

Here, dst (I) = (xonomiMagneyfanti), src (I) = (xi,yi), I = 0.. 3.

WarpPerspective function

Main function: perspective transformation of the image

The calling form of the function:

Void warpPerspective (InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar ())

Detailed description of parameters:

InputArray src: imported ima

OutputArray dst: exported ima

InputArray M: the Matrix of Perspective Transformation

Size dsize: the size of the output image

Int flags=INTER_LINEAR: interpolation method of output Image

Code demonstration

Let's create a new project named opencv--toushi, follow the configuration properties (VS2017 configure OpenCV generic properties), and then write # include and main methods in the source file.

Define the coordinates of the perspective transformation

The definition of the points in the src_corners above is based on the points set by ourselves on our original diagram. Later we will find an opportunity to use the program to obtain these points.

Dst_corners is the point that we want to generate after the matrix change.

The order of defining four points should be obtained clockwise, as shown in the following figure

Get the perspective transformation matrix and display the last image

The resulting effect

The picture on the right is the image effect we finally get through perspective transformation. On the basis of this image, it is very simple for us to do some feature extraction.

At this point, I believe that everyone on the "C++ how to use OpenCV to achieve perspective transformation function" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Transformations images matrices outputs functions effects shooting functions clocking pictures coordinates methods contents parameters epigenetic quadrilateral quadrilateral attributes time vertices Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Technology macOS OPPO Reno vpn