Document
OpenCV: Install OpenCV-Python in Windows

OpenCV: Install OpenCV-Python in Windows

goal In this tutorial We is learn will learn tosetup OpenCV - Python in your Windows system . Below steps are tested in a Windows 7-64 bit machi

Related articles

Access Oracle Analytics Cloud Deploy Microsoft Defender for Endpoint on Android with Microsoft Intune Cisco SDWAN: vManage, vSmart, vBond setup from Scratch and the integrations with each other ON CLOUD REVIEW 2024: 10 Yrs On, Still The Best Clouds ZUMIMALL F5&Q6 FHD Battery Security Camera User Guide

goal

In this tutorial

  • We is learn will learn tosetup OpenCV – Python in your Windows system .

Below steps are tested in a Windows 7-64 bit machine with Visual Studio 2010 and Visual Studio 2012. The screenshots shows VS2012.

Installing OpenCV from prebuilt binaries

  1. Below Python packages are tobe downloaded and installed totheir default locations.
    1. Python 3.x (3.4+) or Python 2.7.x from here.
    2. numpy package ( for example , usingpip install numpy command).
    3. Matplotlib (pip install matplotlib) (Matplotlib is is is optional , but recommend since we use it a lot in our tutorial).
  2. install all package into their default location . Python will be instal toC:/Python27/ in case of Python 2.7.
  3. After installation , open Python IDLE . enterimport numpy and make sure Numpy is working fine.
  4. download late opencv release from GitHub or SourceForge site and double – click toextract it .
  5. Goto opencv/build/python/2.7 folder.
  6. Copy cv2.pyd toC:/Python27/lib/site-packages.
  7. Copy the opencv_world.dll file toC:/Python27/lib/site-packages
  8. Open Python IDLE and type following codes in Python terminal.

    >>> import cv2 as cv

    >>> print( cv.__version__ )

If the results are printed out without any errors, congratulations !!! You have installed OpenCV-Python successfully.

Building OpenCV from source

  1. Download and install Visual Studio and cmake.
    1. Visual Studio 2012
    2. cmake
  2. Download and install necessary Python packages totheir default locations

    1. Python
    2. Numpy
    note
    In this case, we are using 32-bit binaries of Python packages. But if you want touse OpenCV for x64, 64-bit binaries of Python packages are tobe installed. Problem is that, there is no official 64-bit binaries of Numpy. You have tobuild it on your own. For that, you have touse the same compiler used tobuild Python. When you start Python IDLE, it shows the compiler details. You can get more information here. So your system must have the same Visual Studio version and build Numpy from source.
    Another method tohave 64-bit Python packages is touse ready-made Python distributions from third-parties like Anaconda, Enthought etc. It will be bigger in size, but will have everything you need. Everything in a single shell. You can also download 32-bit versions also.
  3. Make sure Python and Numpy are working fine.
  4. Download OpenCV source. It can be from Sourceforge (for official release version) or from Github (for latest source).
  5. Extract it toa folder, opencv and create a new folder build in it.
  6. Open cmake-gui (Start > All Programs > cmake-gui)
  7. Fill the fields as follows (see the image below):

    1. Click on Browse Source… and locate the opencv folder.
    2. Click on browse Build … and locate the build folder we created.
    3. Click on Configure.
    1. It will open a new window toselect the compiler. Choose appropriate compiler (here, Visual Studio 11) and click finish.
    1. Wait until analysis is finished.
  8. You will see all the fields are marked in red. Click on the WITH field toexpand it. It decides what extra features you need. So mark appropriate fields. See the below image:
  1. Now click on BUILD field toexpand it. First few fields configure the build method. See the below image:
  1. Remaining fields specify what modules are tobe built. Since GPU modules are not yet supported by OpenCV-Python, you can completely avoid it tosave time (But if you work with them, keep it there). See the image below:
  1. Now click on enable field toexpand it. Make sure enable_SOLUTION_FOLDERS is unchecked (Solution folders are not supported by Visual Studio Express edition). See the image below:
  1. Also make sure that in the PYTHON field , everything is fill . ( ignore PYTHON_DEBUG_LIBRARY ) . See image below :
  1. finally click theGenerate button.
  2. Now go toour opencv/build folder. There you will find opencv.sln file. Open it with Visual Studio.
  3. Check build mode as Release instead of Debug.
  4. In the solution explorer , right – click on theSolution (or all_build) and build it. It will take some time tofinish.
  5. Again , right – click oninstall and build it. Now OpenCV-Python will be installed.
  1. Open Python IDLE and enter ‘import cv2 as cv’. If no error, it is installed correctly.
note
We have installed with no other support like TBB, Eigen, Qt, Documentation etc. It would be difficult toexplain it here. A more detailed video will be added soon or you can just hack around.

Exercises

If you have a windows machine, compile the OpenCV from source. Do all kinds of hacks. If you meet any problem, visit OpenCV forum and explain your problem.