OpenCV安装


OpenCV安装

声明:此文是在天下文章一大抄的百度帮助下,完成的;首先说明,本人并非原创,根据大神文章尝试实验,我只想记录一下,安装的过程和问题以及解决方法!如侵犯您的权益请告知

环境:

1. 系统:   ContOS7

2. GCC:    gcc version 4.8.5 (GCC) 及以上均可

3. cmake:  cmake version 2.8.12.2 及以上均可

4. Python:  Python 2.7.11 

安装基本工具:

在安装之前,要先配置好epel源,因为下面的某些安装在系统自带的源中找不到

yum -y install epel-release  

 首先是gcc,g++等这些基本的工具

yum install gcc gcc-c++  
yum install cmake  
yum install python-devel numpy
yum install python-imaging

安装numpy如果出错的朋友,可以通过源码包安装(其它的我是没出现问题)

# 我安装的是1.9.0版本,经检测时可用的(如有版本问题自行下载)
wget http://jaist.dl.sourceforge.net/project/numpy/NumPy/1.9.0/numpy-1.9.0.zip
unzip numpy-1.9.0.zip
cd numpy-1.9.0
python setup.py install

安装OpenCV依赖:

yum install gtk2-devel  
yum install libdc1394-devel  
yum install libv4l-devel  
yum install gstreamer-plugins-base-devel 
yum install gtk*

# ==========虽说我也不太清楚这些依赖包都是干啥的吧!我反正是装上了===============
yum install 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 libtool nasm
# 友情提示:如果出错,可以一个一个装,然后解决依赖,我这里是没有出问题

 yasm

wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar zxvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
./configure --prefix=/usr
make && make install

安装ffmpeg(大坑,我之前已经装好了一套opencv,但是就是因为装ffmpeg的问题,运行的时候各种错误,导致没办法使用),以下为正确姿势

yum install libopencore-amr-devel.x86_64 libopencore-amr.x86_64
yum install libmp3lame.x86_64 libmp3lame-devel.x86_64
yum install libass.x86_64 libass-devel.x86_64
yum install opus.x86_64 opus-devel.x86_64
yum install pulseaudio-libs.x86_64 pulseaudio-libs-devel.x86_64
yum install libsoxr-devel.x86_64 libsoxr.x86_64
yum install speex.x86_64 speex-devel.x86_64
yum install libvorbis-devel.x86_64 libvorbis.x86_64
yum install libvpx.x86_64 libvpx-devel.x86_64
yum install libxvidcore-devel.x86_64 libxvidcore.x86_64

# 以上安装包,建议全部安装上,否则按编译的ffmpeg的时候可能会出现各种缺失

图1安装ffmpeg所需的安装包

下面就开始一个个的安装了!!!(要有耐心,有句mmp不知当讲不当讲)

1、lame

url:https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz/download
tar -zxvf lame-3.100.tar.gz 
cd lame-3.100  
./configure --enable-shared --prefix=/usr  
make  
make install  

2、libogg

url:http://downloads.xiph.org/releases/ogg/libogg-1.1.3.tar.gz
tar -zxvf libogg-1.1.3.tar.gz
cd libogg-1.1.3.tar.gz ./configure --prefix=/usr make && make install

 3、libvorbis

url:http://downloads.xiph.org/releases/vorbis/libvorbis-1.1.2.tar.gz
tar -zxvf libvorbis-1.1.2.tar.gz
cd libvorbis-1.1.2
./configure --prefix=/usr  
make  
make install  

 4、xvid

重点提示:安装版本必须要在1.3.0之上(安装低版本会遇到坑,可自行尝试)

url:http://downloads.xvid.org/downloads/xvidcore-1.3.3.tar.gz
tar zvxf xvidcore-1.3.3.tar.gz  
cd xvidcore-1.3.3/build/generic  
./configure --prefix=/usr  
make  
make install 

 5、x264

url:ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar -jxvf x264-snapshot-20160411-2245.tar.bz2
./configure --prefix=/usr --enable-shared  
make  
make install  

 6、libdts

tar -zxvf libdts-0.0.2.tar.gz  
cd libdts-0.0.2  
./configure --prefix=/usr  
make  
make install  

 7、a52

url:http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
tar -zxvf a52dec-0.7.4.tar.gz
cd a52dec-0.7.4
./configure --prefix=/usr  
make  
make install  

 8、faad2

url:http://downloads.sourceforge.net/faac/faad2-2.7.tar.bz2
tar -jxvf faad2-2.7.tar.bz2
cd faad2-2.7.tar.bz2
./configure --prefix=/usr --with-mp4v2 --enable-shared  
make  
make install   

 9、faac

url:http://downloads.sourceforge.net/faac/faac-1.28.tar.bz2
tar -jxvf faac-1.28.tar.bz2
cd faac-1.28
./bootstrap  
./configure --prefix=/usr  
make   
make install   

 10、amr-nb

url:http://ftp.penguin.cz/pub/users/utx/amr/amrnb-6.1.0.4.tar.bz2
tar -jxvf amrnb-6.1.0.4.tar.bz2
cd amrnb-6.1.0.4
./configure --prefix=/usr  
make  
make install  

 11、amr-wb

url:http://ftp.penguin.cz/pub/users/utx/amr/amrwb-7.0.0.1.tar.bz2
tar -jxvf amrwb-7.0.0.1.tar.bz2
cd amrwb-7.0.0.1
./configure --prefix=/usr  
make  
make install  

 12、lib52

url:http://liba52.sourceforge.net/files/a52dec-0.7.4.tar.gz
tar -zxvf a52dec-0.7.4.tar.gz
cd a52dec-0.7.4
./configure --prefix=/usr  
make  
make install

 这12个安装完成后,就可以尝试安装ffmpeg了

url:http://ffmpeg.org/releases/ffmpeg-3.4.1.tar.bz2
tar -jxvf ffmpeg-3.4.1.tar.bz2
cd ffmpeg-3.4.1
./configure --prefix=/usr --enable-gpl --enable-shared --enable-libmp3lame --enable-libogg --enable-libvorbis --enable-libamr-nb --enable-libamr-wb --enable-libxvid --enable-libx264 --enable-liba52 --enable-liba52bin --enable-libfaac --enable-libfaad --enable-libfaadbin --enable-pp --enable-pthreads --disable-ffserver --disable-ffplay  
make
# 编译时间有点长,过程中可能会出现错误,可以根据错误,定位一下(之前咱们的一些依赖都已经安装了,应该不会出现太多错误了,如有错误,请自行Google)
make install

如果make时,出现一下错误,可根据错误进行安装

错误:ERROR: x265 not found using pkg-config

# 去掉编译参数: --enable-libx265

错误: ERROR: libx264 not found

ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar -jxvf last_x264.tar.bz2
cd last_x264
./configure --enable-static
make && make install

 安装OpenCV

1. 从官网下载 opencv-2.4.13.zip

2.  执行安装

unzip opencv-2.4.13.zip
cd opencv-2.4.13
# 此处大约1分钟,会看到很多打印信息,包括ffmepg的配置信息
cmake CMakeLists.txt
# 编译时间很长(大约30分钟)
make
make install

 安装过程时间可能会有点长10~30分钟左右,编译完成之后OpenCV就安装到了指定的/usr/local下面的一些目录中,库文件就安装到了/usr/local/lib下,Python的模块安装位置是:/usr/local/lib/python2.7/site-packages,执行 ls /usr/local/lib/python2.7/site-packages/ 可以看到两个文件,一个是cv2.so一个是cv.py

3.  复制到python环境中

ln /usr/local/lib/python2.7/site-packages/cv.* /home/zk/python_env/scrapy_env/lib/python2.7/site-packages

4. 验证

#!/usr/local/bin/python
# -*- coding: utf8 -*-

import cv2
import os, sys

BASE_PATH = os.path.dirname(os.path.abspath(__file__))


file_path = os.path.join(BASE_PATH, "fj.jpg")
img = cv2.imread(file_path)
cv2.namedWindow("Image")
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

 终于成功了,浪费整整一天时间!!!好吧,我承认,我比较笨!

参考:http://blog.csdn.net/shine_journey/article/details/69544219

参考:http://blog.csdn.net/guiru/article/details/6023691

参考:http://rickie622.blog.163.com/blog/static/212388112014113125836773