OpenCV 编译安装笔记

编译OpenCV with CUDA

编译方法官方文档写的特别清楚。此外,如果要使用python接口,编译完成后需要把opencv/site-packages目录下的cv2.so链接(或复制)到python/site-packages中,我执行的命令为sudo ln -s /usr/local/opencv3/lib/python2.7/dist-packages/cv2.so /usr/lib/python2.7/dist-packages/cv2.so(如果不清楚可以参照[翻译]Python 2.7 和 Python 3+ 的OpenCV 3.0 安装教程

查看Api和Guides推荐使用Zeal/Dash,就不用那么麻烦跑去官网搜索了:

编译时如果要支持cuda,需要增加几个编译参数,可以参照Build OpenCV with CUDA support。如果您不会用cmake,不知到应该加填什么参数。我建议用图形化的cmake-gui来设置参数。

其中要注意的是 CUDA_GENERATION 的设置。如果选择您GPU内核的型号(PascalMaxwell等),那么就不需要设置CUDA_ARC_BINCUDA_ARC_PTX参数,可是这种条件下会导致一些错误。推荐的方法是设置 CUDA_GENERATION = Auto 然后到CUDA GPUs查看您显卡的Compute Capability,填到CUDA_ARC_BINCUDA_ARC_PTX中。

Bugs:

Ubuntu 16 + OpenCV 3 + Cuda 8.0 编译Bug

编译时会出现如下已知错误error: no default constructor exists for class "thrust::detail::execute_with_allocator<cv::cuda::device::ThrustAllocator, thrust::system::cuda::detail::execute_on_stream_base>"。解决方案为:

  1. 下载https://github.com/thrust/thrustcuda-next-release分支
  2. CUDA\v8.0\include\thrust\ 重命名为 thrust_old
  3. 建立软链接``CUDA\v8.0\include\thrust` 到 ...cuda-next-release\thrust

参考:
  1.OpenCV 3.0 Build Error Cuda 8.0
  2.Opencv 3.1. CUDA 8 compatibility issues
  3.thrust 1.8.3 exclusive_scan with custom temporary allocation, no default constructor error

缺少 ImfChromaticities.h

过了一个星期重新编译的时候发现了一个错误

1
2
3
4
5
6
7
8
9
10
In file included from /home/duchengyao/project/opencv/modules/imgcodecs/src/grfmts.hpp:53:0,
from /home/duchengyao/project/opencv/modules/imgcodecs/src/loadsave.cpp:47:

***/opencv/modules/imgcodecs/src/grfmt_exr.hpp:52:31: fatal error: ImfChromaticities.h: No such file or directory
compilation terminated.
modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/build.make:62: recipe for target 'modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/loadsave.cpp.o' failed
make[2]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/src/loadsave.cpp.o] Error 1
CMakeFiles/Makefile2:4114: recipe for target 'modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all' failed
make[1]: *** [modules/imgcodecs/CMakeFiles/opencv_imgcodecs.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

解决方案:安装libopenexr-dev

OpenCV 2与3共存

参见ubuntu 16.04 编译opencv3.1,opencv多版本切换

上一个教程讲的很清楚,可是作者是在shell中运行OpenCV程序的。如果您正在使用Eclipse调试,那么无法用export来设置环境变量。需要在Eclipse中添加CDT的环境变量,步骤如下:(参照《Eclipse CDT环境下编写、调试、运行C++程序详谈》的后半部分)

  1. 右键点击工程–>Run As–>Run Configurations–>Environment
  2. 添加PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv3/lib/pkgconfig
  3. 添加LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv3/lib

用Eclipse开发还需要记得添加includelib目录,参照Using OpenCV with Eclipse (plugin CDT)

OpenCV 3 Demo

OpenCV 3CUDA模块跟OpenCV2的完全不一样,升级OpenCV3以后,你会发现官方的Hello World for CUDA都跑不起来,因为 OpenCV 3 连GPU类都没了。详细请参见doc.OpenCV3, doc.OpenCV2

Cuda OpenCV 3 的 Demo 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//代码修改自 [OpenCV 3 CUDA小测试](http://blog.csdn.net/zimizile/article/details/47168107)

#include"opencv2/opencv.hpp"
#include"opencv2/cudaimgproc.hpp"
#include<time.h>
#include<iostream>


using namespace std;
using namespace cv;


int main()
{
clock_t t1 = clock();

int num_devices = cv::cuda::getCudaEnabledDeviceCount();

if (num_devices <= 0){
cerr << "There is no device." << endl;
return -1;
}

int enable_device_id = -1;
for (int i = 0; i < num_devices; i++){
cv::cuda::DeviceInfo dev_info(i);
if (dev_info.isCompatible()){
enable_device_id = i;
}
}

if (enable_device_id < 0){
cerr << "GPU module isn't built for GPU" << endl;
}

cv::cuda::setDevice(enable_device_id);

Mat src_image = imread("/home/duchengyao/cuda-workspace/cat.jpg");
Mat dst_image;
cuda::GpuMat d_src_img(src_image); //upload src image to gpu
cuda::GpuMat d_dst_img;
cuda::cvtColor(d_src_img, d_dst_img, CV_BGR2GRAY);
d_dst_img.download(dst_image);
imshow("test", dst_image);

clock_t t2 = clock();

std::cout << "time: " << (t2 - t1) / 1000 << "s" << endl;

waitKey();

return 0;
}