Sunday, December 21, 2008

Python&OpenCV






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来写东西
了。


Linux用户


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


OpenCV Python的范例


  1.  
  2. #! /usr/bin/env python
  3.  
  4. import sys
  5.  
  6. # import the necessary things for OpenCV
  7. #原先范例的格式是下面的两行,也就是from opencv.cv import*
  8. #这里需要改为import cv 和import highgui,然后就可以了。
  9. #from opencv.cv import *
  10. #from opencv.highgui import *
  11. import cv
  12. import highgui
  13.  
  14. # the codec existing in cvcapp.cpp,
  15. # need to have a better way to specify them in the future
  16. # WARNING: I have see only MPEG1VIDEO working on my computer
  17.  
  18. import ctypes
  19. H263 = 0x33363255
  20. H263I = 0x33363249
  21. MSMPEG4V3 = 0x33564944
  22. MPEG4 = 0x58564944
  23. MSMPEG4V2 = 0x3234504D
  24. MJPEG = 0x47504A4D
  25. MPEG1VIDEO = 0x314D4950
  26. AC3 = 0x2000
  27. MP2 = 0x50
  28. FLV1 = 0x31564C46
  29.  
  30. # so, here is the main part of the program
  31.  
  32. if __name__ == '__main__':
  33.  
  34. # a small welcome
  35. print "OpenCV Python capture video"
  36.  
  37. # first, create the necessary window
  38. highgui.cvNamedWindow ('Camera', highgui.CV_WINDOW_AUTOSIZE)
  39.  
  40. # move the new window to a better place
  41. highgui.cvMoveWindow ('Camera', 10, 10)
  42.  
  43. try:
  44. # try to get the device number from the command line
  45. device = int (sys.argv [1])
  46.  
  47. # got it ! so remove it from the arguments
  48. del sys.argv [1]
  49. except (IndexError, ValueError):
  50. # no device number on the command line, assume we want the 1st device
  51. device = 0
  52.  
  53. if len (sys.argv) == 1:
  54. # no argument on the command line, try to use the camera
  55. capture = highgui.cvCreateCameraCapture (device)
  56. else:
  57. # we have an argument on the command line,
  58. # we can assume this is a file name, so open it
  59. capture = highgui.cvCreateFileCapture (sys.argv [1])
  60.  
  61. # check that capture device is OK
  62. if not capture:
  63. print "Error opening capture device"
  64. sys.exit (1)
  65.  
  66. # capture the 1st frame to get some propertie on it
  67. frame = highgui.cvQueryFrame (capture)
  68.  
  69. # get size of the frame
  70. frame_size = cv.cvGetSize (frame)
  71.  
  72. # get the frame rate of the capture device
  73. fps = highgui.cvGetCaptureProperty (capture, highgui.CV_CAP_PROP_FPS)
  74. if fps == 0:
  75. # no fps getted, so set it to 30 by default
  76. fps = 30
  77.  
  78. # create the writer
  79. writer = highgui.cvCreateVideoWriter ("captured.mpg", MPEG1VIDEO,
  80. fps, frame_size, True)
  81.  
  82. # check the writer is OK
  83. if not writer:
  84. print "Error opening writer"
  85. sys.exit (1)
  86.  
  87. while 1:
  88. # do forever
  89.  
  90. # 1. capture the current image
  91. frame = highgui.cvQueryFrame (capture)
  92. if frame is None:
  93. # no image captured... end the processing
  94. break
  95.  
  96. # write the frame to the output file
  97. highgui.cvWriteFrame (writer, frame)
  98.  
  99. # display the frames to have a visual output
  100. highgui.cvShowImage ('Camera', frame)
  101.  
  102. # handle events
  103. k = highgui.cvWaitKey (5)
  104.  
  105. if k % 0x100 == 27:
  106. # user has press the ESC key, so exit
  107. break
  108.  
  109. # end working with the writer
  110. # not working at this time... Need to implement some typemaps...
  111. # but exiting without calling it is OK in this simple application
  112. #highgui.cvReleaseVideoWriter (writer)
  113.  

No comments: