Installing OpenCV on Windows 7 for Python 2.7

當我第一次爲了做一個項目而接觸Opencv的時候,我按照網上的方法安裝了opencv2.2或者opecv2.3但是結果總是出問題。比如:ImportError: No module named opencv

後來終於明白問題出在哪個地方了,現在我把我的經歷分析給大家。希望對剛接觸Opencv的有所幫助

對於Python2.5的安裝我上面一片文章有所介紹。經測試沒有問題,然而,我需要在Python2.7上面運行。

後來我在網上看到了這段對話:

As of OpenCV 2.2.0, the package name for the Python bindings is "cv".The old bindings named "opencv" are not maintained any longer. You might have to adjust your code. Seehttp://opencv.willowgarage.com/wiki/PythonInterface.

The official OpenCV installer does not install the Python bindings into your Python directory. There should be a Python2.7 directory inside your OpenCV 2.2.0 installation directory. Copy the whole Lib folder from OpenCV\Python2.7\ to C:\Python27\ and make sure your OpenCV\bin directory is in the Windows DLL search path.

Alternatively use the opencv-python installers at http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv.

大概意思是隨着版本的升級導入方法有變化,下面就是Python27的安裝方法

Setup OpenCV for Python in Windows

I have always struggled when trying to set up OpenCV for Python before. So I decide to write this post to help myself in the future and share with you =). My setup is for OpenCV 2.2 but I think you can apply for any version of OpenCV.

Step 1: Download and install Python 2.7 from http://www.python.org/getit/releases/2.7.2/. You need to install the 32bit version of Python. OpenCV currently does not work with Python 64bit.

Step 2: Download and install OpenCV 2.2 from http://sourceforge.net/projects/opencvlibrary/files/. Note that this version only supports Python 2.7 (not 3.x).

Step 3: Download and install NumPy 1.6.1 and SciPy 0.9.0 from: (you need to choose the files which support Python 2.7)

Step 4:Setup Windows Path in Environment Variables

  • Add “C:/Python2.7;C:/OpenCV2.2/bin” to PATH variable (You need to change the directory to where you install Python and OpenCV).
  • Create PYTHONPATH variable and put “C:/OpenCV2.2/Python2.7/Lib/site-packages” as value.

Update: For OpenCV2.3, There are two other options for this step:

  • As Kyle commented below, you can copy the content of folder “C:\opencv\build\python\2.7” (for opencv 2.2: C:/OpenCV2.2/Python2.7/Lib/site-packages) to folder “C:\Python27\Lib\site-packages
  • OR, add these two lines at the beginning of your program:
    1 import sys
    2 sys.path.append("C:\OpenCV2.2\Python2.7\Lib\site-packages")

Yosh!!! You have finished setup OpenCV for Python in Windows. Open Python IDLE, create a new file, add the program below and run it:

1 importcv
2  
3 cv.NamedWindow("camera",1)
4 capture=cv.CaptureFromCAM(0)
5  
6 whileTrue:
7     img=cv.QueryFrame(capture)
8     cv.ShowImage("camera", img)
9     ifcv.WaitKey(10)==27:
10         break
11 cv.DestroyWindow("camera")

(This is the program to show your webcam)

UPDATE:

  1. OpenCV has just released version 2.3.1 which is more stable than 2.2 I think. You can download ithere and try it (the setup is same as 2.2).
  2. About the black screen you encounter with the example, I think it’s because your camera is not supported. My camera works fine. You can find more information here.

下面是網上的翻譯版本:

1 .下載 OpenCV 2.3.1 。文中下載了OpenCV-2.3.1-win-superpack (大概124MB,解壓後1G多)。他不需編譯,使用方便 下載地址 

2. OpenCV-2.3.1-win-superpack.exe是自解壓文件,直接運行。即可解壓。默認解壓到opencv文件夾裏。

3.下載numpy。opencv的python版需要該模塊。下載頁面在這裏 注意,下載和Python版本一致的numpy。文中下載的是numpy-1.6.1-win32-superpack-python2.7.exe,6.1M

4.運行numpy-1.6.1-win32-superpack-python2.7.exe

5.安裝python2.7,默認安裝在C:\Python27


好了,配置

opencv文件夾中,build->python->2.7 複製2.7下面的所有文件 到C:\Python27\Lib\site-packages 中

測試

打開opencv文件夾中的samples\python

雙擊drawing.py 如果沒有問題應該看到彩色條紋。

注意 這裏的其他樣例 有可能一閃而過,什麼都不顯示。一種原因是哪個腳本需要參數,另一種是腳本中寫圖片文件路徑錯誤,還有一種是urllib的問題,打開代碼看看,應該比較容易理解。


The official OpenCV installer does not install the Python bindings into your Python directory. There should be a Python2.7 directory inside your OpenCV 2.2.0 installation directory. Copy the whole Lib folder from OpenCV\Python2.7\ to C:\Python27\ and make sure your OpenCV\bin directory is in the Windows DLL search path.

Alternatively use the opencv-python installers at http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv.


同時由於Opencv沒有綁定Python,我們需要去下載opencv包




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章