文章目录
  1. 1. 我的系统配置和安装版本
  2. 2. 编译环境准备
  3. 3. 下载OpenCV安装包
  4. 4. 编译安装
  5. 5. 环境变量修改
  6. 6. 测试

今天在编译安装opencv时遇到的一些问题,记录下来。

我的系统配置和安装版本

ubuntu server 12.04 和 redhat 6.4 两台机器
安装opencv 2.4.8

编译环境准备

对于ubuntu的机器,首先安装以下软件:

1
2
3
4
5
6
7
8
# 安装基本编程环境
sudo apt-get install build-essential
# 安装编解码支持库
sudo apt-get install libgtk2.0-dev libavcodec-dev libavformat-dev libtiff4-dev libswscale-dev libjasper-dev
# 安装cmake
sudo apt-get install cmake
# 安装pkg-config
sudo apt-get install pkg-config

对于redhat的机器,使用yum安装以下包:

1
yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp-help-browser zlib-devel libtiff-devel libjpeg-devel libpng-devel gstreamer-devel libavc1394-devel libraw1394-devel libdc1394-devel jasper-devel jasper-utils swig python libtool nasm

下载OpenCV安装包

官网下载OpenCV的安装包,我下载的是2.4.8的版本,但这个版本我安装的时候有bug,后面会介绍。(建议下载2.4.11及以上版本,安装步骤基本类似)

下载后解压,进入目录

1
2
3
unzip -v opencv-2.4.8.zip
#进入解压后的目录
cd opencv-2.4.8

编译安装

直接在当前目录下编译即可,并不一定要建立子目录

1
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .

make

1
2
make -j8    #(-j8表示使用8个线程编译,可以大大加快编译速度)
sudo make install

我主要在此处遇到了问题,因为我的电脑安装了GPU卡,因此OpenCV会自动编译CUDA相关的代码,可以直接关掉( 在cmake时加上-D WITH_CUDA=OFF ),但我需要编译。因此默认是打开的,因此在make时遇到以下错误:a storage class is not allowed in an explicit specialization

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
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(51): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(52): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(53): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(54): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(55): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(56): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(57): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(58): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(61): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(62): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(63): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(64): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(65): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(66): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(67): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(68): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(119): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(120): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(121): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(122): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(123): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(124): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(125): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(126): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(127): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(128): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(129): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(130): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(131): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(132): error: a storage class is not allowed in an explicit specialization
/root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp(133): error: a storage class is not allowed in an explicit specialization
31 errors detected in the compilation of "/tmp/tmpxft_00006af4_00000000-9_NCVPyramid.compute_35.cpp1.ii".
CMake Error at cuda_compile_generated_NCVPyramid.cu.o.cmake:266 (message):
Error generating file
/root/opencv-2.4.8/modules/gpu/CMakeFiles/cuda_compile.dir/src/nvidia/core/./cuda_compile_generated_NCVPyramid.cu.o

make[2]: *** [modules/gpu/CMakeFiles/cuda_compile.dir/src/nvidia/core/./cuda_compile_generated_NCVPyramid.cu.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [modules/gpu/CMakeFiles/opencv_gpu.dir/all] Error 2
make: *** [all] Error 2

我安装的CUDA是6.5版本,计算能力是3.5,针对以上错误,我找到的说在cmake时加上以上参数的方法都是没用:

1
2
3
-D CUDA_GENERATION=Auto

-D CUDA_GENERATION="" -D CUDA_ARCH_BIN=2.0(或3.5,5.0)

最后找的解决办法是:
打开错误的文件:

1
2
# 注意修改为你的编译路径
vim /root/opencv-2.4.8/modules/gpu/src/nvidia/core/NCVPixelOperations.hpp

将其中template声明中的所有static全部删除。可以使用vim的全局替换,或者其他编辑工具全局替换即可(template<> static inline 替换为template<> inline ):

1
:%s/template<> static inline/template<> inline/g

这个问题的原因是OpenCV 2.4.8 与CUDA 6.5不兼容的一个bug(好像2.4系列都有问题),已在后续版本中修复,参考http://code.opencv.org/issues/3814

修改过后重新执行以下,切记有多核的一定使用多线程,编译CUDA慢的要哭:

1
2
3
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .
make -j8 #(-j8表示使用8个线程编译,可以大大加快编译速度)
sudo make install

环境变量修改

在/etc/ld.so.conf 文件中添加 /usr/local/lib , 然后执行

1
sudo ldconfig -v

为程序指定openvc的头文件位置, 使用pkg-config命令来完成。首先在 /etc/profile 中添加

1
export  PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

然后source生效

1
source /etc/profile

再执行以下命令可以打印opencv的配置信息

1
pkg-config --libs opencv

1
2
root@ubuntu:~# pkg-config  --libs opencv
/usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_ocl.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_superres.so /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videostab.so /usr/local/cuda/lib64/libcufft.so /usr/local/cuda/lib64/libnpps.so /usr/local/cuda/lib64/libnppi.so /usr/local/cuda/lib64/libnppc.so /usr/local/cuda/lib64/libcudart.so -lrt -lpthread -lm -ldl

测试

在解压的opencv目录下找到 samples/c/build_all.sh,运行该文件

1
2
3
cd /root/opencv-2.4.8/samples/c/
chmod a+x build_all.sh
./build_all.sh

选取生成的可执行文件进行测试,比如find_obg:

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
root@ubuntu:~/opencv-2.4.8/samples/c# ls
adaptiveskindetector delaunay mser_sample
adaptiveskindetector.cpp delaunay.c mser_sample.cpp
agaricus-lepiota.data example_cmake mushroom
airplane.jpg facedetect mushroom.cpp
baboon200.jpg facedetect.cmd one_way_sample
baboon200_rotated.jpg facedetect.cpp one_way_sample.cpp
baboon.jpg fback_c one_way_train_0000.jpg
bgfg_codebook fback_c.c one_way_train_0001.jpg
bgfg_codebook.cpp find_obj one_way_train_images.txt
blobtrack_sample find_obj_calonder polar_transforms
blobtrack_sample.cpp find_obj_calonder.cpp polar_transforms.c
box_in_scene.png find_obj.cpp puzzle.png
box.png find_obj_ferns pyramid_segmentation
build_all.sh find_obj_ferns.cpp pyramid_segmentation.c
calonder_params.xml fruits.jpg scene_l.bmp
cat.jpg intrinsics.yml scene_r.bmp
cat.xml JCB.png smiledetect
CMakeLists.txt latentsvmdetect smiledetect.cpp
contours latentsvmdetect.cpp stuff.jpg
contours.c lena.jpg tree.avi
convert_cascade morphology tree_engine
convert_cascade.c morphology.c tree_engine.cpp
cvsample.dsp motempl waveform.data
cvsample.vs2005.vcproj motempl.c

1
./find_obj

安装结束。






转载请标明文章出处。本文内容为实践后的原创整理,如果侵犯了您的版权,请联系我进行删除,邮件:yanhealth@163.com

参考资料:
[1] http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
[2] http://blog.csdn.net/surgewong/article/details/39078251
[3] http://blog.csdn.net/xuejiren/article/details/24347555

文章目录
  1. 1. 我的系统配置和安装版本
  2. 2. 编译环境准备
  3. 3. 下载OpenCV安装包
  4. 4. 编译安装
  5. 5. 环境变量修改
  6. 6. 测试