Image rotation based on C + + and OpenCV underlying

Source: Internet
Author: User

Image rotation: Essentially, the position of each pixel in the rotated picture is calculated at the original .

There is a spin function in the OPENCV package, when you know the tilt angle theta:

The getrotationmatrix2d can be 2x3 with the rotation transformation matrix M, which can be tilted after using the warpaffine function for the image DST.

Very convenient Ah, why do you have to achieve the bottom of the image rotation? Because there are places where you have problems with these two functions, such as:

When the size of the original is MXN, and the image is fully populated (because if the problem is not fully reflected), now you need to change it 90° (for illustration), but what happens with the previous two APIs? As follows:

Oh? There's a part of it that can't be seen ... Let's take a look at the above two functions:

C + +: Mat getrotationmatrix2d (point2f Center, double angle, double scale)

C + +: void Warpaffine (Inputarray src, outputarray DST, Inputarray M, Size dsize, int? ags=inter_linear, int border Mode=border_constant, const scalar& bordervalue=scalar ())

At first I thought it was the setup of these two parameters. In general, the center is set in the source image of the central point to flip, and now a part of the invisible, is not the central point of the problem? Can I change the center point? The experimental results show that some images will not appear when the upper and lower right and the bottom corner are the center points. I give up the center, try to dsize, change the larger point, or not, but the display and black area is larger, the proportion of the display has not changed anything. You don't have to try it this time, I might as well write a spin function myself. Why this, because the original area is not large , Ah, a spin out ah, so still do not want to write their own function (not recommended, because the function code is very simple AH) of the children's shoes, with the copymakeborder function in the original image to add the boundary and then rotate it. After adding the boundary, the problem comes again: the small image added by the boundary will still spin out. The boundary is large, the picture itself is very small (like in a vast black universe, of course, exaggerated), then you have to the original picture of the pixels of what processing is more difficult, a little bit. If necessary, we have to go to the border, OpenCV there seems to be no ready to go to the boundary function ah.

Therefore, it is necessary for us to write a simple and convenient rotation function for ourselves (long ago). First, the rotation matrix and its transformation relationship are given (don't ask me how I got here, I was taught by a ready-made teacher, but I know a friend can illustrate)

The code is as follows:

1Mat Anglerectify (Mat img,floatangle)2 {3Mat Retmat = Mat::zeros (550,850, CV_8UC3);4     floatAnglepi = (float) (Angle * CV_PI/ the);5     intxSm, ySm;6      for(inti =0; i < retmat.rows; i++)7          for(intj =0; J < Retmat.cols; J + +)8         {9XSm = (int) ((i-retmat.rows/2) *cos (Anglepi)-(j-retmat.cols/2) *sin (Anglepi) +0.5);TenYSm = (int) ((i-retmat.rows/2) *sin (Anglepi) + (j-retmat.cols/2) *cos (Anglepi) +0.5); OneXSm + = img.rows/2; AYSm + = Img.cols/2; -             if(xSm >= img.rows | | ySm >= img.cols | | xSm <=0|| YSm <=0){ -Retmat.at<vec3b> (i, j) = Vec3b (0,0); the             } -             Else{ -Retmat.at<vec3b> (i, j) = Img.at<vec3b>(XSm, ySm); -             } +         } -  +     returnRetmat; A}

Here are a few questions to note:

1 Originally need post-transform picture multiplied by inverse matrix of the original transformation matrix corresponds to the coordinates in the original image. But because y axis direction downward, so post image multiplied by the original transformation matrix (no inverse matrix required) can correspond to the original coordinates

2 9th, line needs - the reason for retmat.rows/2 and retmat.cols/2 is that images are (RETMAT.COLS/2, RETMAT.ROWS/2) rotated for the origin of the coordinates, so each pixel in the picture is transformed . (i; j), a new coordinate that needs to be shifted to the center of the relative rotation, i.e. (I-MAT.ROWS/2; j-mat.cols/2).

  1. , restore the old coordinates to the origin of the upper-left corner again;
  2. Before the matrix subscript and the original transformation matrix are multiplied, the matrix subscript two values should be interchanged. After multiplying, the subscript values need to be interchanged again to revert to the matrix subscript. What does this mean, we can see , but the actual xSm = (int) ((I-RETMAT.ROWS/2) *cos (Anglepi)-(J-RETMAT.COLS/2) *  Sin (Anglepi) + 0.5); (I-RETMAT.ROWS/2) is what we think of the ordinate.
  3. the rotation angle integer represents clockwise, and the negative number represents counterclockwise (this OpenCV used in many cases)
  4. this represents the rotation of the color map, vec3b is a color type, if it is a grayscale, the <> in the type replaced by Uchar and Will retmat.at<vec3b> (i, j) = vec3b (0, 0); =>retmat.at<uchar> (i, j) = Uchar (0);
  5. If you are rotating -° This knownDSTof thesizeThe case can be specifiedRematof thesizeso that no boundary, other types will still have boundaries (but more than their ownCopymakeborderwill be smaller), because you don't knowRemathow big it needs to be.

Rotate 45° Rotate 90° Original

Article reference: Http://blog.csdn.net/ironyoung/article/details/41117039?utm_source=tuicool

Image rotation based on C + + and OpenCV underlying

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.