I managed installing Python 3.5 and OpenCV library appropriately in my system after gathering steps and troubleshooting solutions over different tutorials and guides.
The installation is performed under a virtualenv, so there is no need to clean up previous install attempts footprints from your system.
Following the steps presented here you'll install:
- openCV 3.1.0
- opencv_contrib 3.1.0
- numpy
- scipy
- scikit
- matplotlib
- cython
- venv
At the end, it may take up to 20Gb of space if you haven't already installed any of those packages previously.
You will need gcc-4.9+ for compiling OpenCV, I tested it with gcc-5.4
Install OpenCV dependencies
sudo apt-get build-dep -y opencv
Create and setup a virtualenv
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
Compile OpenCV 3.1.0 and openvc_contrib 3.1.0
Dependencies
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
Getting repositories
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
Making sure some libs will be found
ffmpeg libs
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 ./
If, during compilation, occurs any problems when trying to find some ffmpeg libs, uninstall ffmpeg and build it from source.
python bindings with opencv_contrib modules
echo "\nfind_package(HDF5)\ninclude_directories(\${HDF5_INCLUDE_DIRS})" >> ~/git/opencv/modules/python/common.cmake
Compiling
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" ..
The output should include this:
-- 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
Now:
make
If it succeed, then:
make install
Add OpenCV libs to your virtualenv
cd ~/python35-opencv31/lib/site-packages
ln -s ~/opencv3.1.0/lib/python3.5/site-packages/cv2.cpython-35m-x86_64-linux-gnu.so
Done!
To test if it works as expected:
cd ~
source ~/python35-opencv31/bin/activate
python
import cv2
cv2.__version__
It should import cv2 and show version number 3.1.0.