resize视频帧尺寸,根据短边按比例缩放

用视频做检测需要把每一帧resize成检测器需要的大小。这里是把4196×2160(w×h)大小的视频缩放成1024×1024。为了不引起形状变化,按比例resize成1941×1024,之后裁剪。

添加链接描述

import cv2

def img_resize(image):
    height, width = image.shape[0], image.shape[1]#  2160,4196
    width_new = 1024
    height_new = 1024
    # 2160/1024 < 4196/1024
    if width / height <= width_new / height_new:
        img_new = cv2.resize(image, (width_new, int(height * width_new / width)))
    else:# w/w_new >= h/h_new, 按照h比例缩放,w_current = w * (h_new/h)  
        img_new = cv2.resize(image, (int(width * height_new / height), height_new))
    return img_new

videowriter = cv2.VideoWriter("/home/chenlc/am/demo/DJI_0008_resize"+".avi", cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 50, (1941,1024))  # 50 帧率。“.mov”会报错,改成‘.avi’了。
cap = cv2.VideoCapture("/home/chenlc/am/demo/DJI_0008.MP4")
success, _ = cap.read()
i = 0
while success:
    success, img1 = cap.read()
    if i%50 ==0:
      print(i/50)//输出写了多少帧了
    i=i+1
    try:
         img = img_resize(img1)
         videowriter.write(img)
    except:
         break

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值