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

Installation (Linux, MATLAB, OpenCV 3)

Amro edited this page Oct 4, 2018 · 9 revisions

This page provides a guide on how to install mexopencv with MATLAB on Ubuntu. It covers 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) 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.1 sources:

$ mkdir ~/cv && cd ~/cv
$ wget -O opencv-3.4.1.zip https://github.com/opencv/opencv/archive/3.4.1.zip
$ wget -O opencv_contrib-3.4.1.zip https://github.com/opencv/opencv_contrib/archive/3.4.1.zip
$ unzip opencv-3.4.1.zip
$ unzip opencv_contrib-3.4.1.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=OFF \
    -DWITH_CUFFT=OFF \
    -DWITH_NVCUVID=OFF \
    -DWITH_MATLAB=OFF \
    -DBUILD_opencv_cudaarithm=OFF \
    -DBUILD_opencv_cudabgsegm=OFF \
    -DBUILD_opencv_cudacodec=OFF \
    -DBUILD_opencv_cudafeatures2d=OFF \
    -DBUILD_opencv_cudafilters=OFF \
    -DBUILD_opencv_cudaimgproc=OFF \
    -DBUILD_opencv_cudalegacy=OFF \
    -DBUILD_opencv_cudaobjdetect=OFF \
    -DBUILD_opencv_cudaoptflow=OFF \
    -DBUILD_opencv_cudastereo=OFF \
    -DBUILD_opencv_cudawarping=OFF \
    -DBUILD_opencv_cudev=OFF \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_java_bindings_generator=OFF \
    -DBUILD_opencv_js=OFF \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=OFF \
    -DBUILD_opencv_python_bindings_generator=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.1/modules ~/cv/opencv-3.4.1
$ 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

2) 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 MATLAB:

$ cd ~/cv/mexopencv
$ make MATLABDIR=/usr/local/matlab/R2017a WITH_CONTRIB=true all contrib

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

>> cd('~/cv/mexopencv')
>> addpath('~/cv/mexopencv')
>> addpath('~/cv/mexopencv/opencv_contrib')
>> cv.getBuildInformation()

You might wanna use savepath() if you don't want to have to repeat the addpath() calls every time MATLAB is started.

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

>> addpath('~/cv/mexopencv/test')
>> UnitTest('ContribModules',true)