视频的处理和图片的处理类似,只不过视频处理需要连续处理一系列图片。

一般有两种视频源,一种是直接从硬盘加载视频,另一种是获取摄像头视频。

0x00. 本地读取视频

核心函数:

cv.CaptureFromFile()

代码示例:

123456789101112131415161718192021222324 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 = \’, nbFramesprint \’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

12345678910111213141516 import numpy as npimport 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()

示例代码:

12345678910 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

1234567891011121314151617181920 import numpy as npimport 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;\”>

123456789101112131415161718192021222324 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 = \’, nbFramesprint \’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

12345678910111213141516 import numpy as npimport 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()

示例代码:

12345678910 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

1234567891011121314151617181920 import numpy as npimport cv2 cap =