Install OpenCV 3.3 with Intel’s Numpy and Python Build

2 minute read

Published:

images/opencv.png

Installing OpenCV with Python bindings can be as easy as brew install opencv3 on a Mac. But replacing the default Numpy with your own build is no longer possible via options since Homebrew moved the formula to homebrew-core and removed the install options.

Build OpenCV with Intel Python and Numpy

Building OpenCV with Intel’s Math Kernel Library (MKL) can speed up operations on Intel hardware like my MacBook Pro (up to 97x).

Let’s get started.

Step 1:

If you haven’t done so, install Xcode, which is necessary for using certain compiling tools:

xcode-select --install

Step 2:

Install Homebrew using the one line of code on their website:

cd ~
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 3:

Use Intel’s version of Python. Get it via Anaconda, a Python package manager.

Install Miniconda3 to get conda on your system.

Update your conda:

conda update conda

Then download Intel’s Python 3 distribution:

Add the channel to conda:

conda config --add channels intel

Create the conda environment:

conda create -n idp intelpython3_core python=3

(Optional) Replace intelpython3_core with intelpython3_full to get the full Intel package.

Activate the environment:

source activate idp

Confirm you have Intel’s Python installed:

(idp) $ python --version
Python 3.5.3 :: Intel Corporation

Step 4:

Install OpenCV requirements:

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

Step 5:

cd ~/installs
git clone https://github.com/Itseez/opencv.git
cd opencv

(Optional) To use the contributed modules like SIFT and text detection:

cd ~/installs
git clone https://github.com/Itseez/opencv_contrib

Step 6:

Setup the OpenCV build directory:

cd ~/installs/opencv
mkdir build
cd build

Use CMake to configure the build. Note: If you have multiple versions of OpenCV on your computer, update CMAKE_INSTALL_PREFIX with something like ~/installs/opencv/build.

cmake -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/usr/local \
	-D PYTHON3_PACKAGES_PATH=~/anaconda3/idp/lib/python3.5/site-packages \
  -D PYTHON3_EXECUTABLE=~/anaconda3/envs/idp/bin/python3 \
	-D PYTHON3_LIBRARY=~/anaconda3/envs/idp/lib/libpython3.5m.dylib \
	-D PYTHON3_INCLUDE_DIR=~/anaconda3/envs/idp/include/python3.5m \
  -D PYTHON3_NUMPY_INCLUDE_DIRS=~/anaconda3/envs/idp/lib/python3.5/site-packages/numpy/core/include \
  -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON \
	-D BUILD_EXAMPLES=ON \
	-D OPENCV_EXTRA_MODULES_PATH=~/installs/opencv_contrib/modules ..

Read more about useful OpenCV CMake flags.

Step 7:

Compile OpenCV:

make -j4

Replace the 4 with the number of cores you have in your processor.

make install

Step 8:

Make sure OpenCV Python bindings are installed:

$ ls ~/anaconda3/envs/intel/lib/python3.5/site-packages/cv2*so
cv2.cpython-35m-darwin.so

Step 9:

Test out your installation:

$ python -c "import cv2; print(cv2.__version__)"
3.3.0-dev

Now you can optimize your code to work on Intel’s hardware. Let me know how it goes!

Leave a Comment