OpenCV Python rotate image by X degrees around specific point

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

提交回复
热议问题