Python OpenCV a way to rotate an image without cropping the picture

Source: Internet
Author: User
Tags abs cos sin

Recently, we need to use the image processing related operation in the depth study, in the Niang find the picture rotation method is stereotyped, rotate the picture is not the original size, very distressed, so Google to the site of crooked nuts push lightly a method, close to test and use, again to reject the world article a big copy of the phenomenon, Although I also copy crooked nuts. No more nonsense to say, directly affixed to the code.

def rotate_bound (image, Angle):
    # Grab the dimensions of the image and then determine the
    # Center
    (h, w) = IM Age.shape[:2]
    (CX, CY) = (w//2, H//2)

    # Grab the rotation matrix (applying the negative of the
    # angle to Rotate clockwise), then grab the sine and cosine
    # (i.e., the rotation components of the matrix)
    M = Cv2.getrotati Onmatrix2d (CX, CY),-angle, 1.0)
    cos = np.abs (m[0, 0))
    sin = Np.abs (m[0, 1])

    # Compute the new bounding dime Nsions of the image
    NW = Int ((h * sin) + (w * cos))
    NH = Int ((h * cos) + (w * sin))

    # Adjust the rotation m Atrix to take in account translation
    m[0, 2] + = (NW/2)-CX
    m[1, 2] + = (NH/2)-CY

    # Perform the ACTU Al Rotation and return the image return
    cv2.warpaffine (image, M, (NW, NH))

The other is not to say it, the first parameter wears OPENCV read the image, the second parameter passes the angle that needs to rotate, enjoy!

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.