Python 如何从具有纵横比的视频中调整帧的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43315349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to resize frame's from video with aspect ratio
提问by GGzet
I am using Python 2.7, OpenCV. I have written this code.
我正在使用 Python 2.7,OpenCV。我写了这段代码。
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
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.
我正在处理视频并将视频分成单独的帧,作为 .jpg 图像。同时,我还将帧的大小调整为 640x480。帧的顺序也被保留。代码的唯一问题是它不保存以前的图像比例。
For example how it look's like, resize from 1920x1080:
There is a problem in ratio, as you can see. 1920x1080 16:9, but 640:480 4:3
如您所见,比率存在问题。1920x1080 16:9,但 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.
感谢您抽出时间阅读问题。如果你能帮我解决这个问题,我会很高兴的~祝你有美好的一天,我的朋友。
回答by Saransh Kejriwal
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:
除了使用硬编码值 640 和 480,您可以将原始帧的高度和宽度除以一个值并将其作为参数提供,如下所示:
while success:
success,image = vidcap.read()
height , width , layers = img.shape
new_h=height/2
new_w=width/2
resize = cv2.resize(image, (new_w, new_h))
cv2.imwrite("%03d.jpg" % count, resize)
回答by Jobin Mathew
Please try this.. It should give you the expected output.
请试试这个..它应该给你预期的输出。
def resize_image(image, width, height,COLOUR=[0,0,0]):
h, w, layers = image.shape
if h > height:
ratio = height/h
image = cv2.resize(image,(int(image.shape[1]*ratio),int(image.shape[0]*ratio)))
h, w, layers = image.shape
if w > width:
ratio = width/w
image = cv2.resize(image,(int(image.shape[1]*ratio),int(image.shape[0]*ratio)))
h, w, layers = image.shape
if h < height and w < width:
hless = height/h
wless = width/w
if(hless < wless):
image = cv2.resize(image, (int(image.shape[1] * hless), int(image.shape[0] * hless)))
else:
image = cv2.resize(image, (int(image.shape[1] * wless), int(image.shape[0] * wless)))
h, w, layers = image.shape
if h < height:
df = height - h
df /= 2
image = cv2.copyMakeBorder(image, int(df), int(df), 0, 0, cv2.BORDER_CONSTANT, value=COLOUR)
if w < width:
df = width - w
df /= 2
image = cv2.copyMakeBorder(image, 0, 0, int(df), int(df), cv2.BORDER_CONSTANT, value=COLOUR)
image = cv2.resize(image,(1280,720),interpolation=cv2.INTER_AREA)
return image