1. CaptureFromCAM函数对图像进行提取: capture = cv.CaptureFromCAM(0) 读取直接的视频文件只需将语句改变为: capture = cv.VideoCapture('videoname.avi')

  2. 对每一帧图像进行读取: while True:

    img = cv.QueryFrame(capture)

    #如果按下 esc 键则终止程序退出 if cv.WaitKey(10) == 27:

    break

  3. 在循环中对读取的每一帧图像进行二值化处理:

    def binaryThreshold(Image, threshold):
    grey = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U, 1) out = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U, 1)

    cv.CvtColor(Image,grey,cv.CV_BGR2GRAY)
    cv.Threshold(grey, out , threshold , 255 ,cv.CV_THRESH_BINARY)

    return out
    其中,CreateImage 函数表示按原帧大小创建 256 1 通道的灰度图像, CvtColor 函数将传入的图像从 RGB 图像转换成灰度图,写入 grey 图像。 Threshold函数按程序开始时threshold = input("threshold=")语句获取的用 户输入的的阈值将图像进行二值化写入 out 图像。将 out 图像作为函数返回 值传入主函数。

  4. 在每一帧图像上覆盖“3100102592 menglixia”的文字:
    (width, height) = cv.GetSize(img)
    text_font = cv.InitFont(cv.CV_FONT_HERSHEY_DUPLEX, 2, 2) cv.PutText(img, "3100102592 menglixia", (50, height / 2), text_font, 
    cv.RGB(255, 0, 0))

获取图像高度和宽度,并初始化文字字体,在 1/2 高度处写入“3100102592 menglixia”的字符串,以颜色红色(在二值化后的图像中显示为 黑色)

       5. 显示每一帧图像:

            cv.ShowImage("camera",img) 

#coding=utf-8
import cv2.cv as cv

def binaryThreshold(Image, threshold):
    grey = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U, 1)
    out = cv.CreateImage(cv.GetSize(img),cv.IPL_DEPTH_8U, 1)
 
    cv.CvtColor(Image,grey,cv.CV_BGR2GRAY)
    cv.Threshold(grey, out ,threshold , 255 ,cv.CV_THRESH_BINARY)

    return out

if __name__ == '__main__':

    #threshold = input("threshold=")
    cv.NamedWindow("camera",1)
    capture = cv.CaptureFromCAM(0)  

    
    while True:
        """ capture image from camera """
        img = cv.QueryFrame(capture)

        """ convert color image to grey """
        #img = binaryThreshold(img, threshold)

        """ Get the width and height of the image """
        (width, height) = cv.GetSize(img)

        """ put text id and name in image """        
        text_font = cv.InitFont(cv.CV_FONT_HERSHEY_DUPLEX, 2, 2)
        cv.PutText(img, "3100102592 menglixia", (50, height / 2), text_font, cv.RGB(255, 255, 0))

        """ show each frame """
        cv.ShowImage("camera",img)

        """ press esc to quit the script """
        if cv.WaitKey(10) == 27:
            break

    del(capture)
    cv.DestroyWindow("camera")