OpenCv视频编码学习之路深度学习-推荐系统-CV-NLP

VS2013+CMake+OpenCV 环境搭建

2018-09-19  本文已影响5人  团不慌

0x00 - 应用目的

笔者使用OpenCV主要目的是暂时完成图像的配准与融合,所以主要用到OpenCVSURFSIFTORB等工具,搭建时主要遇到的问题:


0x01 - Python 默认安装

使用Python时习惯使用pip对需要的包进行安装,所以在初次配置OpenCV时直接执行了下述命令:

pip install opencv-python

安装完成后可以正常引入cv2并完成图像读取显示,但无法使用特征提取函数,使用时报错:

AttributeErrorTraceback (most recent call last)
<ipython-input-5-a409304a8e5d> in <module>()

----> 2 surf = cv2.xfeatures2d.SURF_create(300)

AttributeError: 'module' object has no attribute 'xfeatures2d'

产生该错误原因是OpenCV 3.0+中将部分功能分离成了一个新的库:opencv-contribSURFSIFT属于扩展库中的功能,而默认库中则只包含了表现更好的ORB

0x02 - OpenCV Contrib 安装

按照广大网友指示应先卸载opencv-python后重新安装opencv-contrib-python,即

pip uninstall opencv-python
pip install opencv-contrib-python  # Permission denied 的话可以加上 --user

不知道大家尝试结果如何,反正我是失败了:依然无法使用SURF_create

0x03 - CMake 重新编译

pip安装依赖形式添加OpenCV外,直接从OpenCV官网上下载源码也可以完成依赖添加:

当然这样拷贝出的和pip安装的区别不大,以同样方法将binlib添加进VS工程也会出现无法找到nonfreelegacy的问题,想要解决只能使用CMake重新编译OpenCV(C++的话顺路还能加上Nonfree)。

0x04 - 重新编译OpenCV

准备工具:

编译流程:

  1. 打开CMake/bin/cmake-gui.exe,选择源文件路径及输出路径(可新建)
  2. 执行左下方Configure,选择对应版本编译工具(此处选择了Virtual Studio 12 2013 Win64)
  3. 重复执行Configure直到结束后没有被标记为红色的选项
  4. 搜索PATH,在OPENCV_EXTRA_MODULES_PATH项中填入opencv-contribmodules路径
    (如需Nonfree支持可搜索OPENCV_ENABLE_NONFREE选项并勾选)
  5. 重复执行Configure直到结束后没有被标记为红色的选项
  6. 执行左下方Generate
  7. 完成CMake部分编译
配置样例

此时的输出路径中是没有我们需要的依赖文件的!无法配置路径或者拷贝cv2.pyd并不是编译失败...我们还需要进入VS2013中进行正式的编译:

  1. 打开输出路径中的OpenCV.sln,设置ALL_BUILD为启动项(默认)
  2. 选择对应版本(如 Release x64)
  3. 编译生成解决方案(或直接运行)

VS2013中完成编译后才可以重新配置路径或拷贝cv2.pyd,配置完成后重新运行脚本,功能顺利实现。


0xFF - About ORB

ORB: Oriented FAST and Rotated BRIEF, An efficient alternative to SIFT and SURF and NOT PATENTED.

ORB is basically a fusion of FAST keypoint detector and BRIEF descriptor with many modifications to enhance the performance. First it use FAST to find keypoints, then apply Harris corner measure to find top N points among them. It also use pyramid to produce multiscale-features. But one problem is that, FAST doesn’t compute the orientation. So what about rotation invariance? Authors came up with following modification. [Get all theory here]

ORB in OpenCV :

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('simple.jpg',0)

# Initiate STAR detector
orb = cv2.ORB()

# find the keypoints with ORB
kp = orb.detect(img,None)

# compute the descriptors with ORB
kp, des = orb.compute(img, kp)

# draw only keypoints location,not size and orientation
img2 = cv2.drawKeypoints(img,kp,color=(0,255,0), flags=0)
plt.imshow(img2),plt.show()
Result

Additional Resources:
Ethan Rublee, Vincent Rabaud, Kurt Konolige, Gary R. Bradski: ORB: An efficient alternative to SIFT or SURF. ICCV 2011: 2564-2571.

上一篇 下一篇

猜你喜欢

热点阅读