Ask Your Question
1

How to resize frame's from video with aspect ratio

asked 2017-04-10 19:53:48 -0600

Gagazet gravatar image

At the head of question main idea. I am using Python 2.7, openCV for it. Finally, i have 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), interpolation = cv2.INTER_LINEAR) 
  cv2.imwrite("%03d.jpg" % count, resize)     
  if cv2.waitKey(10) == 27:                     
      break
  count += 1

We have video and cutting it on each frames, as a .jpg images. In the same time we making resize from any size to the 640x480. And all images has correct order of reading. There is no any problems with code and it work's well, but!...it doesn't save prev. image ratio. And this is big problem, because i am need in it.

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

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

And how it should be in my dream: image description

At first, thank you for your reading of it. I will be very glad if you can help to me solve this issue of my code~ Have a good day, my friend.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-11 00:09:25 -0600

berak gravatar image

easy !

ifyou look at resize():

>>> help(cv2.resize)
>>> resize(...)
    resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst

you see, there's an fx and fy parameter. use those instead of the fixed dsize argument, to preserve your original ratio:

 # half size:
 resized = cv2.resize(image, fx=0.5, fy=0.5, interpolation = cv2.INTER_LINEAR)
edit flag offensive delete link more

Comments

1

Thank you. I spend around 4 hours, but found another way how to do it with correct borders! Anyway thank you. And i am not sure, that you can resize by your way from 1080 to the 480 properly~

Gagazet gravatar imageGagazet ( 2017-04-11 00:12:55 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-10 19:53:48 -0600

Seen: 30,408 times

Last updated: Apr 11 '17