OpenCV Python rotate image by X degrees around specific point

前端 未结 9 1664
谎友^
谎友^ 2020-11-27 05:08

I\'m having a hard time finding examples for rotating an image around a specific point by a specific (often very small) angle in Python using OpenCV.

This is what I

9条回答
  •  孤城傲影
    2020-11-27 05:26

    import numpy as np
    import cv2
    
    def rotate_image(image, angle):
      image_center = tuple(np.array(image.shape[1::-1]) / 2)
      rot_mat = cv2.getRotationMatrix2D(image_center, angle, 1.0)
      result = cv2.warpAffine(image, rot_mat, image.shape[1::-1], flags=cv2.INTER_LINEAR)
      return result
    

    Assuming you're using the cv2 version, that code finds the center of the image you want to rotate, calculates the transformation matrix and applies to the image.

提交回复
热议问题