【Python】-005 OpenCV-Python生成视频

【Python】-005 OpenCV-Python生成视频

  OpenCV提供了python绑定,能够很方便的进行图像处理。

1、OpenCV-Python读取视频

  读取视频,显示,并将其中的一块区域写成新视频。

import numpy as np
import cv2 as cv
# open the video file
cap = cv.VideoCapture('d:/video/2.avi');

# get the first frame of this video
ret,frame = cap.read();

# get width and height of the video
imgHeight = frame.shape[0];
imgWidth = frame.shape[1];
print(imgWidth ,imgHeight)

# set fps and image size for the videowriter
fps = 30;
imgsize=(imgWidth,225);
fourcc = cv.VideoWriter_fourcc('M','J','P','G');

# open the video writer
videow = cv.VideoWriter('d:/video/1.avi',fourcc,fps,imgsize);

while(1):
    # take a frame from the video file
    ret ,frame = cap.read();
    # if read not success, break the loop and exit
    if ret != True:
        break;

    # show the frame read from the video
    cv.imshow('video',frame);

    # make a frame for the video writer
    newImg = frame[150:375,:];
    cv.imshow('newimg',newImg)
    print(newImg.shape[0],newImg.shape[1]);

    # write frame into file
    videow.write(newImg);

    # wait a time, if not, the window will always gray!
    cv.waitKey(60);
# close all windows opened by opencv
cv.destroyAllWindows();
# release the video file
cap.release();

# release the writer
videow.release();

2、注意

  创建videowriter时的图像大小必须与最后写入的图像大小一致,否则将导致写的视频无法播放。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值