This image comes from Goolge Image.
Recently, I try to utilize the SIFT module in OpenCV. I think that this module should be included by OpenCV because I remembered that Yuanqing had ever used SIFT with OpenCV in his Travi-Navi paper. After I read some materials online, it turns out that some algorithms are not included since version 3.0 just because they are patented. If you want to use those patented algorithms, you can build OpenCV in Microsoft Visual Studio. That is why I write this article to note what I have learned in building OpenCV with extra modules for Python. On the other hand, I cannot get the correct results following the so-called videos and tutorials online. So I write this article to record the correct whole procedures step by step.
Required materials
- Microsoft Visual Studio (mine is version 2015, Microsoft Visual Studio Express 2015 for Windows Desktop)
- Python (mine is version 2.7.11)
- OpenCV: source code
- OpenCV_contrib: extra modules source code
- CMake with GUI
- NumPy and Matplotlib
Please guarantee that all the above dependencies are available. Follow the steps below:
CMake part
- Unzip OpenCV and OpenCV_contrib to a known directory and create a new folder.
- Open CMake and specify the two empty boxes shown in Figure 1.
- Where is the source code
-
Where to build the binaries
Set the "Where to build the binaries" as the directory of the new folder.
- Click the Configure button in CMake GUI and choose the correct Visual Studio version shown in Figure 2. It asks to specify the generator for this project. I choose Visual Studio 14 2015 Win64. There are three options for each version of Visual Studio.
- Visual Studio 14 2015: 32-bits application in VS
- Visual Studio 14 2015 Win64: 64-bits application in VS
- Visual Studio 14 2015 ARM: mobile version of OpenCV
I choose Win64 because my Python is 64-bits. Then choose on use default native compilers. Click finish.
Some items are supposed to be checked and some are not. The details of mine can be found on the official website. Please note that some fields have changed. I just leave them alone. Remember to add the directory of OPENCV_EXTRA_MUDULES_PATH, which should be .\directory\to\OpenCV_contrib\opencv_contrib-master\modules.
After Configure the source codes without any errors, click Generate button. When you see the Configuring done and Generating done, you can use Visual Studio open OpenCV.sln in "Where to build the binaries" directory.
Microsoft Visual Studio part
- Set the build mode as Release.
- Since I generated Win64 project, I use x64. You can find in Figure 3.
- In Solution Explorer(Figure 4) of Visual Studio, find ALL_BUILD in CMake Targets item. Right click it and choose build.
- Do the same procedures of last step 3 for INSTALL file just below ALL_BUILD.
Copy and Paste
- Find the cv2.pyd file in where\to\build\binaries\lib\Release and copy it into Python\directory\Lib\site-packages.
- In where\to\build\binaries\bin\Release, find all the ".dll" files in Figure 5 and copy them to Python root directory (the same directory of python.exe).
Testing
Open Python Shell and input the following codes:
import cv2
If failed, you can see the error shown below.
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import cv2
ImportError: DLL load failed: %1 is not a valid Win32 application.-
If no errors, input the version checking codes:
cv2.version
Then you can see the results shown below, which means that you have successfully install OpenCV for you Python.
In [1]: import cv2In [2]: cv2.__version__ Out[2]: '3.2.0-dev'
You can also check the Chinese version of this article here.
您可以点击这里查看本文的中文版本。