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)

tatsambon edited this page Dec 8, 2016 · 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.

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), Yakkety (16.10), or Zesty, 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.

NOTE: It is possible to keep the installed OpenCV 2.x package from Ubuntu, and to install OpenCV 3.x To do that you need to build and install OpenCV 3.x locally and to add permanently the path /home/tatsambon/soft/opencv-build/install/lib/pkgconfig of the locally installed OpenCV 3.x to the PKG_CONFIG_PATH environment variable

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
# GTK+2, TBB, Eigen
sudo apt-get install libgtk2.0-dev libtbb-dev libeigen3-dev
# FFmpeg
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
# zlib, JPEG, PNG, TIFF, JasPer, OpenEXR, GDAL
sudo apt-get install zlib1g-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libopenexr-dev libgdal-dev
# Video4Linux, DC1394, Xine, GStreamer
sudo apt-get install libv4l-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev

Then we download OpenCV 3.1.0 sources:

$ mkdir ~/cv && cd ~/cv
$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.1.0.zip
$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.1.0.zip
$ unzip opencv.zip
$ unzip opencv_contrib.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 \
    -DWITH_CUDA=OFF \
    -DBUILD_opencv_cuda=OFF \
    -DBUILD_opencv_ts=OFF \
    -DBUILD_opencv_world=OFF \
    -DBUILD_opencv_contrib_world=OFF \
    -DBUILD_opencv_matlab=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr/local \
    -DOPENCV_EXTRA_MODULES_PATH=~/cv/opencv_contrib-3.1.0/modules ~/cv/opencv-3.1.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

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

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

NOTE: There's a bug in the current version with the installed file opencv.pc (when the WITH_IPP=ON configuration option is specified); the file will have -lippicv in the list of libraries to link against. This is not needed when linking against dynamic libraries (only valid when static linking, i.e pkg-config --static ...). To fix it, either remove the -lippicv entry, or move it from "Libs" section to "Libs.private" section while adding the correct path to the .a static library. In this guide, the correction to make would be:

/usr/local/lib/pkgconfig/opencv.pc

...
Libs: <...remove -lippicv from here...>
Libs.private: <...>  -L/usr/local/share/OpenCV/3rdparty/lib -lippicv
...

3) mexopencv

Download the latest version of mexopencv:

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

Compile the MEX-files for Octave:

$ cd ~/cv/mexopencv
$ make WITH_OCTAVE=true WITH_CONTRIB=true NO_CV_PKGCONFIG_HACK=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.

You can optionally run the full test suite as:

$ make WITH_OCTAVE=true WITH_CONTRIB=true test