Tag Archives: Machine Vision

Configure OpenCV-Python development environment in Windows

In older days, it is relative complex to configure a OpenCV development environment. You have to install an IDE, e.g. Mircosoft Visual Studio, and have to set many configurations of the project, such as the include folder, the header folder and where the IDE can find the third-party OpenCV library. But after Python becomes one of the main streams, things has changed. Python has proven to be a better and easier way to program and test computer vision programs, compared with C++. In this article, I will show you the simple steps that could help you to configure an OpenCV-Python ready environment in Windows.

1. First, you have to download and install Python 2.7 and Numpy. Install them to their default locations, in order to avoid any further issues. The reason to use Python 2.7 instead of Python 3.x is that Python 2.7 is best supported by most third-party libraries, while Python 3 is too new for all the scientific libraries to be fully supported.

2. Download the latest OpenCV release from Sourceforge and extract it into a system folder, e.g., C:\Program Files\opencv.

3. Goto $(opencv folder)\build\python\2.7\x86 or \x64 based on your machine type. Copy cv2.pyd to $(python folder)\lib\site-packages.

That’s all you have to do. Really easy and will only cost you 10 minutes. Now let’s open the Python IDLE and test if the environment is working:

import numpy
import cv2

print cv2.__version__

Run the code and if you got the version printed and no error message, then congratulations: you have succeed. But if you did get error message, then you should check the version of both the Python and the OpenCV is correct. If you have multiple versions of Python, then check if you have run the right version of IDLE.

Before we try more complicated vision algorithms, let’s simply connect a USB camera first and try to get the video stream from the camera and display it on the screen:

import cv2

cap = cv2.VideoCapture(0)

while(True):
    _, frameInput = cap.read()
    cv2.imshow('camera', frameInput)

    if cv2.waitKey(33) == 27:
        break

cv2.destroyAllWindows()
cap.release()

What you should got is a pop-up window displaying the current image captured from the camera. You don’t have to understand this code at the moment, but the thing is once you got the live video stream from the USB camera, further steps can be done to process, modify and save it. More details and examples will be discussed later in my further blogs.

References
[1] OpenCV-Python Tutorials, Install OpenCV-Python in Windows, available at: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html