[OpenCV] Xcode 下 opencv 3.1 及 opencv_contrib 库的编译

opencv_logo

0、系统环境
Mac OSX 10.11.6
CMake 3.0.2
Xcode 8.0
OpenCV 3.1.x (Build 2016.10.18)

最新版的 OpenCV 3.1.0 存在一些问题,修改会比较麻烦,你可以下载它官网的最新代码:
https://github.com/opencv/opencv/archive/master.zip
以及:
https://github.com/opencv/opencv_contrib/archive/master.zip

我所使用的 OpenCV 代码附带如下,也包含我所做的一些修改,你可以尝试编译运行我的版本(基于2016年10月18日的版本):
https://pan.baidu.com/s/1cqrcjO

1、依赖库安装
1)安装 gflags:
在命令行运行:

2)安装 Command Line Tools(可选,如果需要编译 CUDA 库):
在命令行运行:

然后点击 [Install] 按钮。

如果下载速度太慢也可下载这里的 dmg 安装包:
官网下载:http://adcdownload.apple.com/Developer_Tools/Command_Line_Tools_macOS_10.12_for_Xcode_8/Command_Line_Tools_macOS_10.12_for_Xcode_8.dmg
网盘下载:https://pan.baidu.com/s/1boBD08f

3)安装 VTK7(可选,如果需要 VTK 模块):
在命令行运行:

2、设置 CMake
1)打开 CMake 客户端;

2)点击 [Add Entry] 按钮,如图所示增加 PATH 属性的键 GFLAGS_INCLUDE_DIR 键值为 /usr/local/include:
6c9553c6-ab25-4654-b946-cb6debabd13f

3)查找 OPENCV_EXTRA_MODULES_PATH 键,将键值修改为你下载 opencv_contrib 库下 modules 文件夹的的路径 /modules:
d1bbc318-5713-49b1-a28d-afc5edbc1df8

4)选择 Examples 选项(可选)
如果你需要编译 OpenCV Examples,请查找 BUILD_EXAMPLES 键并选中。
screen-shot-2016-10-19-at-4-00-50-pm

如果你需要 C 或者 Python 版本的 Examples 则记得勾选相应的选项。

5)选择合适的 Where to build binaries 路径,依次点击 [Configure] 和 [Generate] 按钮生成相应 Xcode 工程。直到出现以下字样表示生成工程成功:
Tests and samples:
Tests: YES
Performance tests: YES
C/C++ Examples: NO

Install path: /usr/local

cvconfig.h is in: /Users/valiantliu/Documents/opencv/osx
-----------------------------------------------------------------

Configuring done
Generating done

3、编译 & 运行
1)打开你生成 Xcode 工程路径下的 OpenCV.xcodeproj 文件;

2)弹出菜单点击 [Automatically Create Schemes] 按钮:
ddac0b41-9ed8-4fcb-9493-23618d089056

3)选中 opencv_modules 的 Scheme 然后点击 Build 按钮进行编译。
a9a064d6-0a67-4daf-b40c-6b312d748ae2

如果显示 Build Succeeded 则表示 opencv 编译成功。

常见问题
1、错误:nvcc fatal : The version ('80000') of the host compiler ('Apple clang') is not supported
在编译 OpenCV 时,如果您选择了 WITH_CUDA,有可能出现如下错误:
nvcc fatal : The version ('80000') of the host compiler ('Apple clang') is not supported
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:206 (message):
Error generating
/Users/valiantliu/Documents/opencv/osx/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/Debug/cuda_compile_generated_gpu_mat.cu.o

方法1:出现此问题有可能是没有安装 Command Line Tools for Xcode,请参照步骤1中2)进行安装。
但是需要注意 Xcode 8.0 与 CUDA 8.0 不兼容,需要使用 Xcode 7.3 的 Command Line Tools。

方法2:如果不需要 CUDA 相关模块,可以在 CMake GUI 中关闭 WITH_CUDA 选项:
57c256bb-9ae6-42ce-96f9-9414ca1ffa24

以及以下 cude 相关模块:
screen-shot-2016-10-19-at-5-14-34-pm

2、错误:Incomplete type 'cv::Mat' named in nested name specifier
在编译 viz 相关模块时,可能出现如下错误:
/usr/local/include/opencv2/core/mat.hpp:58:13: Incomplete type 'cv::Mat' named in nested name specifier

有可能是你本地安装了 OpenCV 的 2.x 版本,解决方法是卸载之前版本:

3、错误:Requested modules not available: vtkRenderingOpenGL
在运行 CMake Configure 时可能出现如下错误:
Could NOT find Matlab (missing: MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN)
CMake Error at /usr/local/Cellar/vtk/7.0.0/lib/cmake/vtk-7.0/vtkModuleAPI.cmake:120 (message):
Requested modules not available:

vtkRenderingOpenGL
Call Stack (most recent call first):
/usr/local/lib/cmake/vtk-7.0/VTKConfig.cmake:88 (vtk_module_config)
cmake/OpenCVDetectVTK.cmake:6 (find_package)
CMakeLists.txt:597 (include)

方法1:如果您不需要 VTK,请在 CMake 中关闭 WITH_VTK 选项。
方法2:如果您需要 VTK,请编辑 cmake/OpenCVDetectVTK.cmake 文件:
查找:

替换为:

4、错误:xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance
在运行 CMake 时可能出现如下错误:
xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

这通常是因为 CMake 无法找到原来的 Xcode 地址,执行以下命令即可修复:

5、错误:'QTKit/QTKit.h' file not found
在使用 Xcode 编译时可能出现如下问题:
/Users/valiantliu/Documents/opencv-3.1.0/modules/videoio/src/cap_qtkit.mm:46:9: 'QTKit/QTKit.h' file not found

这是由于 macOS 10.12 将 QTKit 合入 AVFoundation 框架所致。在最新的 opencv 版本中已经修正这一问题:
https://github.com/opencv/opencv/pull/7266/files

Add a Comment

您的电子邮箱地址不会被公开。 必填项已用*标注