如何使用宽高比从视频调整帧的大小 [英] How to resize frame's from video with aspect ratio

查看:91
本文介绍了如何使用宽高比从视频调整帧的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 2.7,OpenCV.我已经写了这段代码.

I am using Python 2.7, OpenCV. I have written this code.

import cv2
vidcap = cv2.VideoCapture('myvid2.mp4')
success,image = vidcap.read()
count = 0;
print "I am in success"
while success:
  success,image = vidcap.read()
  resize = cv2.resize(image, (640, 480)) 
  cv2.imwrite("%03d.jpg" % count, resize)     
  if cv2.waitKey(10) == 27:                     
      break
  count += 1

我正在处理视频,并将视频分为.jpg图像的各个帧.同时,我还将帧的尺寸调整为640x480.帧的顺序也被保留.该代码的唯一问题是它不保存以前的图像比率.

I am working with video and am dividing the video into individual frames, as a .jpg images. I am also at the same time resizing the frames to dimension 640x480. The order of the frames is also being preserved. The only issue with the code is that it does not save the previous image-ratio.

例如,外观如何,请从1920x1080调整尺寸:

For example how it look's like, resize from 1920x1080:

如您所见,比率存在问题.1920x1080 16:9,但640:480 4:3

There is a problem in ratio, as you can see. 1920x1080 16:9, but 640:480 4:3

我理想的情况是:

感谢您抽出宝贵的时间阅读问题.如果您能帮助我解决这个问题,我将非常高兴〜祝你有美好的一天,我的朋友.

Thank you for your taking the time for reading the question. I will be very glad if you can help me solve this issue~ Have a good day, my friend.

推荐答案

您可以使用值将原始帧的高度和宽度除以一个值,并将其作为参数,而不是使用硬编码的值640和480,例如:

Instead of using hard-coded values 640 and 480, you can divide the original frame height and width by a value and supply that as an argument, like so:

import cv2

vidcap = cv2.VideoCapture("/path/to/video")
success, image = vidcap.read()
count = 0

while success:
    height, width, layers = image.shape
    new_h = height / 2
    new_w = width / 2
    resize = cv2.resize(image, (new_w, new_h))
    cv2.imwrite("%03d.jpg" % count, resize)
    success, image = vidcap.read()
    count += 1

这篇关于如何使用宽高比从视频调整帧的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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