之前实现了在 Ubuntu 上通过 opencv-python 使用 Camera - Python OpenCV 在Ubuntu虚拟机上使用Camera。当时的 opencv-python 版本是 3.x

现在需要使用 2.x 版本的 opencv-python,通过源码编译的方式进行


参考:

Installation in Linux

Installation in Linux

Installing OpenCV from source on Anaconda Python on Ubuntu 16.10


主要内容:

  1. 依赖包配置
  2. OpenCV 源码下载
  3. 源码配置/编译/安装
  4. 问题与解决

依赖包配置

[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

OpenCV 源码下载

首先在 github 上下载 opencv 源码

cd ~/<my_working _directory>
git clone https://github.com/opencv/opencv.git

切换到 2.4 版本

git checkout -t origin/2.4

源码配置/编译/安装

之前在依赖包配置中已安装好 cmake,如果想要通过图形化界面配置

sudo apt-get install cmake-qt-gui

命令行配置如下:

cd ~/<my_working _directory>/opencv/  
mkdir build  
mkdir install
cd build  
cmake -D CMAKE_BUILD_TYPE=RELEASE \  
    -D CMAKE_INSTALL_PREFIX=../install \  
    -D PYTHON_EXECUTABLE=<anaconda_work_dir>/envs/<environment>/bin/python \  
    -D PYTHON_LIBRARY=<anaconda_work_dir>/envs/<environment>/lib/python2.7 \  
    -D PYTHON_INCLUDE_DIR=<anaconda_work_dir>/envs/<environment>/include/python2.7 \  
    ..

因为我用的是 anaconda,所以将 python 相关的路径均改为对应 anaconda 路径

完成后进行编译与安装

make
sudo make install

>>> import cv2
>>> cv2.__version__
'2.4.13.7'
>>>

问题与解决

问题描述:

[ 28%] Building CXX object modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/test/test_contours.cpp.o
[ 28%] Building CXX object modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/test/test_boundingrect.cpp.o
[ 28%] Linking CXX executable ../../bin/opencv_test_imgproc
/usr/bin/ld: warning: libiconv.so.2, needed by //home/zhujian/software/anaconda/anaconda3/envs/openface/lib/libgobject-2.0.so.0, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libpcre.so.1, needed by //home/zhujian/software/anaconda/anaconda3/envs/openface/lib/libgobject-2.0.so.0, not found (try using -rpath or -rpath-link)
//home/zhujian/software/anaconda/anaconda3/envs/openface/lib/libglib-2.0.so.0: undefined reference to `libiconv_open'
//home/zhujian/software/anaconda/anaconda3/envs/openface/lib/libglib-2.0.so.0: undefined reference to `libiconv_close'
//home/zhujian/software/anaconda/anaconda3/envs/openface/lib/libglib-2.0.so.0: undefined reference to `libiconv'
collect2: error: ld returned 1 exit status
modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/build.make:674: recipe for target 'bin/opencv_test_imgproc' failed
make[2]: *** [bin/opencv_test_imgproc] Error 1
CMakeFiles/Makefile2:1741: recipe for target 'modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all' failed
make[1]: *** [modules/imgproc/CMakeFiles/opencv_test_imgproc.dir/all] Error 2
Makefile:160: recipe for target 'all' failed
make: *** [all] Error 2

参考:opencv-2.4.13.4在ubuntu16.04安装遇到的一些问题

实际上,在 /home/zhujian/software/anaconda/anaconda3/envs/openface/lib/ 路径下是存在 libiconv.so.2libpcre.so.1

/etc/ld.so.conf.d 路径下新建文件 opencv.conf,添加库文件路径

cd /etc/ld.so.conf.d
sudo gedit opencv.conf

//添加库路径,换成你自己的
/home/zhujian/software/anaconda/anaconda3/envs/openface/lib

再次 cmake,成功

问题描述:

安装完成后,没有在 python 环境中发现 cv2,并且发现 cv2.so cv.py 文件在 ~/<my_working _directory>/opencv/install/lib/python2.7/site-packages 中

解决:

将 site-packages 文件夹中文件复制到 <anaconda_dir>/envs/py27/lib/python2.7/site-packages 
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐