Chapter 6-image transformation-image stretching, shrinkage, distortion, and rotation [1]-cvwarpaffine)

Source: Internet
Author: User

Stretching, shrinkage, distortion, and rotation are geometric transformations of images. These transformations are widely applied in 3D vision technology, and include affine transformation and perspective transformation. Generally, a single-application modeling method is used for affine transformation. cvwarpaffine is used for dense ing, and cvtransform is used for sparse ing. The ry transform can convert a rectangle into a parallelogram. It can flatten the side of the rectangle But must keep the side parallel. It can also rotate the rectangle or change proportionally. Perspective transformation provides greater flexibility. A perspective transformation can convert a matrix into a trapezoid. Of course, the parallelogram is also a trapezoid, so the affine transformation is a subset of the perspective transformation.

This section describes how to implement image affine transformation.

Bytes ------------------------------------------------------------------------------------

The following are the methods used in this routine:

Cloneimage
Complete image copying

Iplimage * cvcloneimage (const iplimage * image );
Image
Original image.
Function cvcloneimage: Creates a complete copy of the image, including the header, ROI, and data.
-----------------------------

Getaffinetransform

Calculate the affine transformation from three vertices

Cvmat * cvgetaffinetransform (const cvpoint2d32f * SRC, const cvpoint2d32f * DST, cvmat * map_matrix );

SRC

Coordinates of the triangle vertices of the input image.

DST

Output the corresponding triangle vertex coordinates of the image.

Map_matrix

Pointer to the 2x3 output matrix.

Function cvgetaffinetransform:

Here,DST(I) = (X'I,Y'I),SRC(I) = (XI,Yi),I= 0 .. 2.

-----------------------------

Warpaffine

Apply the affine transform to the image

Void cvwarpaffine (const cvarr * SRC, cvarr * DST, constcvmat * map_matrix,

Int flags = cv_inter_linear + cv_warp_fill_outliers,

Cvscalar fillval = cvscalarall (0 ));

SRC

Input image.

DST

Output image.

Map_matrix

2 × 3 Transformation Matrix

Flags

A combination of the interpolation method and the following switch options:

· Cv_warp_fill_outliers-fill the pixels of all output images. If some pixels fall beyond the boundary of the input image, their values are set to fillval.

· Cv_warp_inverse_map-specifies that map_matrix is the inverse transformation from the output image to the input image. Therefore, it can be directly used for pixel interpolation. Otherwise, the function returns a inverse transformation from map_matrix.

Fillval

Used to fill out the value of the boundary.

The cvwarpaffine function transforms the input image using the matrix specified below:

  • If cv_warp_inverse_map is not specified,
  • Otherwise,

The function is similar to cvgetquadranglesubpix, but not identical. Cvwarpaffine requires that the input and output images have the same data type and have higher resource overhead (which is not suitable for small images), and the output images can be retained. Cvgetquadranglesubpix can accurately extract a quadrilateral from an 8-bit image to a floating-point cache, which has a low system overhead and always changes the content of the output image. To change the sparse matrix, use the cvtransform function in cxcore.

-----------------------------

2 drotationmatrix

2 drotationmatrix
Computes the two-dimensional rotating affine transformation matrix

Cvmat * cv2drotationmatrix (cvpoint2d32f center, double angle,
Double scale, cvmat * map_matrix );
Center
Coordinates of the input image rotation center
Angle
Rotation Angle (degree ). A positive value indicates clockwise rotation (the coordinate origin is assumed in the upper left corner ).
Scale
Scale Factor of the same sex
Map_matrix
Returns the pointer of a 2x3 matrix.
Function cv2drotationmatrix calculation matrix:

[α | (1-α) * center. X-β * center. Y]
[-β α | β * center. x + (1-α) * center. Y]

Where α = scale * Cos (angle), β = scale * sin (angle)
This transformation does not change the coordinates of the original rotation center. If this is not an operation purpose, you can change the coordinates by adjusting the translation volume, the implementation of affine transformation is to first set the center of rotation to the coordinate origin, then rotate and scale transformation, and finally set the coordinate origin to the upper left corner of the input image. The translation here is center. x, center. Y ).

Bytes ------------------------------------------------------------------------------------

/* Code */

# Include 

Bytes ------------------------------------------------------------------------------------

/* Result */

Source image

Rotate 50 degrees clockwise to change the image size and distortion

Rotate 90 degrees clockwise to further reduce the image size and Deformation

We can see that the cvwarpaffine affine transform can easily transform the image, and the transformed image is a parallelogram.

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.