python旋转裁剪图像

def rotate(image, angle, center = None, scale = 1.0):

    (h, w) = image.shape[:2]

    if center is None:
        center = (w / 2, h / 2)

    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    return rotated
    new_image = rotate(image, angle=4.5) 
import cv2
import numpy as np


def rotate(image, angle, center = None, scale = 1.0):

    (h, w) = image.shape[:2]

    if center is None:
        center = (w / 2, h / 2)

    M = cv2.getRotationMatrix2D(center, angle, scale)
    rotated = cv2.warpAffine(image, M, (w, h))

    return rotated


# image = rotate(image, angle= 5);#旋转图片规正

video_name = 'DJI_0092.mp4'
cap = cv2.VideoCapture(video_name)
count = 0
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.

fps = cap.get(cv2.CAP_PROP_FPS)
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
out = cv2.VideoWriter('PartParking_video2.avi', cv2.VideoWriter_fourcc(*'XVID'), fps, (1454, 482))#这里的1454 482必须跟之后的裁剪图片大小一致,不然会显示不出来

ret, image = cap.read()  # 从视频中读取一张张的图片

#
# cv2.imshow("img", image)
# cv2.imwrite("test.jpg", image)
#
# #按下按键之后才会结束
# cv2.waitKey()

while ret:

    # image = cv2.resize(image, (1920,1080))

    new_image = rotate(image, angle=4.5) # 旋转图片规正

    new_image = new_image[1233:1715, 777:2231]

    out.write(new_image)  # 保存一张张图片流,最终形成视频

    ret, image = cap.read()  # 从视频中读取一张张的图片

cv2.destroyAllWindows()
cap.release()

 

你可能感兴趣的:(python人工智障)