How to use OpenCV to rotate images in Python

Source: Internet
Author: User
This article describes how to use OpenCV to rotate images in Python. the code and core algorithms are very simple, for more information, see OpenCV, the most widely used open-source Visual Library. It allows you to use a small amount of code to detect faces in images or videos.

Here are some internet tutorials to illustrate how to use affine transform in OpenCV) rotating images-they do not solve this problem when rotating the rectangle in an image. Generally, the corner of the rectangle is cut off, so the generated image needs to be modified. This is a flaw when a bit of code is used correctly.

def rotate_about_center(src, angle, scale=1.):  w = src.shape[1]  h = src.shape[0]  rangle = np.deg2rad(angle) # angle in radians  # now calculate new image width and height  nw = (abs(np.sin(rangle)*h) + abs(np.cos(rangle)*w))*scale  nh = (abs(np.cos(rangle)*h) + abs(np.sin(rangle)*w))*scale  # ask OpenCV for the rotation matrix  rot_mat = cv2.getRotationMatrix2D((nw*0.5, nh*0.5), angle, scale)  # calculate the move from the old center to the new center combined  # with the rotation  rot_move = np.dot(rot_mat, np.array([(nw-w)*0.5, (nh-h)*0.5,0]))  # the move only affects the translation, so update the translation  # part of the transform  rot_mat[0,2] += rot_move[0]  rot_mat[1,2] += rot_move[1]  return cv2.warpAffine(src, rot_mat, (int(math.ceil(nw)), int(math.ceil(nh))), flags=cv2.INTER_LANCZOS4)

From the center of the original image to the center of the target image, the affine transformation in rotation must be combined with the translation of the affine transformation. In A plane (2D), an affine transformation is a 2x2 matrix a and A translation vector a. It obtains the original point p = (x, y) to the target: Ap +. combined with two transformations of Ap + a and Bp + B, first A and then B, then B (Ap + a) is obtained) + B -- another affine transformation with matrix BA and vector Ba + B.


In this case, we are merging the rotation function with the translation function. The translation of similar transformations has the characteristics of 2x2 matrix I and motion vector m. Therefore, in the Ip + m representation, we want to first translate to the new center and rotate it after dinner. in this way, after applying Ip + m, we rotate Rp + r to generate Rp + Rm + r, which explains why we have to increase only two coefficients.

Note: Sadly, if numpy regards the input data as a vector rather than a matrix, it explains that a multiplication operator is not a matrix multiplication operator. Therefore, we must write np explicitly. dot.

Also note: We use the lansos interpolation, which is generally advantageous for expansion but very unfavorable for scale reduction. considering the application, we should adapt this interpolation.

The interaction with Python has improved a lot because of the cv2 module. However, because numpy coordinates are different from OpenCV, there are still some improvements that are inevitable. In addition, for some reason, OpenCV always treats each unit as a degree rather than a radian. In numpy, the coordinates in the image array are accessed in the order of [y, x], as shown in the following figure: increase vertically and then increase horizontally to the right. As far as OpenCV is concerned, the size is represented by (width, height), and the order is the opposite.

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.