1. 程式人生 > >python-opencv旋轉影象,保持影象不被裁減

python-opencv旋轉影象,保持影象不被裁減

python-opencv旋轉影象,保持影象不被裁減

 
import cv2
from math import *

def remote(img,degree):
    #degree左轉
    img = cv2.imread(img)
    height, width = img.shape[:2]
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

    matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

    matRotation[0, 2] += (widthNew - width) / 2
    matRotation[1, 2] += (heightNew - height) / 2
    imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

    cv2.imshow("img", img)
    cv2.imshow("imgRotation", imgRotation)
    cv2.waitKey(0)
remote('1.jpg',90)