最初に

コレは2、3年ほど前に自分がPythonからOpenCVをLinux上で扱うために調査した結果をローカルで纏めていたものを公開したものです。
苦手な英語で書いているので文章がおかしなところが有ると思いますがご容赦下さい。

Pythonの場合、pipからOpenCVをインストールできますが、OpenCV公式ではありません。
そのデメリットを理解した上で利用する事自体は問題ありませんが、なにか有った際に自分で公式の手順、ソースでビルドできるようになっておきたかったので調査した内容になります。

  • まっさらな環境でPythonとOpenCVを準備する方法(Dockerコンテナで実施)
  • 自分の端末(Linux Mint)上でpyenvを使ってPythonをインストールして、その上でOpenCVをビルドする

という2つの方法を纏めています。
注意ですが、pyenvでPythonをインストールするのはあまりお勧めできません。
Pythonはソースからビルドしてインストールするのが王道です。(個人の意見)

以下で、Docker上にPythonをインストールしている方の内容でも分かるとは思いますが、記事既存のPython環境を壊すことなく、自分でビルドしてインストールする(altinstall)でも自分でPythonをビルドしてインストールする方法を説明していますので参考にして下さい。

Build OpenCV

This document explains how to build and install OpenCV for Python.
Use clean docker container to explain about that.

Build on docker

sudo docker run --name opencv-work -itd ubuntu:bionic bash
sudo docker exec -it opencv-work bash

Install libraries need to build OpenCV

Python 3.7

Default Python3 is 3.6 series by Ubuntu 18.04 .

apt update

apt install wget libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavresample-dev ffmpeg build-essential cmake unzip pkg-config libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev python3.7-dev python3.7-venv vim

OpenCV checks python3 command to build.
Default python3 is 3.6. Therefore it should be changed to 3.7.

root@af02b531ce01:/usr/bin# rm /usr/bin/python3
root@af02b531ce01:/usr/bin# ln -s /usr/bin/python3.7 /usr/bin/python3
root@6eff8c7e2c04:~# python3 --version
Python 3.7.3
root@6eff8c7e2c04:~# 

Create directories

root@6eff8c7e2c04:~# mkdir -p /root/opencv_work/out  
root@6eff8c7e2c04:~# mkdir -p /root/opencv_work/src
root@6eff8c7e2c04:~# cd /root/opencv_work/
root@6eff8c7e2c04:~/opencv_work# 

Create virtual environments and install numpy

root@6eff8c7e2c04:~/opencv_work# pwd
/root/opencv_work
root@6eff8c7e2c04:~/opencv_work# python3 -m venv venv
root@6eff8c7e2c04:~/opencv_work# source venv/bin/activate
(venv) root@6eff8c7e2c04:~/opencv_work# pip install numpy
Collecting numpy
  Downloading https://files.pythonhosted.org/packages/05/4b/55cfbfd3e5e85016eeef9f21c0ec809d978706a0d60b62cc28aeec8c792f/numpy-1.17.0-cp37-cp37m-manylinux1_x86_64.whl (20.3MB)
    100% |████████████████████████████████| 20.3MB 84kB/s 
Installing collected packages: numpy
Successfully installed numpy-1.17.0
(venv) root@6eff8c7e2c04:~/opencv_work# 

Download OpenCV and OpenCV contribution

NOTICE: You have to work with activated venv.

(venv) root@6eff8c7e2c04:~/opencv_work/src# pwd
/root/opencv_work/src
(venv) root@6eff8c7e2c04:~/opencv_work/src# 
(venv) [koji:python_opencv]$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.7.zip
(venv) [koji:python_opencv]$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.7.zip
(venv) [koji:python_opencv]$ unzip opencv.zip
(venv) [koji:python_opencv]$ unzip opencv_contrib.zip

Build

(venv) root@6eff8c7e2c04:~/opencv_work/src# cd opencv-3.4.7/
(venv) root@6eff8c7e2c04:~/opencv_work/src/opencv-3.4.7# mkdir build
(venv) root@6eff8c7e2c04:~/opencv_work/src/opencv-3.4.7# cd build/
(venv) root@6eff8c7e2c04:~/opencv_work/src/opencv-3.4.7/build# 
(venv) root@6eff8c7e2c04:~/opencv_work/src/opencv-3.4.7/build# pwd
/root/opencv_work/src/opencv-3.4.7/build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/root/opencv_work/out \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -DWITH_FFMPEG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=/root/opencv_work/src/opencv_contrib-3.4.7/modules \
    -D PYTHON_EXECUTABLE=/usr/bin/python3.7 \
    -D PYTHON3_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.7m.so \
    -D BUILD_EXAMPLES=ON ..

if you see following message then it is ok.

--   Python 3:
--     Interpreter:                 /root/opencv_work/venv/bin/python3 (ver 3.7.3)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.7m.so (ver 3.7.3)
--     numpy:                       /root/opencv_work/venv/lib/python3.7/site-packages/numpy/core/include (ver 1.17.0)
--     install path:                lib/python3.7/site-packages/cv2/python-3.7

Install

make -j4

Setup

You have to not execute make install. There is already built shared library. file names cv2.cpython-37m-x86_64-linux-gnu.so
So, copy this file to site-packages under venv directory.

cp /root/opencv_work/src/opencv-3.4.7/build/lib/python3 cv2.cpython-37m-x86_64-linux-gnu.so /root/opencv_work/venv/lib/python3.7/site-packages/.

you can use OpenCV now.
check it!!

(venv) root@6eff8c7e2c04:~# python
Python 3.7.3 (default, Apr  3 2019, 19:16:38) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> import cv2
>>> cv2.__version__
'3.4.7'
>>> 

Use python installed via pyenv

Install Python 3.7

If you want to install and use Python via pyenv, you have to install Python like following:

CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.3

Install libraries need to build OpenCV

apt update

apt install wget libavcodec-dev libavformat-dev libavutil-dev libavfilter-dev libavresample-dev ffmpeg build-essential cmake unzip pkg-config libjpeg-dev libpng-dev libtiff-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev vim

Create directories

[koji:work]$ cd
[koji:~]$ mkdir -p $HOME/work/python-opencv/src
[koji:~]$ mkdir -p $HOME/work/python-opencv/out
[koji:~]$ cd $HOME/work/python-opencv 
[koji:python-opencv]$ 

Create virtual environments and install numpy

[koji:python-opencv]$ python --version
Python 3.7.2
[koji:python-opencv]$ python -m venv venv
[koji:python-opencv]$ source venv/bin/activate
(venv) [koji:python-opencv]$ pip install numpy
Collecting numpy
  Using cached https://files.pythonhosted.org/packages/05/4b/55cfbfd3e5e85016eeef9f21c0ec809d978706a0d60b62cc28aeec8c792f/numpy-1.17.0-cp37-cp37m-manylinux1_x86_64.whl
Installing collected packages: numpy
Successfully installed numpy-1.17.0
You are using pip version 18.1, however version 19.2.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
(venv) [koji:python-opencv]$ 

Download OpenCV and OpenCV contribution

(venv) [koji:src]$ pwd
/home/koji/work/python-opencv/src
(venv) [koji:src]$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.7.zip
(venv) [koji:src]$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.7.zip
(venv) [koji:src]$ unzip opencv
(venv) [koji:src]$ unzip opencv_contrib.zip 

Build

(venv) [koji:src]$ cd opencv-3.4.7 
(venv) [koji:opencv-3.4.7]$ mkdir build
(venv) [koji:opencv-3.4.7]$ cd build 
(venv) [koji:build]$ pwd
/home/koji/work/python-opencv/src/opencv-3.4.7/build
(venv) [koji:build]$ 

cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/home/koji/work/python-opencv/out \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -DWITH_FFMPEG=ON \
    -D OPENCV_EXTRA_MODULES_PATH=/home/koji/work/python-opencv/src/opencv_contrib-3.4.7/modules \
    -D PYTHON_EXECUTABLE=/home/koji/work/python_opencv/venv/bin/python \
    -D PYTHON3_LIBRARY=/home/koji/.pyenv/versions/3.7.2/lib/libpython3.7m.so \
    -D BUILD_EXAMPLES=ON ..

if you see following message then it is ok.

--   Python 3:
--     Interpreter:                 /home/koji/work/python-opencv/venv/bin/python3 (ver 3.7.2)
--     Libraries:                   /home/koji/.pyenv/versions/3.7.2/lib/libpython3.7m.so (ver 3.7.2)
--     numpy:                       /home/koji/work/python-opencv/venv/lib/python3.7/site-packages/numpy/core/include (ver 1.17.0)
--     install path:                lib/python3.7/site-packages/cv2/python-3.7

Install

make -j4

Setup

You have to not execute make install. There is already built shared library. file names cv2.cpython-37m-x86_64-linux-gnu.so
So, copy this file to site-packages under venv directory.

(venv) [koji:~]$ cp /home/koji/work/python-opencv/src/opencv-3.4.7/build/lib/python3/cv2.cpython-37m-x86_64-linux-gnu.so /home/koji/work/python-opencv/venv/lib/python3.7/site-packages/.

you can use OpenCV now.
check it!!

(venv) [koji:~]$ python
Python 3.7.2 (default, Mar  7 2019, 13:35:42) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 
>>> import cv2
>>> cv2.__version__
'3.4.7'
>>>