Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Installation (Linux, Octave, OpenCV 3)

Amro edited this page Feb 7, 2018 · 14 revisions

This page provides a guide on how to install mexopencv with Octave on Ubuntu. It covers Octave 4, OpenCV 3 and latest mexopencv.

We compile OpenCV with "contrib" modules, which provide non-free features such as SIFT and SURF, as well as other experimental algorithms not included in main distribution.

The instructions below are meant for Ubuntu. Other Debian-like distros will probably also have these packages or similarly named ones available. Adjust accordingly for other Linux distributions.

1) Octave

First we install Octave 4. For older Ubuntu releases, you should use this PPA ppa:octave/stable.

Ubuntu >= 16

For Xenial (16.04), Artful (17.10), or Bionic (18.04) install the official Octave 4 package simply as:

$ sudo apt-get install octave liboctave-dev

Ubuntu < 16

For Precise (12.04) or Trusty (14.04), install Octave from the above PPA (maintained by GNU Octave team):

$ sudo add-apt-repository ppa:octave/stable
$ sudo apt-get update
$ sudo apt-get install octave liboctave-dev

2) OpenCV

Here we will build opencv + opencv_contrib from source (this requires about 2 to 3GB of free disk space).

The instructions below are similar to those in the official tutorial.

NOTE: If you had previously installed OpenCV 2.x package from Ubuntu, it would be better to remove it before continuing with OpenCV 3.x:

$ sudo apt-get autoremove libopencv-dev

if previously installed from source, do: sudo make uninstall.

This step is not mandatory, it is only suggested to avoid any conflicts in the libraries. In fact, you can have both OpenCV 2.x and 3.x installed side-by-side, as long as they are not both installed system-wide but locally. In this case, you will have to manually manage locations by using environment variables like PKG_CONFIG_PATH and LD_LIBRARY_PATH to switch between the two installations. In the rest of this guide, we assume that only OpenCV 3 is installed.

We start by installing some build dependencies (some are required, others are optional):

# GCC, make, CMake, pkg-config, Git
sudo apt-get install build-essential cmake pkg-config git
# zlib, JPEG, PNG, TIFF, JasPer, OpenEXR
sudo apt-get install zlib1g-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libopenexr-dev
# FFmpeg
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# Video4Linux, DC1394, Xine, gPhoto, GStreamer
sudo apt-get install libv4l-dev libdc1394-22-dev libxine2-dev libgphoto2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
# GTK+2, TBB, Eigen, BLAS/LAPACK/Atlas
sudo apt-get install libgtk2.0-dev libtbb-dev libeigen3-dev libblas-dev liblapack-dev liblapacke-dev libatlas-base-dev

Then we download OpenCV 3.4.0 sources:

$ mkdir ~/cv && cd ~/cv
$ wget -O opencv-3.4.0.zip https://github.com/opencv/opencv/archive/3.4.0.zip
$ wget -O opencv_contrib-3.4.0.zip https://github.com/opencv/opencv_contrib/archive/3.4.0.zip
$ unzip opencv-3.4.0.zip
$ unzip opencv_contrib-3.4.0.zip

Next we build and install it:

$ mkdir ~/cv/build && cd ~/cv/build
$ cmake -G "Unix Makefiles" \
    -DBUILD_DOCS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_TESTS=OFF \
    -DBUILD_JAVA=OFF \
    -DWITH_CUDA=OFF \
    -DWITH_CUBLAS:BOOL=OFF \
    -DWITH_CUFFT:BOOL=OFF \
    -DWITH_NVCUVID:BOOL=OFF \
    -DWITH_MATLAB=OFF \
    -DBUILD_opencv_cudaarithm:BOOL=OFF \
    -DBUILD_opencv_cudabgsegm:BOOL=OFF \
    -DBUILD_opencv_cudacodec:BOOL=OFF \
    -DBUILD_opencv_cudafeatures2d:BOOL=OFF \
    -DBUILD_opencv_cudafilters:BOOL=OFF \
    -DBUILD_opencv_cudaimgproc:BOOL=OFF \
    -DBUILD_opencv_cudalegacy:BOOL=OFF \
    -DBUILD_opencv_cudaobjdetect:BOOL=OFF \
    -DBUILD_opencv_cudaoptflow:BOOL=OFF \
    -DBUILD_opencv_cudastereo:BOOL=OFF \
    -DBUILD_opencv_cudawarping:BOOL=OFF \
    -DBUILD_opencv_cudev:BOOL=OFF \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_js=OFF \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=OFF \
    -DBUILD_opencv_ts=OFF \
    -DBUILD_opencv_world=OFF \
    -DBUILD_opencv_matlab=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DOPENCV_ENABLE_NONFREE=ON \
    -DOPENCV_EXTRA_MODULES_PATH=~/cv/opencv_contrib-3.4.0/modules ~/cv/opencv-3.4.0
$ make    # -j$(nproc)
$ sudo make install

# add CMAKE_INSTALL_PREFIX lib-dir to locations searched for shared libraries
$ sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv3.conf'
# update cache
$ sudo ldconfig

# setup pkg-config search path
$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH

Finally we check the output of pkg-config to verify the installation:

$ pkg-config --modversion opencv
$ pkg-config --cflags --libs opencv

3) mexopencv

Download the latest version of mexopencv:

$ cd ~/cv
$ wget -O mexopencv-master.zip https://github.com/kyamagu/mexopencv/archive/master.zip
$ unzip mexopencv-master.zip && mv mexopencv-master mexopencv

Compile the MEX-files for Octave:

$ cd ~/cv/mexopencv
$ make WITH_OCTAVE=true WITH_CONTRIB=true all contrib

Once it's done, you can start using OpenCV functions in Octave:

>> cd('~/cv/mexopencv')
>> addpath('~/cv/mexopencv')
>> addpath('~/cv/mexopencv/opencv_contrib')
>> addpath('~/cv/mexopencv/+cv/private')                 % HACK
>> addpath('~/cv/mexopencv/opencv_contrib/+cv/private')  % HACK
>> cv.getBuildInformation()

You might wanna put those addpath() calls in your ~/.octaverc config file if you don't want to have to repeat them every time Octave is started.

To verify the installation, you can optionally run the full test suite:

$ make WITH_OCTAVE=true WITH_CONTRIB=true test