ubuntu下opencv2.4.9/3.0/3.1安装过程

2016年5月3日 由 Creater 留言 »

安装OpenCv后出现各种问题,究其原因是一些基础库没有安装好,以下是基础库安装步骤,保证尽量完整,免得在使用的时候出错。

  • 1.由于opencv的cmakelist里依赖一些库,所有要先配置好这些库,先去除你电脑上的这些重新装.
  •  apt-get remove ffmpeg x264 libx264-dev
  • 2.安装其他依赖库.
  •  apt-get update
     apt-get install build-essential checkinstall Git cmake libfaac-dev libjack-jackd2-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libsdl1.2-dev libtheora-dev libva-dev libvdpau-dev libvorbis-dev libx11-dev libxfixes-dev libxvidcore-dev texi2html yasm zlib1g-dev
  • 3.安装gstreamer,主要是opencv里头的相机模块用到
  •   apt-get install libgstreamer0.10-0 libgstreamer0.10-dev gstreamer0.10-tools gstreamer0.10-plugins-base libgstreamer-plugins-base0.10-dev gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-ffmpeg

    如果 gstreamer0.10-ffmpeg提示没有候选,那么手动下载并安装,或者按如下步骤安装

     apt-get install libgstreamer0.10-dev gstreamer-tools gstreamer0.10-tools gstreamer0.10-doc
    apt-get install gstreamer0.10-plugins-base gstreamer0.10-plugins-good gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad gstreamer0.10-plugins-bad-multiverse  

    所有您需要做的就是添加PPA到您的系统,更新本地存储索引和安装gstreamero.10-ffmpeg包。如下输入命令:

     add-apt-repository ppa:mc3man/trusty-media
    apt-get update
    apt-get install gstreamer0.10-ffmpeg
  • 4.安装gtk,jpeg8
  •  apt-get install libgtk2.0-0 libgtk2.0-dev
    apt-get install libjpeg8 libjpeg8-dev
  • 5.安装 install x264.
  • 下载链接: ftp://ftp.videolan.org/pub/videolan/x264/snapshots

     tar xvf x264-snapshot-**********-stable.tar.bz2
    cd x264-snapshot--**********--stable
    ./configure --enable-shared --enable-pic
    make
     make install

    由于我的电脑是64位,所以要加上 –enable-shared –enable-pic ,否则就会报错:

        /usr/bin/ld: /usr/local/lib/libavcodec.a(avpacket.o): relocation R_X86_64_32S against `av_destruct_packet' can not be used when making a shared object; recompile with -fPIC
        /usr/local/lib/libavcodec.a: could not read symbols: Bad value
  • 6.安装ffmpeg
  • 如果出现下面错误:

     /usr/local/lib/libswscale.a(swscale_unscaled.o): relocation R_X86_64_32S against `av_pix_fmt_descriptors@@LIBAVUTIL_52' can not be used when making a shared object; recompile with -fPIC
    /usr/local/lib/libswscale.a: error adding symbols: 错误的值
    collect2: error: ld returned 1 exit status
    make[2]: *** [lib/libopencv_highgui.so.2.4.9] 错误 1
    make[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] 错误 2
    make: *** [all] 错误 2

    可能是opencv249依赖的ffmpeg版本不对照

     wget http://ffmpeg.org/releases/ffmpeg-1.2.tar.bz2
    tar xvf ffmpeg-1.2.tar.bz2
    cd ffmpeg-1.2
    ./configure --enable-gpl --enable-libfaac --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvorbis --enable-libx264 --enable-libxvid --enable-nonfree --enable-postproc --enable-version3 --enable-x11grab --enable-shared --enable-pic
    make
    make install
  • 7.安装v4l (video for linux) , http://www.linuxtv.org/downloads/v4l-utils/.
  • 报错:dvb-v5.h:69:9: error: ‘NULL’ undeclared here ……..原因是v4l的版本不对,于是我装了v4l 1.0.0

     wget http://www.linuxtv.org/downloads/v4l-utils/v4l-utils-1.0.0.tar.bz2
    tar xvf v4l-utils-1.0.0.tar.bz2
    cd v4l-utils1.0.0
    make
    make install
  • 8.安装OpenCV 2.4.9.
  •  tar xvf OpenCV-2.4.9.tar.bz2
    cd OpenCV-2.4.9/
    mkdir build
    cd build
    cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON  -D WITH_OPENGL=ON ..
    make
    make install

    -D WITH_QT=ON选项就别加上了,免得在Qt中用不了
    环境变量中添加

     export LD_LIBRARY_PATH=/usr/local/lib

    另外修改

     sudo vi /etc/ld.so.conf.d/opencv.conf

    追加 /usr/local/lib
    执行

     sudo ldconfig /etc/ld.so.conf

    在/etc/bash.bashrc添加

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

    最后查看版本和头文件目录

     pkg-config --modversion opencv
    pkg-config --cflags opencv 

如果准备在Qt Creator中写代码,关注下一篇文章:Linux下OpenCv+Qt搭配使用问题

广告位

发表评论

你必须 登陆 方可发表评论.