OpenCV 3.1.0 with Python 3.5
<p>After following several different tutorials, guides and steps recommended in other SO answers I didn't manage to install OpenCV for use with Python 3.5 in my Ubuntu 16.04 system.</p>

<p>As long as OpenCV 3.1.0 officially supports Python 3.x, how do I install it appropriately?</p>

<p>I managed installing Python 3.5 and OpenCV library appropriately in my system after gathering steps and troubleshooting solutions over different tutorials and guides.</p>

<p><em>The installation is performed under a virtualenv, so there is no need to clean up previous install attempts footprints from your system.</em></p>

<p>Following the steps presented here you'll install:</p>

<ul>
<li>openCV 3.1.0</li>
<li>opencv_contrib 3.1.0</li>
<li>numpy</li>
<li>scipy</li>
<li>scikit</li>
<li>matplotlib</li>
<li>cython</li>
<li>venv</li>
</ul>

<p>At the end, <strong>it may take up to 20Gb of space</strong> if you haven't already installed any of those packages previously.</p>

<p><em>You will need gcc-4.9+ for compiling OpenCV, I tested it with gcc-5.4</em></p>

<h1>Install OpenCV dependencies</h1>

<pre><code>sudo apt-get build-dep -y opencv
</code></pre>

<h1>Create and setup a virtualenv</h1>

<pre><code>sudo apt-get install python3-venv
python3.5 -m venv python35-opencv31
source ~/python35-opencv31/bin/activate
pip install matplotlib
pip install numpy
pip install scipy
pip install scikit-learn
pip install cython
pip install -U scikit-image
</code></pre>

<h1>Compile OpenCV 3.1.0 and openvc_contrib 3.1.0</h1>

<h2>Dependencies</h2>

<pre><code>sudo apt-get install build-essential cmake libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
</code></pre>

<h2>Getting repositories</h2>

<pre><code>mkdir ~/git
cd ~/git
git clone https://github.com/opencv/opencv.git
cd ./opencv
git checkout 3.1.0

cd ~/git
git clone https://github.com/Itseez/opencv_contrib.git
cd ./opencv_contrib
git checkout 3.1.0
</code></pre>

<h2>Making sure some libs will be found</h2>

<p><strong>ffmpeg libs</strong></p>

<pre><code>sudo -i
mkdir /usr/include/ffmpeg
cd /usr/include/ffmpeg
ln -sf /usr/include/x86_64-linux-gnu/libavcodec/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libavformat/*.h ./
ln -sf /usr/include/x86_64-linux-gnu/libswscale/*.h ./
</code></pre>

<p>If, during compilation, occurs any problems when trying to find some ffmpeg libs, uninstall ffmpeg and <a href="https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu" rel="nofollow">build it from source</a>.</p>

<p><strong>python bindings with opencv_contrib modules</strong></p>

<pre><code>echo "\nfind_package(HDF5)\ninclude_directories(\${HDF5_INCLUDE_DIRS})" &gt;&gt; ~/git/opencv/modules/python/common.cmake
</code></pre>

<h2>Compiling</h2>

<pre><code>source ~/python35-opencv31/bin/activate
mkdir ~/opencv3.1.0
cd ~/git/opencv/
mkdir release
cd ./release
export CC=$(which gcc)
export CXX=$(which g++)
cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=~/opencv3.1.0 \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/git/opencv_contrib/modules \
    -D BUILD_EXAMPLES=ON \
    -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..
</code></pre>

<p>The output should include this:</p>

<pre><code>--   Python 2:
--     Interpreter:                 /home/rodrigo/anaconda/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /home/rodrigo/anaconda/lib/python2.7/site-packages/numpy/core/include (ver 1.10.4)
--     packages path:               lib/python2.7/site-packages
-- 
--   Python 3:
--     Interpreter:                 /home/rodrigo/python35-opencv/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /home/rodrigo/python35-opencv/lib/python3.5/site-packages/numpy/core/include (ver 1.11.2)
--     packages path:               lib/python3.5/site-packages
-- 
--   Python (for build):            /home/rodrigo/anaconda/bin/python2.7
</code></pre>

<p>Now:</p>

<pre><code>make
</code></pre>

<p>If it succeed, then:</p>

<pre><code>make install
</code></pre>

<h1>Add OpenCV libs to your virtualenv</h1>

<pre><code>cd ~/python35-opencv31/lib/site-packages
ln -s ~/opencv3.1.0/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
</code></pre>

<h1>Done!</h1>

<p>To test if it works as expected:</p>

<pre><code>cd ~
source ~/python35-opencv31/bin/activate
python
import cv2
cv2.__version__
</code></pre>

<p>It should import cv2 and show version number 3.1.0.</p>

python linux opencv python-3.5 opencv3.1