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个步骤:

  1. 应用高斯滤波来平滑图像,目的是去除噪声。

  2. 找寻图像的强度梯度(intensity gradients)。

  3. 应用非最大抑制(non-maximum suppression)技术来消除边误检(本来不是但检测出来是)。

  4. 应用双阈值的方法来决定可能的(潜在的)边界。

  5. 利用滞后技术来跟踪边界。

具体原理性质的东西可以参考这里

读取本地视频处理代码示例:

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,处理视频,输入输出,视频处理,OpenCV)