【问题标题】:Rotate video 180 with OpenCV Python使用 OpenCV Python 旋转视频 180
【发布时间】:2021-08-03 00:45:10
【问题描述】:

我是 python 初学者,对于错误提前表示歉意。 我正在尝试将视频旋转 180 度,然后使用我应该创建的视频。

目前我正在这样做:

import cv2
import numpy as np
    
#that's my original video - the one that I want to rotate 180 degrees 

cap = cv2.VideoCapture('/Users/name/Desktop/VIDEO 12.05/PIC_P01.mp4')
    
    frame_number = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    
    # Original Frames
    frames = []
    for i in range(frame_number):
        ret, frame = cap.read()
        frames.append(frame)

    #here's where I try to rotate the video 
    new = cv2.rotate(frame, cv2.ROTATE_180)
    
    cv2.imshow('output', new)
     if cv2.waitKey(1) & 0xFF == ord('q'):
                break

    #here I'm trying to write the new rotated video
    newvideoR = cv2.VideoWriter('PIC_P01R.mp4',0x7634706d, 50, (360, 640))
    for jj in range(len(new)):
        newvideoR.write(new[jj])
    newvideoR.release()
    cap.release()

然而,最后,当我去读取文件时,我发现只有一个无法打开的空文件。 由此,我推断我所做的步骤存在错误。 有人对我应该如何做到这一点有任何建议吗?

** 我也尝试更改 cv2.Videowriter 上的参数(例如 fps、大小)但仍然无法正常工作

【问题讨论】:

  • 你能定义“仍然不工作”吗?你有输出吗?代码失败了吗?你怎么知道它不起作用?您可以发布错误消息吗?
  • 没有错误或警告消息。代码看起来不错,但最后我根本无法用任何程序打开视频(因为它是一个空视频)。

标签: python opencv video rotation video-capture


【解决方案1】:

您不需要累积帧,并将它们写入另一个循环。
您可以在同一个循环中读取、旋转和写入帧。

  • 获取宽度和高度:

     frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
     frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    
  • 在循环之前打开输出视频文件:

     newvideoR = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*"mp4v"), 50, (frame_width, frame_height))
    
  • 读取框架,旋转它并循环写入:

     for i in range(frame_number):
         ret, frame = cap.read()
    
         new = cv2.rotate(frame, cv2.ROTATE_180)
    
         cv2.imshow('output', new)
         if cv2.waitKey(1) & 0xFF == ord('q'):
             break
    
         newvideoR.write(new)
    
  • 发布视频阅读器和编写器:

     newvideoR.release()
     cap.release()
    

完整的代码示例(我将文件命名为input.mp4 和output.mp4):

import cv2
    
#that's my original video - the one that I want to rotate 180 degrees 
cap = cv2.VideoCapture('input.mp4')
    
frame_number = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

# Get width and height
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# here I'm trying to write the new rotated video
# Open the output video file before the loop, cv2.VideoWriter_fourcc(*"mp4v") = 0x7634706d
newvideoR = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*"mp4v"), 50, (frame_width, frame_height))
    
# Original Frames
#frames = []
for i in range(frame_number):
    ret, frame = cap.read()
    #frames.append(frame)  # No need to append the original frames

    #here's where I try to rotate the video 
    new = cv2.rotate(frame, cv2.ROTATE_180)
    
    cv2.imshow('output', new)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    newvideoR.write(new)

newvideoR.release()
cap.release()

【讨论】:

  • 效果很好!非常感谢您的清晰解释和示例。
猜你喜欢
  • 1970-01-01
  • 2012-05-20
  • 1970-01-01
  • 2017-02-02
  • 2013-02-09
  • 2011-11-19
  • 2011-07-08
  • 2011-04-25
  • 1970-01-01
相关资源
最近更新 更多