OPENCV implementation of affine transformations

Source: Internet
Author: User

What is affine transformation??
    1. An arbitrary affine transformation can be expressed as multiplying by a matrix (linear transformation) followed by a vector (translation).

    2. To sum up, we can use affine transformations to represent:

      1. Rotation (linear transformation)
      2. Pan (vector Plus)
      3. Scaling Operations (linear transformations)

      As you can now see, in fact, the affine transformation represents the relationship between the two graphs.

    #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include < Stdio.h>using namespace cv;using namespace std;///global variable char* source_window = "source image"; char* Warp_window = "warp";   char* Warp_rotate_window = "warp + rotate";/** @function main */int main (int argc, char** argv) {point2f srctri[3];   POINT2F Dsttri[3];   Mat Rot_mat (2, 3, CV_32FC1);   Mat Warp_mat (2, 3, CV_32FC1);   Mat src, warp_dst, warp_rotate_dst;   Load source image src = imread (argv[1], 1);   Sets the size and type of the target image consistent with the source image warp_dst = Mat::zeros (Src.rows, Src.cols, Src.type ());   Set three sets of points on the source and target images to calculate affine transformations srctri[0] = point2f (0,0);   SRCTRI[1] = point2f (src.cols-1, 0);   SRCTRI[2] = point2f (0, src.rows-1);   Dsttri[0] = point2f (src.cols*0.0, src.rows*0.33);   DSTTRI[1] = point2f (src.cols*0.85, src.rows*0.25);   DSTTRI[2] = point2f (src.cols*0.15, src.rows*0.7);   The affine transformation Warp_mat = Getaffinetransform (Srctri, Dsttri) is obtained; Application of the affine transformation obtained from the source imageChange Warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ());   /** the image is distorted and then rotated */////The rotation matrix with a 50-degree scaling factor of 0.6 clockwise rotation around the image point Center = point (WARP_DST.COLS/2, WARP_DST.ROWS/2);   Double angle =-50.0;   Double scale = 0.6;   The rotation matrix Rot_mat = getrotationmatrix2d (center, angle, scale) is obtained through the above rotation detail information;   Rotate distorted Image warpaffine (WARP_DST, WARP_ROTATE_DST, Rot_mat, Warp_dst.size ());   Showing results Namedwindow (Source_window, cv_window_autosize);   Imshow (Source_window, SRC);   Namedwindow (Warp_window, cv_window_autosize);   Imshow (Warp_window, WARP_DST);   Namedwindow (Warp_rotate_window, cv_window_autosize);   Imshow (Warp_rotate_window, WARP_ROTATE_DST);   Wait for the user to press any key to exit the program Waitkey (0);  return 0; }
    Description?
    1. Define some variables that need to be used, such as the mat that needs to store the middle and target images, and two two-dimensional array of points that need to be used to define affine transformations.

      POINT2F Srctri[3]; POINT2F Dsttri[3]; Mat Rot_mat (2, 3, CV_32FC1); Mat Warp_mat (2, 3, CV_32FC1); Mat src, warp_dst, warp_rotate_dst;
    2. To load the source image:

      src = imread (argv[1], 1);
    3. WARP_DST = Mat::zeros (Src.rows, Src.cols, Src.type ()); 
    4. affine transformation:   As mentioned above, we need three points on the source and target images that are one by one mappings to define affine transformations:

      srctri[0] = point2f (0,0); srctri[1] = point2f (src.cols-1, 0); srctri[2] = Point2f (0, src.ro WS-1);d sttri[0] = point2f (src.cols*0.0, src.rows*0.33);d sttri[1] = point2f (src.cols*0.85, src.rows*0.25);d sttri[2] = POINT2F (src.cols*0.15, src.rows*0.7); 

      You might want to draw these dots to get a more intuitive feel for the transformation. Their position is probably the position of the point in the legend above (principle section). You will notice that the size and direction of the triangles defined by three points have changed.

    5. With these two sets of points, we can use the OpenCV function Getaffinetransform to find the affine transformation:

      Warp_mat = Getaffinetransform (Srctri, Dsttri);

      We have obtained a 2X3 matrix to describe affine transformations (here is warp_mat)

    6. Apply the affine transform you just obtained to the source image

      Warpaffine (SRC, warp_dst, Warp_mat, Warp_dst.size ());

      The function has the following parameters:

        • src: input source image
        • warp_dst: Output image
        • Warp_mat: affine transformation matrix
        • warp_dst.size (): size of output image

      So we get the transformed image! We're going to show it. Before this, we also want to rotate it ...

    7. rotation:   to rotate an image, you need two parameters:

      1. rotate the image around the center
      2. angle of rotation. In OpenCV, the angle is counterclockwise
      3. selectable:   scaling factor

      point Center = point (WARP_DST.COLS/2, WARP_DST.ROWS/2);d ouble angle = -50.0;double Scale = 0.6 ;
    8. We use the OpenCV function getrotationmatrix2d to get the rotation matrix, which returns a 2X3 matrix (here is Rot_mat)

      Rot_mat = getrotationmatrix2d (center, angle, scale);
    9. Now apply the rotation to the output of the affine transformation.

      Warpaffine (WARP_DST, WARP_ROTATE_DST, Rot_mat, Warp_dst.size ());
    10. Finally we draw the affine transform and the result of the rotation in the form, and the source image is drawn out for reference:

      Namedwindow (Source_window, cv_window_autosize); Imshow (Source_window, SRC); Namedwindow (Warp_window, CV_WINDOW_ AUTOSIZE); Imshow (Warp_window, WARP_DST); Namedwindow (Warp_rotate_window, cv_window_autosize); Imshow (Warp_rotate_ window, WARP_ROTATE_DST);
    11. Waiting for the user to exit the program

      Waitkey (0);

OPENCV implementation of affine transformations

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.