美文网首页
Build OpenCV with extra modules

Build OpenCV with extra modules

作者: CCXJTU_NTU | 来源:发表于2017-06-12 00:39 被阅读0次
    OpenCV & Python

    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

    1. Microsoft Visual Studio (mine is version 2015, Microsoft Visual Studio Express 2015 for Windows Desktop)
    2. Python (mine is version 2.7.11)
    3. OpenCV: source code
    4. OpenCV_contrib: extra modules source code
    5. CMake with GUI
    6. NumPy and Matplotlib

    Please guarantee that all the above dependencies are available. Follow the steps below:

    CMake part

    1. Unzip OpenCV and OpenCV_contrib to a known directory and create a new folder.
    2. Open CMake and specify the two empty boxes shown in Figure 1.
    • Where is the source code
    • Where to build the binaries


      Figure 1: CMake settings

      Set the "Where to build the binaries" as the directory of the new folder.

    1. 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.
      FIgure 2: CMake Configure
    1. 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.

    2. 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

    1. Set the build mode as Release.
    2. Since I generated Win64 project, I use x64. You can find in Figure 3.
      Figure 3: Release mode and x64 in Visual Studio
    3. In Solution Explorer(Figure 4) of Visual Studio, find ALL_BUILD in CMake Targets item. Right click it and choose build.
      Figure 4: Solution Explorer of Visual Studio
    4. Do the same procedures of last step 3 for INSTALL file just below ALL_BUILD.

    Copy and Paste

    1. Find the cv2.pyd file in where\to\build\binaries\lib\Release and copy it into Python\directory\Lib\site-packages.
    2. 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).
      Figure 5: dll files

    Testing

    1. 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.

    2. 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 cv2

       In [2]: cv2.__version__
       Out[2]: '3.2.0-dev'
      

    You can also check the Chinese version of this article here.
    您可以点击这里查看本文的中文版本。

    相关文章

      网友评论

          本文标题:Build OpenCV with extra modules

          本文链接:https://www.haomeiwen.com/subject/rxbkqxtx.html