您的位置:首页 > 编程语言 > Python开发

Installing OpenCV on Windows 7 for Python 2.7

2012-04-29 17:27 579 查看
当我第一次为了做一个项目而接触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)

http://sourceforge.net/projects/numpy/files/NumPy/1.6.1/

http://sourceforge.net/projects/scipy/files/scipy/0.9.0/

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
import
cv
2
3
cv.NamedWindow(
"camera"
,
1
)
4
capture
=
cv.CaptureFromCAM(
0
)
5
6
while
True
:
7
img
=
cv.QueryFrame(capture)
8
cv.ShowImage(
"camera"
,
img)
9
if
cv.WaitKey(
10
)
=
=
27
:
10
break
11
cv.DestroyWindow(
"camera"
)
(This is the program to show your webcam)

UPDATE:

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).

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包
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: