How to Use opencv to implement Projection Transformation

Source: Internet
Author: User

In application development, if the obtained video image is after the projection transformation and needs to be corrected, you can use the cvwarpperspective function to implement the perspective transformation. Through the perspective transformation, you can flip the image, linear arbitrary deformation.

You can also use the projection transform cvgetquadranglesubpix to flip an image, but it has three less degrees of freedom, so any linear deformation cannot be achieved.

 

// Perspective transformation
Int humanmotion: warpperspective (iplimage * gray)
{
Iplimage * img_old = cvcloneimage (Gray );
 
Int fix = 4;
Int W = img_old-> width + fix;
Int H = img_old-> height + fix;
 
Cvpoint2d32f src_point [4];
Cvpoint2d32f dst_point [4];

// Set the source observation surface, that is, the four vertices of the specified Quadrilateral
Src_point [0]. x = 17;
Src_point [0]. Y = 475;
 
Src_point [1]. x= 952;
Src_point [1]. Y = 449;
 
Src_point [2]. x = 100;
Src_point [1]. Y = 128;
 
Src_point [3]. x = 840;
Src_point [3]. Y = 110;

 
// Set the target observation surface, that is, the four vertices of the specified Quadrilateral

Dst_point [0]. x =-fix;
Dst_point [0]. Y = h;
 
Dst_point [1]. x = W;
Dst_point [1]. Y = h;

Dst_point [2]. x =-fix;
Dst_point [2]. Y =-fix;
 
Dst_point [3]. x = W;
Dst_point [3]. Y =-fix;

 
Float newm [9];
Cvmat newm = cvmat (3, 3, cv_32f, newm );
// Obtain the perspective conversion matrix
Cvwarpperspectiveqmatrix (src_point, dst_point, & newm );
// Perspective Conversion
Cvwarpperspective (img_old, gray, & newm, cv_inter_linear + cv_warp_fill_outliers, cvscalarall (0 ));
Cvreleaseimage (& img_old );

Return 0;
 
}

 

// Test the Projection Transformation

Int humanmotion: test_cvmul2 (iplimage * gray)
{

Iplimage * src = cvcloneimage (Gray );
 
Float M [6];
Int angle = frame_count % 60-30;
 
Cvmat M = cvmat (2, 3, cv_32f, M );
Int W = Src-> width;
Int H = Src-> height;
 
M [0] = (float) (COS (angle * cv_pi/180 .));
M [1] = (float) (sin (angle * cv_pi/180 .));
M [2] = W * 0.5f;
M [3] =-M [1];
M [4] = m [0];
M [5] = H * 0.5f;
 
Cvgetquadranglesubpix (SRC, gray, & M );
 
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.