OpenCV Python rotate image by X degrees around specific point

前端 未结 9 1618
谎友^
谎友^ 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:34
    def rotate(image, angle, center = None, scale = 1.0):
        (h, w) = image.shape[:2]
    
        if center is None:
            center = (w / 2, h / 2)
    
        # Perform the rotation
        M = cv2.getRotationMatrix2D(center, angle, scale)
        rotated = cv2.warpAffine(image, M, (w, h))
    
        return rotated
    
    0 讨论(0)
  • 2020-11-27 05:35
    import imutils
    
    vs = VideoStream(src=0).start()
    ...
    
    while (1):
       frame = vs.read()
       ...
    
       frame = imutils.rotate(frame, 45)
    

    More: https://github.com/jrosebr1/imutils

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

    I had issues with some of the above solutions, with getting the correct "bounding_box" or new size of the image. Therefore here is my version

    def rotation(image, angleInDegrees):
        h, w = image.shape[:2]
        img_c = (w / 2, h / 2)
    
        rot = cv2.getRotationMatrix2D(img_c, angleInDegrees, 1)
    
        rad = math.radians(angleInDegrees)
        sin = math.sin(rad)
        cos = math.cos(rad)
        b_w = int((h * abs(sin)) + (w * abs(cos)))
        b_h = int((h * abs(cos)) + (w * abs(sin)))
    
        rot[0, 2] += ((b_w / 2) - img_c[0])
        rot[1, 2] += ((b_h / 2) - img_c[1])
    
        outImg = cv2.warpAffine(image, rot, (b_w, b_h), flags=cv2.INTER_LINEAR)
        return outImg
    
    0 讨论(0)
提交回复
热议问题