Ubuntu 16.04: Install OpenCV 3.1

This article will describe building and installing OpenCV 3.1.

 

1 Build and install OpenCV

Install package required by OpenCV 2.4 of Ubuntu 16.04.

sudo apt-get build-dep -y opencv

Build and install OpenCV 3.1. OpenCV 3.1 will be installed to /usr/local, so please run export LD_LIBRARY_PATH=/usr/local/lib.

$ sudo apt-get build-dep -y opencv
$ git clone https://github.com/Itseez/opencv
$ cd opencv
$ git checkout 3.1.0 -b 3.1.0
$ cmake -G 'Unix Makefiles' \
  -DCMAKE_VERBOSE_MAKEFILE=ON \
  -DCMAKE_BUILD_TYPE=Release  \
  -DBUILD_EXAMPLES=ON \
  -DINSTALL_C_EXAMPLES=ON \
  -DINSTALL_PYTHON_EXAMPLES=ON  \
  -DBUILD_NEW_PYTHON_SUPPORT=ON \
  -DWITH_FFMPEG=ON  \
  -DWITH_GSTREAMER=OFF  \
  -DWITH_GTK=ON \
  -DWITH_JASPER=ON  \
  -DWITH_JPEG=ON  \
  -DWITH_PNG=ON \
  -DWITH_TIFF=ON  \
  -DWITH_OPENEXR=ON \
  -DWITH_PVAPI=ON \
  -DWITH_UNICAP=OFF \
  -DWITH_EIGEN=ON \
  -DWITH_XINE=OFF \
  -DBUILD_TESTS=OFF \
  -DCMAKE_SKIP_RPATH=ON \
  -DWITH_CUDA=OFF \
  -DENABLE_PRECOMPILED_HEADERS=OFF \
  -DENABLE_SSE=ON -DENABLE_SSE2=ON -DENABLE_SSE3=OFF \
  -DWITH_OPENGL=ON -DWITH_TBB=ON -DWITH_1394=ON -DWITH_V4L=ON
$ make && sudo make install

2 Excecution result

Trying to create thumbnail from movie with OpenCV python. Interface cv2.cv in OpenCV 2.4 is changed to cv2 in OpenCV 3.1.

#!/usr/bin/env python

import sys

import cv2


def create_thumbnail(movie_name, image_name):
    capture = cv2.VideoCapture(movie_name)
    max_frame = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    half_frame = max_frame // 2
    capture.set(cv2.CAP_PROP_POS_FRAMES, half_frame)
    retval, image = capture.read()
    if not retval:
        return False
    cv2.imwrite(image_name, image)
    return True


if __name__ == '__main__':
    argv = sys.argv
    if len(argv) != 3:
        print('Create thumbnail of movie at half position.')
        print('usage: %s [Input movie] [Output thumbnail]' % argv[0])
        exit(1)
    create_thumbnail(argv[1], argv[2])

PNG file is created from MP4 file.

$ export LD_LIBRARY_PATH=/usr/local/lib
$ python create-thumbnaiil.py input.mp4 output.png
$ file output.png
output.png: PNG image data, 480 x 360, 8-bit/color RGB, non-interlaced