How to resize frame's from video with aspect ratio

六眼飞鱼酱① 提交于 2021-02-07 06:55:52

问题


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

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.

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

How I ideally want it to be:

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.


回答1:


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



回答2:


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



回答3:


Wanted to add onto what Saransh mentioned about dividing. Dividing works great, but I believe the resize function expects that the new dimensions be an int object, so make sure you use the int(x) or round(x, 0) function to do so.



来源:https://stackoverflow.com/questions/43315349/how-to-resize-frames-from-video-with-aspect-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!