本机的python环境为anaconda附带的python版本:
which python
/home/mvpzhao/anaconda3/bin/python
安装过程主要参考了2篇文章:
B、https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv/
本机采用的方式是Install OpenCV from source
1、Installing the necessary packages-安装一堆依赖包...(注意不需要用apt-ge的方式安装python3了)
sudo apt install build-essential cmake git pkg-config libgtk-3-devsudo
apt install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-devsudo
apt install libjpeg-dev libpng-dev libtiff-dev gfortran openexrsudo
apt install python3-dev python3-numpy libtbb2 libtbb-dev libdc1394-22-dev
2、Cloning the OpenCV source code
mkdir ~/opencv_build && cd ~/opencv_build
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
3、Configuring OpenCV with CMake(git clone之后可用git checkout选择不同的版本号,注意openvc与opencv_contrib的版本号必须一致!)
cd ~/opencv_build/opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_EXTRA_MODULES_PATH=/home/mvpzhao/opencv_build/opencv_contrib/modules \
-DPYTHON_PACKAGES_PATH=/home/mvpzhao/anaconda3/lib/python3.6/site-packages \
-DPYTHON_LIBRARY=/home/mvpzhao/anaconda3/lib/libpython3.6m.so \
-DPYTHON_INCLUDE_DIR=/home/mvpzhao/anaconda3/include/python3.6m \
-DPYTHON_EXECUTABLE=/home/mvpzhao/anaconda3/bin/python3 \
-D BUILD_EXAMPLES=ON ..
4、Compiling OpenCV
make -j12
5、Installing OpenCV
sudo make install
sudo ldconfig
6、verify install
pkg-config --modversion opencv
3.4.4
7、Finish your Python+ OpenCV + Ubuntu 18.04 install
查找到cv2.cpython-36m-x86_64-linux-gnu.so,将他放置到/usr/local/python/cv2/python-3.6路径下并重命名为cv2.so
ll /usr/local/python/cv2/python-3.6
总用量 5536
drwxr-xr-x 2 root root 4096 12月 24 20:25 ./
drwxr-xr-x 3 root root 4096 12月 24 19:03 ../
-rw-r--r-- 1 root root 5660336 12月 24 20:25 cv2.so
8、Our last sub-step is to sym-link our OpenCV cv2.so bindings into our cv environment:
cd ~/anaconda3/lib/python3.6/site-packages
ln -s /usr/local/python/cv2/python-3.6/cv2.so cv2.so
9、Testing your OpenCV 3 install on Ubuntu 18.04
python
Python 3.6.5 |Anaconda, Inc.| (default, Apr 29 2018, 16:14:56)
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.5'