视频的处理和图片的处理类似,只不过视频处理需要连续处理一系列图片。
一般有两种视频源,一种是直接从硬盘加载视频,另一种是获取摄像头视频。
0x00. 本地读取视频
核心函数:
cv.CaptureFromFile()
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import cv2.cv as cv capture = cv.CaptureFromFile('myvideo.avi') nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)) #CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream #CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS) wait = int(1/fps * 1000/1) duration = (nbFrames * fps) / 1000 print 'Num. Frames = ', nbFrames print 'Frame Rate = ', fps, 'fps' print 'Duration = ', duration, 'sec' for f in xrange( nbFrames ): frameImg = cv.QueryFrame(capture) print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES) cv.ShowImage("The Video", frameImg) cv.WaitKey(wait) |
cv2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import numpy as np import cv2 cap = cv2.VideoCapture('vtest.avi') while(cap.isOpened()): ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() |
0x01. 摄像头视频读取
核心函数:
cv.CaptureFromCAM()
示例代码:
1 2 3 4 5 6 7 8 9 10 |
import cv2.cv as cv capture = cv.CaptureFromCAM(0) while True: frame = cv.QueryFrame(capture) cv.ShowImage("Webcam", frame) c = cv.WaitKey(1) if c == 27: #Esc on Windows break |
cv2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import numpy as np import cv2 cap = cv2.VideoCapture/span>cv2 cap = cv2.VideoCapture="crayon-5812a37a11732965027020" class="crayon-syntax crayon-theme-github crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-always" style=" margin-top: 12px; margin-bottom: 12px; font-size: 13px !important; line-height: 15px !important;">
cv2
0x01. 摄像头视频读取核心函数:
示例代码:
cv2
|