Python-OpenCV 處理視訊(一)(二): 輸入輸出 視訊處理
視訊的處理和圖片的處理類似,只不過視訊處理需要連續處理一系列圖片。
一般有兩種視訊源,一種是直接從硬碟載入視訊,另一種是獲取攝像頭視訊。
0x00. 本地讀取視訊
核心函式:
cv.CaptureFromFile()
程式碼示例:
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
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()
示例程式碼:
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
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. 寫入視訊
攝像頭錄製視訊
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
從檔案中讀取視訊並儲存
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
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()
————————————————————————————————————喵星人說這是分割線————————————————————————————————————
0x00. 使用 Canny 演算法邊緣識別
Canny 演算法是一種多級邊緣識別演算法。
Canny邊緣識別演算法可以分為以下5個步驟:
應用高斯濾波來平滑影象,目的是去除噪聲。
找尋影象的強度梯度(intensity gradients)。
應用非最大抑制(non-maximum suppression)技術來消除邊誤檢(本來不是但檢測出來是)。
應用雙閾值的方法來決定可能的(潛在的)邊界。
利用滯後技術來跟蹤邊界。
具體原理性質的東西可以參考這裡
讀取本地視訊處理程式碼示例:
import cv2.cv as cv
capture = cv.CaptureFromFile('img/myvideo.avi')
nbFrames = int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT))
fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)
wait = int(1/fps * 1000/1)
dst = cv.CreateImage((int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))), 8, 1)
for f in xrange( nbFrames ):
frame = cv.QueryFrame(capture)
cv.CvtColor(frame, dst, cv.CV_BGR2GRAY)
cv.Canny(dst, dst, 125, 350)
cv.Threshold(dst, dst, 128, 255, cv.CV_THRESH_BINARY_INV)
cv.ShowImage("The Video", frame)
cv.ShowImage("The Dst", dst)
cv.WaitKey(wait)
直接處理攝像頭視訊:
import cv2.cv as cv
capture = cv.CaptureFromCAM(0)
dst = cv.CreateImage((int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT))), 8, 1)
while True:
frame = cv.QueryFrame(capture)
cv.CvtColor(frame, dst, cv.CV_BGR2GRAY)
cv.Canny(dst, dst, 125, 350)
cv.Threshold(dst, dst, 128, 255, cv.CV_THRESH_BINARY_INV)
cv.ShowImage("The Video", frame)
cv.ShowImage("The Dst", dst)
c = cv.WaitKey(1)
if c == 27: #Esc on Windows
break
0x01. 人臉識別
使用OpenCV可以很簡單的檢測出視訊中的人臉等:
import cv2.cv as cv
capture=cv.CaptureFromCAM(0)
hc = cv.Load("haarcascades/haarcascade_frontalface_alt.xml")
while True:
frame=cv.QueryFrame(capture)
faces = cv.HaarDetectObjects(frame, hc, cv.CreateMemStorage(), 1.2,2, cv.CV_HAAR_DO_CANNY_PRUNING, (0,0) )
for ((x,y,w,h),stub) in faces:
cv.Rectangle(frame,(int(x),int(y)),(int(x)+w,int(y)+h),(0,255,0),2,0)
cv.ShowImage("Window",frame)
c=cv.WaitKey(1)
if c==27 or c == 1048603: #If Esc entered
break
from: https://segmentfault.com/a/1190000003804797
https://segmentfault.com/a/1190000003804807
相關文章
- Python-OpenCV 處理視訊(一): 輸入輸出PythonOpenCV
- Python-OpenCV 處理視訊(二): 視訊處理PythonOpenCV
- android音視訊指南-處理音訊輸出的變化Android音訊
- 處理鍵盤輸入訊息(轉)
- Python-OpenCV 處理視訊(四): 運動檢測PythonOpenCV
- 檔案輸入輸出處理(二)-位元組流
- Python-OpenCV 處理視訊(三): 標記運動軌跡PythonOpenCV
- Python-OpenCV 處理視訊(五): 運動方向判斷PythonOpenCV
- 視訊處理之OSD
- 前端視角看視訊處理前端
- opencv 視訊處理相關OpenCV
- 多功能視訊處理方案
- 使用FFmpeg處理音視訊
- 用Python處理視訊Python
- pat處理輸入輸出小技巧(待更新)
- php實現ffmpeg處理視訊PHP
- 【視訊處理】YUV格式說明
- Android音視訊處理之MediaMuxerAndroidUX
- GoPro Quik電腦版,視訊處理GoUI
- iOS學習:AVFoundation 視訊流處理iOS
- 【視訊處理】YV12ToARGB
- Python-OpenCV 處理影象(八):影象二值化處理PythonOpenCV
- 安卓微信視訊播放全屏問題處理安卓
- Android音視訊處理之MediaCodecAndroid
- Movavi Video Editor Plus 22,視訊處理IDE
- 短視訊影象處理 OpenGL ES 實踐
- iOS AVPlayer播放視訊的留白處理iOS
- OpenCV成長之路(10):視訊的處理OpenCV
- 音訊錄製及視覺化處理音訊視覺化
- 處理stdin輸入的字串指令字串
- [譯] 用 Flask 輸出視訊流Flask
- laravel 使用PHP-FFMpeg處理視訊檔案LaravelPHP
- PHP視訊處理器安裝環境搭建PHP
- VideoProc Converter 全能視訊處理軟體IDE
- Movie Studio 2022,視訊處理
- 唱吧 iOS 音視訊快取處理框架iOS快取框架
- 【視訊處理】YUV與RGB格式轉換
- 處理若干行輸出的題目