Since changing from OpenCV C++ to OpenCV-Python, my programming life with OpenCV became a lot easier. If downloading an OpenCV’s wheel (.whl) from https://www.lfd.uci.edu/~gohlke/pythonlibs/ and pip install
it, was still not easy enough, there was a super-convenient PyPi package (https://pypi.org/) that allowed us to install an unofficial pre-built OpenCV with a single command line:
pip install opencv-contrib-python
However, since September 2018, there were changes. The opencv-contrib-python
package was no longer including non-free algorithms like SIFT and SURF. Thinking of SIFT as one of important state-of-the-arts in computer vision, I decided to give it a try—to build all OpenCV from sources in Windows OS.
Although building OpenCV from sources was a kind of nightmares in my old days of OpenCV C++ on Windows OS, this time, I got it. After many hours of googling and CMaking, finally I am able to build OpenCV-Python (including contrib and non-free modules) from sources. And this blog is a memo of how I did it.
Before we start, I want to thank to these two tutorials for their great documents:
- Building and Installing OpenCV with Extra Modules on Windows 7 64-bit: https://putuyuwono.wordpress.com/2015/04/23/building-and-installing-opencv-3-0-on-windows-7-64-bit/
- Install OpenCV-Python in Windows: https://docs.opencv.org/3.3.1/d5/de5/tutorial_py_setup_in_windows.html
:::::::: STEP 0: Get ready ::::::::
Here is a list of what required to build OpenCV-Python from sources (referring to my laptop environment):
- Windows OS. Mine is Windows 10 Enterprise N 64-bit.
- Microsoft Visual C++ 14 (2015) or 15 (2017). Mine is Microsoft Visual Studio Community 2017 (version 15.9.5) with Visual C++ 2017. As a community version, there is a 30-day free trial limitation. Once expired, you may need another Microsoft account to continue using this VS community version.
- Python interpreter from https://www.python.org/downloads/. Mine is Python 3.6.8 x64 installed at
C:\Python\Python368-64\
(withpip
properly installed). Although there are Python 3.7.x available at the time of this blog, I am sticking with Python 3.6 as it is still a requirement of TensorFlow library. - CMake for Windows from https://cmake.org/download/. Mine is CMake 3.13.2 (win64-x64).
Make sure that everything listed above is installed and works correctly. Then create an empty folder to store our to-be-built OpenCV. For me, I created three new empty folders in the following structure:
C:\opencv401_contrib
C:\opencv401_contrib\source
C:\opencv401_contrib\build
To avoid possible conflicts, I think it is a good idea to uninstall any existing OpenCV-Python from the computer before continuing to STEP1. For me, I did this by executing two commands in Windows command prompt: (Note:- if you use a different PyPi package of OpenCV, just uninstall it)
pip uninstall opencv-python pip uninstall opencv-contrib-python
At this point, my laptop was clean and free from any OpenCV-Python installation, meaning that I got an error when trying to import OpenCV library in Python with
import cv2
:::: Update on May 21, 2019 ::::
On another OpenCV-Python setup, I just discovered that installation directories of Python and/or OpenCV DO MATTER with this blog.
It was the case when I put both the Python installation directory and the opencv401_contrib
folder under C:\Program Files\
. At first, there seemed to be no problem as long as I chose to run Command Prompt, CMake and Visual Studio as an administrator. However, once I finished STEP3.3 (with no error at all) and tried the code snippets in STEP4, Python returned an error saying that ‘cv2’ module is not known.
After careful inspection, I found that, despite of no build error in Visual Studio, there were some missing files in STEP3.2-3.3. Somehow Visual Studio skipped building some files including the important ‘cv2’ folder that links my compiled OpenCV to the Python environment.
My working solution is to completely uninstall Python and delete opencv401_contrib
folder from C:\Program Files\
. Then, reinstall them both to C:\
or wherever directory that does not require Run-as-Administrator.
:::::::: STEP 1: Download OpenCV sources ::::::::
It is time to download OpenCV’s sources from Github. In order to include contrib
and non-free
modules, there are two zip files to be downloaded:
- Main OpenCV: https://github.com/Itseez/opencv => opencv-master.zip (87.3 MB)
- Extra modules: https://github.com/opencv/opencv_contrib => opencv_contrib-master.zip (58.6 MB)
Unzip (Extract Here) both downloaded files into C:\opencv401_contrib\source
. The result on my laptop looks like this:
:::::::: STEP 2: Make Visual Studio project with CMake ::::::::
Open CMake-GUI and choose File > Delete Cache to clear any previous history. I always do this whenever I make some mistake in configuring CMake and want to restart all over again.
Then do the following steps in CMake-GUI: (Be careful not to include backslash in any CMake’s file directory, use slash instead)
- Browse Source… >
C:/opencv401_contrib/source/opencv-master
- Browse Build… >
C:/opencv401_contrib/build
- Check Grouped box.
- Click Configure.
- When clicking Configure for the first time, CMake will ask us to specify which generator to be used. For my laptop, I chose Visual Studio 14 2015 x64.
- When (the first) configuring finishes, in OPENCV group, check OPENCV_ENABLE_NONFREE to include SIFT and SURF.
- In OPENCV group, set OPENCV_EXTRA_MODULES_PATH to
C:/opencv401_contrib/source/opencv_contrib-master/modules
. This is in order to include extra modules(opencv_contrib)
of OpenCV. - In OPENCV group, check OPENCV_PYTHON3_VERSION.
- Click Configure again. If everything goes well, there will be the following messages in CMake log. The OpenCV Module: To be built: will include a long list saying
aruco bgsegm bioinspired calib3d ccalib core datasets dnn dnn_objdetect dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc java_bindings_generator line_descriptor ml objdetect optflow phase_unwrapping photo plot python3 python_bindings_generator reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab xfeatures2d ximgproc xobjdetect xphoto
. - Finally, click Generate to create Visual C++ solution for OpenCV. The generation will take few seconds. When done, there will be a lot of files in
C:\opencv410_contrib\build
including ALL_BUILD.vcxproj.
:::::::: STEP 3: Use Visual C++ to build OpenCV from sources ::::::::
Open ALL_BUILD.vcxproj from STEP2 in Microsoft Visual Studio (for OpenCV 4.0.1, at least VC 14 2015 or VC 15 2017 is required).
- Change the build mode of Visual C++ from Debug to Release. Also, make sure that the next drop-down-box is set to x64.
- In solution explorer, right click at the ALL_BUILD project and select Build. Please be patient because this step of building OpenCV from sources, will take a long time (25 minutes for me). In my laptop, it finished building with the VS message saying “Build: 148 succeeded, 0 failed, 0 up-to-date, 0 skipped”.
- Finally, in solution explorer, right click at the INSTALL project and select Build. This step takes just a few seconds to finish. In my laptop, it finished installing with the VS message saying “Build: 2 succeeded, 0 failed, 147 up-to-date, 0 skipped”. (Update on May 21, 2019) To confirm that OpenCV is properly installed to the existing Python environment, navigate to the Python installation directory and look for a folder named
\Lib\site-packages\cv2
. Without this cv2 folder (and its contents), OpenCV becomes an unknown module for Python.
Now our OpenCV-Python should be ready for use.
:::::::: STEP 4: Test OpenCV-Python and SIFT/SURF ::::::::
Open your Python IDE and try the following code snippets, if OpenCV-Python is installed correctly, there should be no error at import cv2
. And if non-free module is successfully included, the last two commands of SIFT and SURF should generate no error.
import cv2 print( cv2.__version__ ) detector1 = cv2.xfeatures2d.SIFT_create() detector2 = cv2.xfeatures2d.SURF_create()