基于ubuntu16.04和python3.5安装opencv3
话说opencv在linux下真是神装,各种bug时不时会出来给你个惊喜,让你爽一阵儿…下面进入正文,主要操作来自于官网:
1. 首先安装相关依赖:
1 sudo apt-get update 2 #downloading the required packages to compile OpenCV 3 sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
2. 安装python3.5的dev包:
根据你所使用的python版本进行调整,比如你用的python3.6,则把下面涉及到python3.5的替换为python3.6即可。
1 sudo apt-get install python3.5-dev
3. 把python3.5的dev文件复制到相应位置:
如果你在使用python3.2及其以下版本,则跳过步骤3:
1 python3.5-config --includes 2 3 #输入以上命令,如果有以下输出(1),则继续执行下面紧接着的一条命令;但如果有以下输出(2),则掠过下面所谓复制命令: 4 #(1)-I/usr/include/python3.5m -I/usr/include/x86_64-linux-gnu/python3.5m 5 #(2)-I/usr/include/python3.5m -I/usr/include/python3.5m 6 7 sudo cp /usr/include/x86_64-linux-gnu/python3.5m/pyconfig.h /usr/include/python3.5m/
4. 下载opencv源码包并编译安装:
1 mkdir OpenCV-tmp 2 cd OpenCV-tmp 3 git clone https://github.com/Itseez/opencv.git
接着把 /OpenCV-tmp 下的opencv文件夹名字改为opencv-3,并执行以下编译安装命令:
1 mkdir build & cd build 2 cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ../opencv-3 3 make -j $(nproc --all) 4 sudo make install
make -j12过程很慢!
5. make过程中出现以下"not found"错误:
/usr/bin/ld: warning: libpng16.so.16, needed by //home/xy/anaconda3/lib/libcairo.so.2, not found (try using -rpath or -rpath-link)
解决办法,然后重新编译即可:
1 cd /usr/lib/x86_64-linux-gnu 2 sudo ln -s ~/anaconda3/lib/libpng16.so.16 libpng16.so.16 3 sudo ldconfig
6. 验证安装:
1 python3 #进入python3环境,导入cv2模块,有以下输出 2 >>> import cv2 3 >>> cv2.__version__ 4 '3.1.0-dev'
7. 附带安装python常用库:
1 sudo apt-get install python3-numpy 2 sudo apt-get install python3-matplotlib
8. 在python3环境下import cv2时候出现BUG:
1 nuc@xy:~$ python 2 Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 3 [GCC 7.2.0] on linux 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> import cv2 6 Traceback (most recent call last): 7 File "<stdin>", line 1, in <module> 8 ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
方法1:在import cv2之前加入以下操作:
1 import sys 2 ros_path = '/opt/ros/kinetic/lib/python2.7/dist-packages' 3 4 if ros_path in sys.path: 5 sys.path.remove(ros_path) 6 7 import cv2 8 9 sys.path.append('/opt/ros/kinetic/lib/python2.7/dist-packages')
方法2:该方法比较麻烦,一旦操作了ros即出问题
1 gedit ~/.bashrc 2 # 用注释掉有关ROS的source语句 3 #source /opt/ros/kinetic/setup.bash 4 source ~/.bashrc
打开一个新的终端,执行之前的方法即可
方法3:给cv2.so换个名字
1 cd /opt/ros/kinetic/lib/python2.7/dist-packages/ 2 sudo mv cv2.so cv2_ros.so
若此时依旧不能解决问题,出现以下ModuleNotFoundError: No module named ‘cv2’:
1 nuc@xy:~$ python 2 Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56) 3 [GCC 7.2.0] on linux 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> import sys 6 >>> ros_path = '/opt/ros/kinetic/lib/python2.7/dist-packages' 7 >>> if ros_path in sys.path: 8 ... sys.path.remove(ros_path) 9 ... 10 >>> import cv2 11 Traceback (most recent call last): 12 File "<stdin>", line 1, in <module> 13 ModuleNotFoundError: No module named 'cv2'
此时,你可能需要装两个东西:
1 sudo apt-get install python-opencv 2 pip install opencv-python
然后再import cv2试一下即可解决。