OpenCV 2.4.9 + CUDA 6.5 + Visual Studio 2013 [中文教程]

来源:互联网 发布:教育软件平台公司 编辑:程序博客网 时间:2024/05/20 12:21

【原文:http://tieba.baidu.com/p/3329042929】

- 首先声明,因为我需要用CUDA,所以需要用CUDA来重新编译OpenCV的源文件。如果不用GPU,可以直接下载预编译好的库

- 接着,我不用OpenCV 3.0 是因为有些CUDA的文件不一样了,如果想编译OpenCV 3.0 + CUDA,过程跟下面是一样的
- 我用了 TBB 才编译成功,不用的话会报错,还没搞清楚为什么

1. 需要的文件
1.1 CMAKE 3.0 [http://www.cmake.org/download/]
1.2 CUDA 6.5 [https://developer.nvidia.com/cuda-toolkit]
1.3 OpenCV 源文件 [https://github.com/Itseez/opencv/releases/tag/2.4.9]
1.4 Intel TBB 库 [https://www.threadingbuildingblocks.org/download]
1.5 Python 2.7 (非必须)
1.6 Eigen 2.0.17 (非必须)
1.7 Visual Studio 2013 (也可以用其他版本)


2. 配置 CMAKE
2.1 打开 CMAKE
- 选择 Source Folder 到 OpenCV 的源文件目录
- 选择 Output Folder
- 勾选 Advanced




2.2 点 Configure,选择编译器
- 选择 ‘Visual Studio 12 2013 Win64′
2.3 配置 CUDA 选项
- 取消 ‘BUILD_DOCS’ and ‘BUILD_EXAMPLES’
- 取消 ‘CUDA_ATTACH_VS_BUILD_RULE_TO_CUDA_FILE’
- 检查 ‘CMAKE_LINKER’, 保证是 Visual Studio 12.0 (vs2013)
- 选上 ‘WITH_CUBLAS’, ‘WITH_CUDA’, ‘WITH_OPENGL’, ‘WITH_TBB’
- 点击 Configure 刷新配置
2.4 配置 TBB 选项
- 把 tbb include path 设置好,例如我的是 “D:\toolkits\tbb43_20140724oss\include”。一定要选到 include文件夹为止。
- 点击 Configure 刷新
- 接着可以看到 tbb 的 library 目录自动有了,但是可能是错的,需要改到 Debug 和 Release 文件夹的上级目录为止。例如我的要加上 ‘vc12*,变成 D:/toolkits/tbb43_20140724oss/lib/intel64/vc12〃
- 点击 Configure 刷新
2.3 点击 Generate 生成 OpenCV.sln


3. 修改 OpenCV 源文件
- 打开 ‘opencv-2.4.9\modules\gpu\src\nvidia\core\NCV.cu’ 加上 #include <algorithm>。不然编译时候会有 *max* undefined error


4. 编译 OpenCV.sln
- 如果 OpenCV,tbb,Python 中的某个安装在 C:\Program Files,那么你需要用管理员权限运行 vs2013 才行
- 我建议先编译 *opencv_core* 和 ‘ opencv_gpu* (右键点击,点 *BUILD*)。如果这两个没错,接下来应该都可以编译
- 右键点击 *ALL_BUILD*,在点 *BUILD*
- *BUILD* 之后在编译一下 *INSTALL*,来把编译好的文件放在一起到 *<Output Folder>\install*
- 在切换到 Release,重复 *ALL_BUILD* 和 *INSTALL*
- *Debug* 应该会有一个 error, *Release* 应该会没有 error
- 编译时间很长。很长。






5. 使用时的问题
- 用 objdetect.hpp 时,我碰到 *std::min* 的 error。我把 *std::* 删掉就好了
- matrix.h 里,碰到 free() 有 error。我把 *free()* 改成 *_free()* 就好了
- tbb 的 dll 文件需要加到系统 Path 里面。(右键点我的电脑 -> 属性 -> 高级系统设置 -> 环境变量 -> 找到 Path -> 加上 tbb 的 bin 文件夹路径,我的是 *D:\toolkits\tbb43_20140724oss\bin\intel64\vc12*

0 0