opencv顺时针,逆时针旋转视频并保存视频

原视频

在这里插入图片描述

代码

import cv2

# 打开视频文件
video = cv2.VideoCapture('inference/video/lianzhang.mp4')

# 获取原视频的宽度和高度
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))

# 创建视频编写器并设置输出视频参数
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output = cv2.VideoWriter('inference/video/output.mp4', fourcc, 30.0, (height, width))

while video.isOpened():
    ret, frame = video.read()

    if not ret:
        break

    # 对每一帧图像进行逆时针旋转90度,正时针是cv2.ROTATE_90_CLOCKWISE
    rotated_frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)

    # 写入旋转后的帧到输出视频文件
    output.write(rotated_frame)

    cv2.imshow('Rotated Video', rotated_frame)

    # 按下 'q' 键退出循环
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

# 释放资源
video.release()
output.release()
cv2.destroyAllWindows()

旋转后

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Python中的OpenCV库实现这些操作。下面是一个简单的代码示例,可以批量对图片进行旋转和亮度调整: ```python import cv2 import os # 顺时针旋转图像 def rotate_clockwise(img, angle): rows, cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1) dst = cv2.warpAffine(img,M,(cols,rows)) return dst # 逆时针旋转图像 def rotate_counterclockwise(img, angle): return rotate_clockwise(img, -angle) # 线性增强亮度 def increase_brightness(img, value): hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) h, s, v = cv2.split(hsv) v = cv2.add(v, value) v[v > 255] = 255 final_hsv = cv2.merge((h, s, v)) return cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR) # 线性降低亮度 def decrease_brightness(img, value): return increase_brightness(img, -value) # 批量处理图片 def process_images(input_folder, output_folder): # 创建输出文件夹 if not os.path.exists(output_folder): os.makedirs(output_folder) # 遍历输入文件夹中的所有图片 for filename in os.listdir(input_folder): if filename.endswith(".jpg") or filename.endswith(".png"): # 读取图片 img = cv2.imread(os.path.join(input_folder, filename)) # 顺时针旋转10度并保存 rotated = rotate_clockwise(img, 10) cv2.imwrite(os.path.join(output_folder, "rotated_clockwise_" + filename), rotated) # 逆时针旋转10度并保存 rotated = rotate_counterclockwise(img, 10) cv2.imwrite(os.path.join(output_folder, "rotated_counterclockwise_" + filename), rotated) # 线性增强亮度10灰度并保存 brightened = increase_brightness(img, 10) cv2.imwrite(os.path.join(output_folder, "brightened_" + filename), brightened) # 线性降低亮度10灰度并保存 darkened = decrease_brightness(img, 10) cv2.imwrite(os.path.join(output_folder, "darkened_" + filename), darkened) # 测试 process_images("input_folder", "output_folder") ``` 在上面的代码中,rotate_clockwise() 和 rotate_counterclockwise() 函数用于旋转图像,increase_brightness() 和 decrease_brightness() 函数用于调整亮度。process_images() 函数遍历输入文件夹中的所有图片,并对它们进行旋转和亮度调整,然后将处理后的结果保存到输出文件夹中。 注意,这只是一个简单的示例代码,实际应用中需要根据具体情况进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

computer_vision_chen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值