OpenCV Python rotate image by X degrees around specific point

前端 未结 9 1617
谎友^
谎友^ 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:16

    You can easily rotate the images using opencv python-

    def funcRotate(degree=0):
        degree = cv2.getTrackbarPos('degree','Frame')
        rotation_matrix = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)
        rotated_image = cv2.warpAffine(original, rotation_matrix, (width, height))
        cv2.imshow('Rotate', rotated_image)
    

    If you are thinking of creating a trackbar, then simply create a trackbar using cv2.createTrackbar() and the call the funcRotate()fucntion from your main script. Then you can easily rotate it to any degree you want. Full details about the implementation can be found here as well- Rotate images at any degree using Trackbars in opencv

    0 讨论(0)
  • 2020-11-27 05:20

    Or much easier use SciPy

    from scipy import ndimage
    
    #rotation angle in degree
    rotated = ndimage.rotate(image_to_rotate, 45)
    

    see here for more usage info.

    0 讨论(0)
  • 2020-11-27 05:21

    You can simply use the imutils package to do the rotation. it has two methods

    1. rotate: Rotate the image at specified angle. however the drawback is image might get cropped if it is not a square image.
    2. Rotate_bound: it overcomes the problem happened with rotate. It adjusts the size of the image accordingly while rotating the image.

    more info you can get on this blog: https://www.pyimagesearch.com/2017/01/02/rotate-images-correctly-with-opencv-and-python/

    0 讨论(0)
  • 2020-11-27 05:25

    Quick tweak to @alex-rodrigues answer... deals with shape including the number of channels.

    import cv2
    import numpy as np
    
    def rotateImage(image, angle):
        center=tuple(np.array(image.shape[0:2])/2)
        rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
        return cv2.warpAffine(image, rot_mat, image.shape[0:2],flags=cv2.INTER_LINEAR)
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 05:26

    The cv2.warpAffine function takes the shape parameter in reverse order: (col,row) which the answers above do not mention. Here is what worked for me:

    import numpy as np
    
    def rotateImage(image, angle):
        row,col = image.shape
        center=tuple(np.array([row,col])/2)
        rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
        new_image = cv2.warpAffine(image, rot_mat, (col,row))
        return new_image
    
    0 讨论(0)
提交回复
热议问题