Ubuntu版本:Ubuntu 10.04.1 LTS
Opencv版本:opencv-2.4.10
Opencv官网:opencv.org
一、必要准备
1、从opencv.org下载你需要的opencv版本(本文下载的是2.4.10版本);
2、将其解压并通过vmware tools拷贝至ubuntu系统的myworkspace目录(根据各自喜好设定目录);
3、安装依赖包:
需要安装如下的依赖包:
· GCC4.4.x or later
· CMake2.8.9 or higher
· Git
· GTK+2.xor higher, including headers (libgtk2.0-dev)
· pkg-config
· Python2.6 or later and Numpy 1.5 or later with developer packages (python-dev,python-numpy)
· ffmpegor libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
· [optional]libtbb2 libtbb-dev
· [optional]libdc1394 2.x
· [optional]libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
参考自:http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#linux-installation
推荐cmake安装高版本
Cmake高版本安装方法:
#apt-get autoremove cmake #卸载之前的cmake(如果有安装)
到cmake官网下载cmake源码
解压
将解压后的文件拷贝至/usr/
# ln -s /usr/cmake-3.3.2/bin/* /usr/bin/
# cmake –version
cmake version 3.3.2
二、编译opencv
1、 编译opencv
(1)
myworkspace# cd opencv-2.4.10
myworkspace# mkdir release #不知为何要创建这个,本文编译完后release里面并没任何东西
myworkspace# cd release
myworkspace# sudo cmake -DCMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local myworkspace/opencv-2.4.10
myworkspace# cd ../ #参考提供的是在release目录下make,但是release目录下并没有makefile,故退回上级目录
myworkspace# make
myworkspace# sudo make install
(2) 编译会遇到的问题及解决方案
①
opencv-2.4.10/modules/core/src/system.cpp: In function '(staticinitializers for myworkspace/opencv-2.4.10/modules/core/src/system.cpp)': myworkspace/opencv-2.4.10/modules/core/src/system.cpp:280:10: error: inconsistent operandconstraints in an 'asm' make[3]: ***[modules/core/CMakeFiles/opencv_core.dir/src/system.cpp.o] Error 1
解决方案:
在modules/core/src/system.cpp文件中
地278行添加"movl %%ebx,%1\n\t"
将第280行的:"=a"(cpuid_data[0]), "=b"(cpuid_data[1]),"=c"(cpuid_data[2]), "=d"(cpuid_data[3])
替换为:"=a"(cpuid_data[0]),"=r"(cpuid_data[1]), "=c"(cpuid_data[2]),"=d"(cpuid_data[3])
@@ -267,14 +267,17 @@ struct HWFeatures |
|
|
: "cc" |
||
); |
||
#else |
||
+ // We need to preserve ebx since we are compiling PIC code. |
||
+ // This means we cannot use "=b" for the 2nd output register. |
||
asmvolatile |
||
( |
||
"pushl %%ebx\n\t" |
||
"movl $7,%%eax\n\t" |
||
"movl $0,%%ecx\n\t" |
||
"cpuid\n\t" |
||
+ "movl %%ebx,%1\n\t" |
||
"popl %%ebx\n\t" |
||
- : "=a"(cpuid_data[0]), "=b"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) |
||
+ : "=a"(cpuid_data[0]), "=r"(cpuid_data[1]), "=c"(cpuid_data[2]), "=d"(cpuid_data[3]) |
||
: |
||
: "cc" |
||
); |
参考自:
https://github.com/Itseez/opencv/commit/ea50be0529c248961e1b66293f8a9e4b807294a6
②
提示modules/highgui/src/ffmpeg_codecs.hpp或cap_ffmpeg_impl.hpp错误
解决方案:
这需要安装ffmpeg。从网上下载ffmpeg源码,解压到相应的目录
#cd ffmpeg
# ./configure --enable-shared
这时可能会提示yasm nasm版本过旧
#sudo apt-get install yasm nasm #用这个命令解决
#make
#sudo make install
或者最直接的解决办法:在cmake中添加参数-D WITH_FFMPEG=OFF
即
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=OFF CMAKE_INSTALL_PREFIX=/usr/local myworkspace/opencv-2.4.10
参考自:
https://github.com/Itseez/opencv/issues/4940
在编译opencv 3.0 gold时,编译到大约37%时,出现
../../lib/libopencv_core.so.3.0.0:undefined reference to `parallel_pthreads_set_threads_num(int)'
../../lib/libopencv_core.so.3.0.0: undefined reference to`parallel_pthreads_get_threads_num()'
../../lib/libopencv_core.so.3.0.0: undefined reference to`parallel_for_pthreads(cv::Range const&, cv::ParallelLoopBody const&,double)'等好几个错误,困扰了好久,经多次尝试和不懈的坚持下,终于在github上找到了解决方法,现分享如下:
修改modules/core/src/parallel.cpp,添加7处删除5处(+表示要添加,-表示要删除)
132 namespace cv
133 {
134 ParallelLoopBody::~ParallelLoopBody() {}
135 +#if defined HAVE_PTHREADS&& HAVE_PTHREADS
136 + voidparallel_for_pthreads(const cv::Range& range, constcv::ParallelLoopBody& body, double nstripes);
137 + size_tparallel_pthreads_get_threads_num();
138 + voidparallel_pthreads_set_threads_num(int num);
139 +#endif
140 }
141
142 +
143 namespace
144 {
145 #ifdef CV_PARALLEL_FRAMEWORK
.
.
.
307 }
308
309 #elif definedHAVE_PTHREADS
- voidparallel_for_pthreads(const Range& range, const ParallelLoopBody& body,double nstripes);
310 +
311 parallel_for_pthreads(range,body, nstripes);
312
313 #else
.
.
.
367
368 #elif definedHAVE_PTHREADS
369
- size_tparallel_pthreads_get_threads_num();
-
370 returnparallel_pthreads_get_threads_num();
371
372 #else
.
.
.
428
429 #elif definedHAVE_PTHREADS
430
- voidparallel_pthreads_set_threads_num(int num);
-
431 parallel_pthreads_set_threads_num(threads);
432
433 #endif
2、配置opencv参数
在/etc/ld.so.conf.d/opencv.conf文件中加入一行:/usr/local/lib
如果没有opencv.conf这个文件,那就自己创建一个:
sudo vim /etc/ld.so.conf.d/opencv.conf
# sudo ldconfig
# sudo vim /etc/bash.bashrc
在文件最后加入:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
三、测试opencv
#cp –R myworkspace/opencv-2.4.10/samples/c myworkspace
#cd myworkspace/c
# chmod +x build_all.sh
#./build_all.sh
# ./facedetect--cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"--scale=1.5 lena.jpg
结果: