centos6.7 编译安装opencv方法

centos6.7 编译安装opencv方法

由于yum安装opencv版本比较老,有必要通过编译安装较新版本的opencv

安装

#yum 安装依赖包
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.org下载2.4.13版本
wget https://github.com/Itseez/opencv/archive/2.4.13.zip
unzip 2.4.13.zip
cd opencv-2.4.13
cmake CMakeLists.txt
make  
make install

配置opencv

#在/etc/ld.so.conf文件中将所有的/etc/ld.so.conf.d/*.conf文件都包含进来了,所以新建文件
#1.新建文件opencv.conf
vi /etc/ld.so.conf.d/opencv.conf
#2.添加内容
/usr/local/lib
#3.保存后重载配置
sudo ldconfig

增加环境变量

vi /etc/bashrc
#在文件末尾增加环境变量
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig:/usr/lib64/pkgconfig
export PKG_CONFIG_PATH
#保存后执行
source /etc/bashrc

查看版本

pkg-config --modversion opencv
2.4.13

安装opencv-python模块

pip install opencv-python

安装时遇到的问题

  1. /usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC /usr/local/lib/libpython2.7.a: could not read symbols: Bad value 解决办法:重新编译安装python,加上参数--enable-shared CFLAGS=-fPIC

    cd /path/to/python2.7
    #先清除之前的编译文件,如果不清除,编译之后还是无法解决问题
    make clean
    ./configure --enable-shared CFLAGS=-fPIC
    make 
    make install

    重新编译python之后再编译安装opencv

  2. libpython2.7.so.1.0: cannot open shared object file: No such file or directory
    python compare.py 
    python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

    解决办法: 执行上述——增加环境变量步骤.

  3. No module named cv2
    python compare.py 
    Traceback (most recent call last):
    File "compare.py", line 5, in <module>
    import cv2  
    ImportError: No module named cv2

    解决办法:安装opencv-python模块

    pip install opencv-python