视频中的帧在提取后出现倒置 [英] Frame from video is upside down after extracting

查看:32
本文介绍了视频中的帧在提取后出现倒置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里的问题是,当我使用OpenCV将视频提取到帧中时,有时我得到的帧会翻转,这在我的机器(Windows)和VM(Ubuntu)上都发生了,但我测试的一些视频,帧没有翻转。因此,我想知道我的代码中应该更改/添加什么因素或哪些内容,以便在不翻转的情况下修复提取

def extract_frame(video,folder):
   global fps

   os.mkdir('./green_frame/{folder}/'.format(folder=folder))
   vidcap = cv2.VideoCapture(video)
   success,image = vidcap.read()
   fps = vidcap.get(cv2.CAP_PROP_FPS)
   count = 0
   success = True
   while success:  #os.path.join(pathOut,(name+'.png'))
      cv2.imwrite(os.path.join('./green_frame/{folder}/'.format(folder=folder),"frame%d.png" % count), image)
      success,image = vidcap.read()
      print('Read a new frame: ', success) 
      count += 1

这是我从这段代码中得到的帧的示例。 我使用的原始视频是这样颠倒的:

所以,在我的例子中,我必须改变什么,才能使它不像我的第一张照片那样翻转。这与视频的分辨率或帧速率有关吗?我用1280x720分辨率的视频进行了测试,提取的所有帧都被颠倒了,但568x320的视频帧是正常的

谢谢

编辑: 所以,我查看了视频的信息,发现在元数据中,提取到倒置帧的视频旋转了180 但当我检查一个正常的视频,它产生了一个非倒置的帧,它没有旋转:180

那么,我该如何处理旋转的视频?

推荐答案

对于还在研究这个问题的人来说,我只是被同样的问题卡住了。事实证明,一些Android手机和iPhone会在横向拍摄图像/帧,并根据EXIF的‘Rotate’标签进行动态转换,以显示图像/帧。

OpenCV中奇怪的设计选择是cv2.imread(img_file)已经通过读取图像的rotate标记以正确的方向读取图像,但cv2.VideoStreamread()方法不执行此操作。

因此,为了解决这个问题,我使用ffmpeg读取‘Rotate’标签并将视频帧旋转到正确的方向。(非常感谢上面的评论,为我指明了正确的方向👍)

代码如下:

  • 确保您有ffmpegfor python。(pip install ffmpeg-python)

  • 创建方法检查Video_file是否需要旋转:

     import ffmpeg    
    
     def check_rotation(path_video_file):
         # this returns meta-data of the video file in form of a dictionary
         meta_dict = ffmpeg.probe(path_video_file)
    
         # from the dictionary, meta_dict['streams'][0]['tags']['rotate'] is the key
         # we are looking for
         rotateCode = None
         if int(meta_dict['streams'][0]['tags']['rotate']) == 90:
             rotateCode = cv2.ROTATE_90_CLOCKWISE
         elif int(meta_dict['streams'][0]['tags']['rotate']) == 180:
             rotateCode = cv2.ROTATE_180
         elif int(meta_dict['streams'][0]['tags']['rotate']) == 270:
             rotateCode = cv2.ROTATE_90_COUNTERCLOCKWISE
    
         return rotateCode
    
  • 创建方法更正视频文件中帧的旋转:

     def correct_rotation(frame, rotateCode):  
         return cv2.rotate(frame, rotateCode) 
    
  • 最后,在您的主循环中执行以下操作:

     # open a pointer to the video file stream
     vs = cv2.VideoCapture(video_path)
    
     # check if video requires rotation
     rotateCode = check_rotation(video_path)
    
     # loop over frames from the video file stream
     while True:
         # grab the frame from the file
         grabbed, frame = vs.read()
    
         # if frame not grabbed -> end of the video
         if not grabbed:
             break
    
         # check if the frame needs to be rotated
         if rotateCode is not None:
             frame = correct_rotation(frame, rotateCode)
    
         # now your logic can start from here
    

希望这对🍻有帮助

这篇关于视频中的帧在提取后出现倒置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆