Compile OpenCV 3.3 for Anaconda Python 3.6, 3.5, 3.4 and 2.7

Related: easy no-compile install OpenCV for Python


This procedure assumes that your preferred Python exe is aliased to (it runs when you type)

python

OpenCV 3.3 works with Python 2.7, 3.4, 3.5, 3.6.

0. OpenCV Prereqs:

Optionally, you can install free Intel IPP, TBB and/or MKL as well.

Linux

apt install gcc g++ git libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev pkg-config cmake libgtk2.0-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavcodec-dev libavutil-dev libavfilter-dev libavformat-dev libavresample-dev

Mac OSX

brew install git cmake pkg-config jpeg libpng libtiff openexr eigen tbb

1. Build Python OpenCV3

wget https://github.com/opencv/opencv/archive/3.3.0.zip

unzip 3*.zip
cd opencv*

mkdir release
cd release

cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=ON -DWITH_OPENGL=ON -DWITH_OPENCL=ON -DWITH_IPP=ON -DWITH_TBB=ON -DWITH_EIGEN=ON -DWITH_V4L=ON -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python2=OFF -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python) -DPYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

If you have trouble with Cmake, consider cmake-gui or using the simplest Cmake script at the bottom of this page.

make -j4
make install   # do not use sudo !

which results in something like this Gist:

2. Test OpenCV

from time import time
from numpy import uint8
from numpy.random import rand
import cv2

xy=(512,512)
Nf = 500

def fpsopencv(dat):
    tic = time()
    for i in dat:
        cv2.imshow('test',i)
        cv2.waitKey(1) #integer milliseconds, 0 makes wait forever
    cv2.destroyAllWindows()
    return Nf / (time()-tic)


imgs = (rand(Nf,xy[0],xy[1])*255).astype(uint8)
fps = fpsopencv(imgs)

print(fps,'fps')

This should play a random noise movie.

Fix OpenCV errors

“ModuleNotFoundError: No module named ‘cv2’”

Ensure that desired Python site-packages found with

python -c "import sys; print(next(p for p in sys.path if 'site-packages' in p))"

contains

  • Windows: cv2.*.pyd
  • Linux: cv2.*.so

ImportError: /media/BigData/miniconda3/lib/libstdc++.so.6: version `GLIBCXX_3.4.22’ not found (required by /media/BigData/miniconda3/lib/libopencv_objdetect.so.3.3)

Use a more up to date system libstdc++ by

cd ~/miniconda3/lib
rm libstdc++.so.6
ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6

If you get error 

/usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: No such file or directory #include_next  

or

~/miniconda3/lib/libopencv_objdetect.so.3.3: undefined symbol: _ZTINSt6thread6_StateE

then try using g++ 5


  • Disable use of Nvidia CUDA when compiling OpenCV via -DWITH_CUDA=OFF. Because Cuda takes so much longer to compile, even if you have the GPU, maybe first try without CUDA, to see if OpenCV3 is going to work for you, then recompile with CUDA.
  • To avoid the libopencv\_imgcodecs.so.3: undefined reference to [email protected]_4.0 type errors, I added the -DBUILD_TIFF=ON option

If you get the error like

lib/libm.so.6: version GLIBC_2.15 not found (required by /usr/lib/x86_64-linux-gnu/libx264.so.142)

then try as a fix to GLIBC_2.15 not found

cd ~/anaconda3/lib
mv libm.so.6 libm.so.6.bak

If you get error

SIGILL illegal instruction (Core dumped)

when typing

import cv2

try to get more information by typing in Terminal

sudo sysctl -w kernel.core_pattern=core
ulimit -c unlimited

and rerun the command that gave the core dumped error. Now you’ll have a file core in that directory. Type in Terminal

gdb python core

to get hints about the failure.

Minimal OpenCV cmake

This has just the stuff needed for a basic OpenCV install for Python 3.

cmake -DBUILD_TIFF=ON -DBUILD_opencv_java=OFF -DWITH_CUDA=OFF -DENABLE_AVX=OFF -DWITH_OPENGL=OFF -DWITH_OPENCL=OFF -DWITH_IPP=OFF -DWITH_TBB=OFF -DWITH_EIGEN=OFF -DWITH_V4L=OFF -DWITH_VTK=OFF -DBUILD_TESTS=OFF -DBUILD_PERF_TESTS=OFF -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_opencv_python2=OFF -DCMAKE_INSTALL_PREFIX=$(python -c "import sys; print(sys.prefix)") -DPYTHON3_EXECUTABLE=$(which python) -DPYTHON3_INCLUDE_DIR=$(python -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") -DPYTHON3_PACKAGES_PATH=$(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") ..

Notes

Intel MKL installation

  1. download free Intel MKL.
  2. extract to /tmp and type

     ./install_GUI.sh
    
  3. choose GUI option “root as sudo”
  4. this will install to /opt/intel

reference

Leave a Comment