美文网首页
编译包含 OpenCV-contrib 的 OpenCV 3.0

编译包含 OpenCV-contrib 的 OpenCV 3.0

作者: CCXJTU_NTU | 来源:发表于2017-06-12 11:47 被阅读0次

手动给 Python 添加包含 SIFT 模块的 OpenCV

OpenCV & Python

This image comes from Goolge Image.

最近,我想要使用 OpenCV 中的 SIFT 库。因为在元庆的 Travi-Navi 论文中见过他使用这个库,感觉应该在 OpenCV 中。看了一些资料后发现由于一些算法申请了专利,所以从 OpenCV 3.0 版本以后,不在官方的 OpenCV 文件中提供访问。如果需要的话,可以自己使用微软的 Visual Studio 去编译。另外,在根据一些视频资料和文字资料进行编译后,总得不到正确的结果。所以在这篇文章中,我计划一步一步的记录正确的过程。

所需资料

  1. 微软 Visual Studio(mine is version 2015, Microsoft Visual Studio Express 2015 for Windows Desktop)
  2. Python (mine is version 2.7.11)
  3. OpenCV: 源代码
  4. OpenCV_contrib: 源代码
  5. CMake 包含 GUI
  6. NumPy and Matplotlib

当以上的资料全都准备好之后再进行下面的操作。

CMake 部分

  1. 解压缩 OpenCVOpenCV_contrib 到一个已知的位置并且新建一个文件夹。
  2. 打开 CMake 软件并且找到下面两个空白区域所需路径。见 Figure 1。
  • Where is the source code
  • Where to build the binaries


    Figure 1: CMake settings

    将新建文件夹的位置放在 “Where to build the binaries” 地方。

  1. 点击 Configure 按钮并且选择正确的 Visual Studio 版本,见 Figure 2。需要选择正确的 generator。我选择了 Visual Studio 14 2015 Win64。每个版本的都有三个选项如下。
  • Visual Studio 14 2015: 32位的应用
  • Visual Studio 14 2015 Win64: 64位的应用
  • Visual Studio 14 2015 ARM: 给移动平台使用
    由于我使用64位的 Python,所以我选择 Win64 的选项。选择 use default native compilers并点击结束按钮。
    FIgure 2: CMake Configure
  1. 有些东西需要选中而另外一些则不然。我的详细设置可以在官网看到。有的选项已经发生了变化,我保留了默认的设置。记得添加 OpenCV-contrib 的路径信息。一般它的路径是 .\directory\to\OpenCV_contrib\opencv_contrib-master\modules
  2. Configure 后如果没错,点击 Generate 按钮。看到 Configuring doneGenerating done的提示后,就可以在 "Where to build the binaries" 看到生成新的文件 OpenCV.sln

Microsoft Visual Studio 部分

  1. 将编译模式设置为 Release
  2. 64位应用对应 x64。见 Figure 3。
    Figure 3: Release mode and x64 in Visual Studio
  3. 在 Visual Studio 的 Solution Explorer 区域 (见Figure 4),在 CMake Targets 项目下找到 ALL_BUILD。右键选择编译按钮。
    Figure 4: Solution Explorer of Visual Studio
  4. 也对 CMake Targets 项目下的 INSTALL 进行右键编译。

复制粘贴

  1. where\to\build\binaries\lib\Release 下找到文件 cv2.pyd 并且拷贝到 Python\directory\Lib\site-packages 中。
  2. where\to\build\binaries\bin\Release 下找到所有的 ".dll" 文件 (见 Figure 5)。并且把他们拷贝到 Python 的根目录 (the same directory of python.exe) 下。
    Figure 5: dll files

测试

  1. 打开 Python Shell 输入如下代码:
    import cv2
    失败的话就会看到如下提示:
    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. 若没有报错,则继续输入如下代码检查 OpenCV 版本:
    cv2.version
    如果看到下面所示的执行结果则表示成功的将 OpenCV 添加到了 Python 之中。
    In [1]: import cv2

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

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

相关文章

网友评论

      本文标题:编译包含 OpenCV-contrib 的 OpenCV 3.0

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