OpenCV——实现视频图像的旋转

# Rotate

import cv2

def rotate_img(image,angle):
    # Reading the image

    # dividing height and width by 2 to get the center of the image
    height, width = image.shape[:2]
    # get the center coordinates of the image to create the 2D rotation matrix
    center = (width/2, height/2)

    # using cv2.getRotationMatrix2D() to get the rotation matrix
    rotate_matrix = cv2.getRotationMatrix2D(center=center, angle=angle, scale=1)

    # rotate the image using cv2.warpAffine
    rotated_image = cv2.warpAffine(src=image, M=rotate_matrix, dsize=(width, height))

    return rotated_image


# 视频路径和输出路径
input_video_path = r'D:\desk\2.mp4'
output_video_path = r'D:\desk\3.mp4'

# 打开视频文件
cap = cv2.VideoCapture(input_video_path)

# 获取视频的帧率和帧大小
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)

# 定义输出视频的编码和创建VideoWriter对象
fourcc = cv2.VideoWriter_fourcc(*'XVID')  # 定义编码器和文件格式,XVID是DivX兼容的MPEG-4编码器
out = cv2.VideoWriter(output_video_path, fourcc, fps, (frame_width, frame_height), True)

angle = 0
frame_count = 0

while cap.isOpened():

    frame_count +=1
    ret, frame = cap.read()
    if not ret:
        break

    # 增加亮度
    # 确保像素值在0-255范围内
    # enhanced_frame = cv2.add(frame, brightness_factor)

    if frame_count % 1 == 0:
        angle += 2.4

    frame = rotate_img(frame,angle = angle)
    # 写入处理后的帧到输出视频
    out.write(frame)

    # 按 'q' 退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放资源
cap.release()
out.release()
cv2.destroyAllWindows()

可以通过frame_count去调整多少帧旋转多少angle。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张飞飞飞飞飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值