Linux下配置OpenCV1.0环境

时间:2022-01-27 01:37:36

自己一直嚷嚷着打算学学图像识别,识别个简单的,车牌号,验证码之类的,之前查过资料,OpenCV可以实现。昨天花了一个下午终于配置好环境了,今天写下总结。

OpenCV这一名称包含了Open和Computer Vision两者的意思。实际上,Open指Open Source(开源,即开放源代码),Computer Vision则指计算机视觉。更详细介绍,请参考:http://zh.wikipedia.org/wiki/OpenCV

配置环境系统信息:Linux

catgenie@debian:~$ cat /etc/debian_version
7.6
genie@debian:~$ cat /proc/version
Linux version 3.2.0-4-amd64 (debian-kernel@lists.debian.org) (gcc version 4.6.3 (Debian 4.6.3-14) ) #1 SMP Debian 3.2.60-1+deb7u1
genie@debian:~$

OpenCV版本:1.0

为何选择1.0进行安装?

由于我想实现的功能比较简单,简单查资料发现最新的OpenCV2.4.9版本中添加了C++的库,我是C++渣渣,手里的书又都是1.0版本的,比较老,所以果断安装版本选择了1.0.

为何选择在Linux上安装?

在Windows上安装,有很全的资料和教程,但版本已经妥协到1.0了,正好Linux上安装业忘得差不多了,再者,个人认为,在Linux下安装成功,Windows上应该也可以,反之,未必,所以选择在Linux上安装。

好了,废话不多说,开始安装。(为了节约时间,强烈建议先看完教程再安装)

第一步:获取安装包

到http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/1.0/ 下载opencv-1.0.0.tar.gz源代码包或者到http://opencv.org/downloads.html的最下方下载

第二步:解压

首先请切换到root用户,然后,找到下载的位置

#cd /home/YOUR_USER_NAME/Downloads/

#tar -zxvf opencv-1.0.0.tar.gz

这个时候,在当前目录下会出现一个名为:opencv-1.0.0的directory

#cd opencv-1.0.0

第三步:执行./configure

#./configure

执行这条命令后,我并不顺利,报了一些错误:

提示你安装一些你需要的库,我安装了两个,就没报错了

#apt-get install g++

#apt-get install pkg-config

第四步:执行./configure,若出现以下信息,表示成功:

#./configure

General configuration ================================================
Compiler: g++
CXXFLAGS: -Wall -fno-rtti -pipe -O3 -fomit-frame-pointer

Install path: /usr/local

HighGUI configuration ================================================

Windowing system --------------
Use Carbon / Mac OS X: no
Use gtk+ 2.x: yes
Use gthread: yes

Image I/O ---------------------
Use libjpeg: yes
Use zlib: yes
Use libpng: yes
Use libtiff: yes
Use libjasper: yes
Use libIlmImf: no

Video I/O ---------------------
Use QuickTime / Mac OS X: no
Use xine: no
Use ffmpeg: no
Use dc1394 & raw1394: no
Use v4l: no
Use v4l2: yes

Wrappers for other languages =========================================
SWIG
Python no

Additional build settings ============================================
Build demo apps yes

Now run make ...

第五步:运行make

#make

make后出现以下错误信息:

../../cxcore/include/cxmisc.h:133:6: error : #elif with no expression

原因分析:
GCC 4.4 will introduce better checks in the preprocessor. The problem
is pretty obvious: you're using a #elif without any condition when
you really want a #else.

You can reproduce this problem with gcc-snapshot from unstable.

> Automatic build of opencv_1.0.0-6.1 on em64t by sbuild/amd64 0.53
...
> /bin/sh ../../libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I../.. -I. -I../../cxcore/include -I../.. -DNDEBUG -fno-strict-aliasing -Wall -g -fopenmp -O2 -g -MT cxalloc.lo -MD -MP -MF .deps/cxalloc.Tpo -c -o cxalloc.lo cxalloc.cpp
> g++ -DHAVE_CONFIG_H -I. -I../.. -I. -I../../cxcore/include -I../.. -DNDEBUG -fno-strict-aliasing -Wall -g -fopenmp -O2 -g -MT cxalloc.lo -MD -MP -MF .deps/cxalloc.Tpo -c cxalloc.cpp -fPIC -DPIC -o .libs/cxalloc.o
> In file included from _cxcore.h:60,
> from cxalloc.cpp:42:
> ../../cxcore/include/cxmisc.h:133:6: error: #elif with no expression
> make[4]: *** [cxalloc.lo] Error 1
> make[4]: Leaving directory `/build/tbm/opencv-1.0.0/cxcore/src'
> make[3]: *** [all-recursive] Error 1

解决方法:

解决办法:

--- cxcore/include/cxmisc.h~ 2008-11-07 15:12:58.000000000 +0000 (删除)
+++ cxcore/include/cxmisc.h 2008-11-07 15:13:03.000000000 +0000 (打开)
@@ -130,7 +130,7 @@
#include <alloca.h>
#elif defined HAVE_ALLOCA
#include <stdlib.h>
-#elif
+#else
#error
#endif

以上的意思就是:把 该文件的#elif 改为 #else后编译成功!!!

第六步:执行./configure,make

#./configure

#make

提示以下错误:

g++ -Wall -fno-rtti -pipe -O3 -fomit-frame-pointer -o .libs/opencv-haartraining haartraining.o libcvhaartraining.a ../../../otherlibs/highgui/.libs/libhighgui.so /home/genie/Downloads/opencv-1.0.0/cv/src/.libs/libcv.so ../../../cv/src/.libs/libcv.so /home/genie/Downloads/opencv-1.0.0/cxcore/src/.libs/libcxcore.so ../../../cxcore/src/.libs/libcxcore.so -lpthread -ldl -lm
../../../otherlibs/highgui/.libs/libhighgui.so: undefined reference to `cvCaptureFromCAM_V4L(int)'
collect2: error: ld returned 1 exit status
make[4]: *** [opencv-haartraining] Error 1
make[4]: Leaving directory `/home/genie/Downloads/opencv-1.0.0/apps/haartraining/src'
make[3]: *** [all-recursive] Error 1
make[3]: Leaving directory `/home/genie/Downloads/opencv-1.0.0/apps/haartraining'
make[2]: *** [all-recursive] Error 1
make[2]: Leaving directory `/home/genie/Downloads/opencv-1.0.0/apps'
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory `/home/genie/Downloads/opencv-1.0.0'
make: *** [all] Error 2

解决方法:https://code.ros.org/trac/opencv/changeset/5206,可能会有的部分文件目录不同,但内容相同

解决方法:https://code.ros.org/trac/opencv/changeset/5206,可能会有的部分文件目录不同,但内容相同

Unmodified:other
Added:green part
Removed:red part

TabularUnified branches/2.2/opencv/cnconfig.h.cmake

旧行号   新行号
19       19 /*V4L2 capturing support */
20   20 #cmakedefine HAVE_CAMV4L2
    21
    22 /* V4L/V4L2 capturing support via libv4l */
    23 #cmakedefine HAVE_LIBV4L
21   24
22   25 /* Carbon windowing environment */

TabularUnified branches/2.2/opencv/modules/highgui/src/cap.cpp
172   172     return capture;
173   173 #endif
174        #if defined (HAVE_CAMV4L)||defined (HAVE_CAMV4L2)
     174 #if defined HAVE_LIBV4L || (defined (HAVE_CAMV4L) && defined (HAVE_CAMV4L2))   
175   175 capture = cvCreateCameraCapture_V4L(index);
176   176 if(capture)

TabularUnified branches/2.2/opencv/modules/highgui/src/cap_libv4l.cpp
225   225 #include "precomp.hpp"
226   226
227     #if !defined WIN32 && defined HAVE_CAMV4L && defined HAVE_CAMV4L2
    227 #if !defined WIN32 && defined HAVE_LIBV4L
228 228
229 229 #defined CLEAR(x) memset(&(x), 0, sizeof(x))
.... ...
242 242 #include <sys/ioctl.h>
243 243
   244 #ifdef HAVE_CAMV4L
244 245 #include <linux/videodev.h>
   246 #endif
   247 #ifdef HAVE_CAMV4L2
245 248 #include <linux/videodev2.h>
  249 #endif
246 250
247 251 #include <libv4l1.h>

第7步:添加库
先做系统更新

#apt-get update

#apt-get upgrade

再执行

#apt-cache search opencv

后显示如下信息

libcv-dev - Translation package for libcv-dev
libcv2.3 - computer vision library - libcv* translation package
libcvaux-dev - Translation package for libcvaux-dev
libcvaux2.3 - computer vision library - libcvaux translation package
libhighgui-dev - Translation package for libhighgui-dev
libhighgui2.3 - computer vision library - libhighgui translation package
libopencv-calib3d-dev - development files for libopencv-calib3d
libopencv-calib3d2.3 - computer vision Camera Calibration library
libopencv-contrib-dev - development files for libopencv-contrib
libopencv-contrib2.3 - computer vision contrib library
libopencv-core-dev - development files for libopencv-core
libopencv-core2.3 - computer vision core library
libopencv-dev - development files for opencv
libopencv-features2d-dev - development files for libopencv-features2d
libopencv-features2d2.3 - computer vision Feature Detection and Descriptor Extraction library
libopencv-flann-dev - development files for libopencv-flann
libopencv-flann2.3 - computer vision Clustering and Search in Multi-Dimensional spaces library
libopencv-gpu-dev - development files for libopencv-gpu
libopencv-gpu2.3 - computer vision GPU Processing library
libopencv-highgui-dev - development files for libopencv-highgui
libopencv-highgui2.3 - computer vision High-level GUI and Media I/O library
libopencv-imgproc-dev - development files for libopencv-imgproc
libopencv-imgproc2.3 - computer vision Image Processing library
libopencv-legacy-dev - development files for libopencv-legacy
libopencv-legacy2.3 - computer vision legacy library
libopencv-ml-dev - development files for libopencv-ml
libopencv-ml2.3 - computer vision Machine Learning library
libopencv-objdetect-dev - development files for libopencv-objdetect
libopencv-objdetect2.3 - computer vision Object Detection library
libopencv-video-dev - development files for libopencv-video
libopencv-video2.3 - computer vision Video analysis library
opencv-doc - OpenCV documentation and examples
python-opencv - Python bindings for the computer vision library

再执行:以下命令,根据你自己上面的list做出相应替换,可能有些库会用不到,但为了防止以后出问题,我就全装了。

#apt-get install libcvaux-dev libcvaux2.3 libhighgui-dev libhighgui2.3 libopencv-calib3d-dev libopencv-calib3d2.3 libopencv-contrib-dev libopencv-contrib2.3 libopencv-core-dev libopencv-core2.3 libopencv-dev libopencv-features2d-dev libopencv-features2d2.3 libopencv-flann-dev libopencv-flann2.3 libopencv-gpu-dev libopencv-gpu2.3 libopencv-highgui-dev libopencv-highgui2.3 libopencv-imgproc-dev libopencv-imgproc2.3 libopencv-legacy-dev libopencv-legacy2.3 libopencv-ml-dev libopencv-ml2.3 libopencv-objdetect-dev libopencv-objdetect2.3 libopencv-video-dev libopencv-video2.3 opencv-doc python-opencv

第8步:重新执行./configure和make

#./configure

#make

第9步:我当时基本就没有Error了,然后执行make install,如果你们有错误,欢迎评论告知

第10步:更新动态链接库

#vim /etc/ld.so.conf

在最后一行添加:/usr/local/lib到文件中

执行以下命令:

#ldconfig

至此:安装结束。

验证安装是否成功:

进入到sample/c下

通过

#ls

可以看到有drawing.c这个文件

执行如下命令

# g++ drawing.c -o drawing `pkg-config --cflags --libs opencv`

执行./drawing

会提示错误:OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support.

原因:简单的解释就是你的GTK+ 2.x要先于OpenCV安装

所以,重新安装gtk+ 2.x,然后重执行

#./configure

#make,

#make install

#make check(检查是否安装全部正确)

再用验证阶段的g++编译,

再执行

#./drawing

就出现了动画效果了。

感谢以下链接及文献,帮我解决了很多问题:

https://code.ros.org/trac/opencv/changeset/5206

http://blog.csdn.net/loveaborn/article/details/7647734

http://www.raben.com/content/opencv-installation-ubuntu-1204

http://opencv.wikispaces.com/1)+Installation+on+Linux

http://blog.163.com/dingmz_frcmyblog/blog/static/217304023201321310532566/

http://bbs.ednchina.com/BLOG_ARTICLE_277614.HTM

http://www.arm9home.net/read.php?tid-15163.html

http://www.arm9home.net/simple/index.php?t15163.html

http://www.arm9home.net/read.php?tid-80418.html

http://www.arm9home.net/read.php?tid-8386.html

《OpenCV教程基础篇》

www.google.com.hk

www.baidu.com

Linux下配置OpenCV1.0环境

Linux下配置OpenCV1.0环境

Linux下配置OpenCV1.0环境

Linux下配置OpenCV1.0环境

Linux下配置OpenCV1.0环境

Linux下配置OpenCV1.0环境