Setup OpenCV for Python in Windows

February 26, 2011

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 Python site. 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 OpenCV Sourceforge. 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

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

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

import cv

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

while True:
  img = cv.QueryFrame(capture)
  cv.ShowImage("camera", img)
  if cv.WaitKey(10) == 27:
    break
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 it here 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.

Discussion, links, and tweets