python OpenCV学习之【视频裁剪】

今天学习了如何用opencv截取视频的ROI,记录一下,直接贴上代码

import cv2

#创建显示窗口
cv2.namedWindow('Video')

# 导入视频文件,参数:0 自带摄像头,1 USB摄像头,为文件名时读取视频文件
video_capture = cv2.VideoCapture(0)

# 获取读入视频的参数
fps = video_capture.get(cv2.CAP_PROP_FPS)
width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
print('fps:',fps)
print('width:', width)
print('height:', height)

# 定义截取尺寸,后面定义的每帧的h和w要于此一致,否则视频无法播放
size = (int(600), int(100)) # (height, width)

# 创建视频写入对象
video_writer = cv2.VideoWriter("test.avi",
								cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'),
								fps,
								size)

#读取视频帧,然后写入文件并在窗口显示
success, frame = video_capture.read()
while success and not cv2.waitKey(1) == 27:
	# 定义ROI的位置
	frame = frame[0:100, 0:600] # [width, height] 要与上面定义的size参数一致,注意参数的位置
	video_writer.write(frame) # 写入视频文件
	cv2.imshow("video", frame)
	success, frame = video_capture.read()

#回收资源
cv2.destroyWindow("video")
video_capture.release()

 

你可能感兴趣的:(OpenCV学习)