• Open Source Computer Vision Library

Python&OpenCV

Wikipedia,自由的百科全书

目前OpenCV支持的Python版本为2.4版.所以我们这里使用2.4为范例. 但是如果你用2.5也能做到请帮忙修改一下这个页面,让大家知道2.5也可以,并且说明需要怎么设置和使用,存在什么问题.

目录

安装Python

Python是一种面向对象、解释性的计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定。这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所有的操作系统中运行。目前,基于这种语言的相关技术正在飞速的发展,用户数量急剧扩大,相关的资源非常多。

Windows用户

Python的安装,可以直接去 http://www.python.org 下载最新版本2.5.1。

opencv4python2.5下载地址:http://www.interactive-china.net/viewthread.php?tid=235&extra=page%3D1

安装完毕之后,请把opencv4python2.5压缩包解压,然后放到你的Python安装目录的所在地。假如Python安装在D:\Program Files\Python\,那么将解压后的文件放到 D:\Program Files\Python\Lib\site-packages\,然后在D:\Program Files\Python\Lib\site-packages\ 里新建一个文档,保存为Opencv.pth,里面的内容写入OpenCV。这样就完成了基本的设置,可以开始在python下调用OpenCV来写东西了。

有一个完整的xp+python2.5的安装流程在http://www.instructables.com/id/Using-openCV-1.0-with-python-2.5-in-Windows-XP/

上面的链接对没有vs2003编译器的xp来说很有帮助,因为你可以注册后下载编译好的python module lib。


Linux用户

Linux用户就好办了,系统默认就有,只是可能版本是2.5的. 用python --version看一下

OpenCV Python的范例

#! /usr/bin/env python
 
import sys
 
# import the necessary things for OpenCV
#原先范例的格式是下面的两行,也就是from opencv.cv import*
#这里需要改为import cv 和import highgui,然后就可以了。
#from opencv.cv import *
#from opencv.highgui import *
import cv
import highgui
 
# the codec existing in cvcapp.cpp,
# need to have a better way to specify them in the future
# WARNING: I have see only MPEG1VIDEO working on my computer
 
import ctypes
H263 = 0x33363255
H263I = 0x33363249
MSMPEG4V3 = 0x33564944
MPEG4 = 0x58564944
MSMPEG4V2 = 0x3234504D
MJPEG = 0x47504A4D
MPEG1VIDEO = 0x314D4950
AC3 = 0x2000
MP2 = 0x50
FLV1 = 0x31564C46
 
# so, here is the main part of the program
 
if __name__ == '__main__':
 
    # a small welcome
    print "OpenCV Python capture video"
 
    # first, create the necessary window
    highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE)
 
    # move the new window to a better place
    highgui.cvMoveWindow ('Camera', 10, 10)
 
    try:
        # try to get the device number from the command line
        device = int (sys.argv [1])
 
        # got it ! so remove it from the arguments
        del sys.argv [1]
    except (IndexError, ValueError):
        # no device number on the command line, assume we want the 1st device
        device = 0
 
    if len (sys.argv) == 1:
        # no argument on the command line, try to use the camera
        capture = highgui.cvCreateCameraCapture (device)
    else:
        # we have an argument on the command line,
        # we can assume this is a file name, so open it
        capture = highgui.cvCreateFileCapture (sys.argv [1])            
 
    # check that capture device is OK
    if not capture:
        print "Error opening capture device"
        sys.exit (1)
 
    # capture the 1st frame to get some propertie on it
    frame = highgui.cvQueryFrame (capture)
 
    # get size of the frame
    frame_size = cv.cvGetSize (frame)
 
    # get the frame rate of the capture device
    fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS)
    if fps == 0:
        # no fps getted, so set it to 30 by default
        fps = 30
 
    # create the writer
    writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
                                          fps, frame_size, True)
 
    # check the writer is OK
    if not writer:
        print "Error opening writer"
        sys.exit (1)
 
    while 1:
        # do forever
 
        # 1. capture the current image
        frame = highgui.cvQueryFrame (capture)
        if frame is None:
            # no image captured... end the processing
            break
 
        # write the frame to the output file
        highgui.cvWriteFrame (writer, frame)
 
        # display the frames to have a visual output
        highgui.cvShowImage ('Camera', frame)
 
        # handle events
        k = highgui.cvWaitKey (5)
 
        if k % 0x100 == 27:
            # user has press the ESC key, so exit
            break
 
    # end working with the writer
    # not working at this time... Need to implement some typemaps...
    # but exiting without calling it is OK in this simple application
    #highgui.cvReleaseVideoWriter (writer)
Views
Personal tools