視訊的處理和圖片的處理類似,只不過視訊處理需要連續處理一系列圖片。
一般有兩種視訊源,一種是直接從硬碟載入視訊,另一種是獲取攝像頭視訊。
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(0) while(True): # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows() |
0x02. 寫入視訊
攝像頭錄製視訊
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import cv2.cv as cv capture=cv.CaptureFromCAM(0) temp=cv.QueryFrame(capture) writer=cv.CreateVideoWriter("output.avi", cv.CV_FOURCC("D", "I", "B", " "), 5, cv.GetSize(temp), 1) #On linux I used to take "M","J","P","G" as fourcc count=0 while count<50: print count image=cv.QueryFrame(capture) cv.WriteFrame(writer, image) cv.ShowImage('Image_Window',image) cv.WaitKey(1) count+=1 |
從檔案中讀取視訊並儲存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import cv2.cv as cv capture = cv.CaptureFromFile('img/mic.avi') nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)) width = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)) height = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)) fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS) codec = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FOURCC) wait = int(1/fps * 1000/1) #Compute the time to wait between each frame query duration = (nbFrames * fps) / 1000 #Compute duration print 'Num. Frames = ', nbFrames print 'Frame Rate = ', fps, 'fps' writer=cv.CreateVideoWriter("img/new.avi", int(codec), int(fps), (width,height), 1) #Create writer with same parameters cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES,80) #Set the number of frames for f in xrange( nbFrames - 80 ): #Just recorded the 80 first frames of the video frame = cv.QueryFrame(capture) print cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_POS_FRAMES) cv.WriteFrame(writer, frame) cv.WaitKey(wait) |
cv2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import numpy as np import cv2 cap = cv2.VideoCapture(0) # Define the codec and create VideoWriter object fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) while(cap.isOpened()): ret, frame = cap.read() if ret==True: frame = cv2.flip(frame,0) # write the flipped frame out.write(frame) cv2.imshow('frame',frame) if cv2.waitKey(1) & 0xFF == ord('q'): break else: break # Release everything if job is finished cap.release() out.release() cv2.destroyAllWindows() |