How to configure Python-OpenCV

From Lofaro Lab Wiki
Jump to: navigation, search

This tutorial will teach you how to configure python-OpenCV in your windows machine (Linux will work as same) so that you can use python language to do image processing using the open computer vision library (OpenCV). We will install OpenCV from prebuilt binaries as it is easy for any beginner. You need to go through the following steps to configure python-OpenCV.

Step 1: You need to have python installed in your windows system. To download python in your system you need to go to https://www.python.org/download, and download version 2.7.x cause these versions are proven compatible with opencv libraries. Other may work also, but for the first time going with certainty must be better. Install the python in a drive of your computer where you are used to install your software, make it better by making a python folder and install python there. If C drive is your software installation folder your python should be in C: /python. It is better to install python in short directory so that you can access the file more easily. Once your python is installed you can go to windows search and type python IDLE, you should get a user interface open for writing code in python.

Step 2: Download Numpy which is a great library of python consisting of numerical operation. OpenCV has direct dependencies on Numpy. So you need to make sure Numpy is installed properly. If you follow OpenCV installation guidance you may install the Numpy version 7 which might end up with a compatible error. So make sure you download the latest or at least greater than 7 version of Numpy which should work fine. After installing Numpy go to python IDLE and write Import Numpy. If it doesn’t give any error than you are okay to go with it.

Step 3: Download OpenCV from the OpenCV page’s recommended site. You will get a zip file which you need to unzip by double clicking. After unzipping the files go to opencv/build/python/2.7 folder where you will get cv2.pyd file. At this moment you need to make sure which version of python you are using. You need to know your python is 32bits or 64bits. If you just copy from one and past it to python you might end up in an error, when trying to import it in your program. The error will be ImportError: DLL load failed: %1 is not a valid Win32 application. To know about your python version go to the IDLE and write

Import platform
Platform.architecture()

This two commands will give you information about your python version. Go again to opencv/build/python/2.7 and choose which version you need, copy the cv2.pyd file of the version. Now you need to paste it in C:/python/lib/site-packages. Check if OpenCV works fine in you python environment by importing it in the IDLE giving the command

import cv2 

If it doesn’t through any error you are good to go with this.

Step 4: You can install Matplotlib library, which is another python library gives you the ability to plot any mathematical functions. It is not mandatory as OpenCV doesn’t have any dependencies on it but it is recommended for your working on image processing.