您的位置:首页 > 编程语言 > Python开发

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

2018-08-24 14:09 302 查看

【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
时的图像大小必须与最后写入的图像大小一致,否则将导致写的视频无法播放。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: