Install OpenCV-Contrib-Python on Ubuntu 14.04

download

This post will show you how to install opencv-python with contrib modules on Ubuntu 14.04 which is the default OS for most Ubuntu servers on AWS or Aliyun. This problem will struck you when you are ready to deploy your OpenCV apps on a server but you can not install latest opencv-python contrib module. Also, it will leave you hopeless because you will not receive any proper error messages when you try to install using convention method.

Most conventional way of installing OpenCV is via pipsudo pip install opencv-contrib-python. Visit here for more details. But this will work only for the latest Python 2.7. 12 (or higher) and Python 3 versions. By default Ubuntu 14.04 comes with old Python 2.7.6 which is not the best to install the latest opencv-contrib-python module.

To install opencv-contrib-python via pip on Ubuntu 14, first we have to upgrade Python to latest i.e. 2.7.14 (at the time of writing this blog-post). Follow these steps to upgrade Python to latest 2.7 version:

sudo add-apt-repository ppa:jonathonf/python-2.7
sudo apt-get update
sudo apt-get install python2.7
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python get-pip.py

To confirm Python is upgraded just switch to the Python console in your Ubuntu Terminal and it should show Python 2.7.14 or whatever is the latest at that time:

~ python
Python 2.7.14 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

 

Now you can install the opencv-contrib-python package using the command:

sudo pip install opencv-contrib-python
sudo apt update && apt install -y libsm6 libxext6

 

To confirm OpenCV installation switch back to Python console and try to import opencv package:

~ python
Python 2.7.14 (default, Dec 4 2017, 14:50:18)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
>>> '3.4.3'

If you see the latest version of OpenCV then it should be correctly installed. To confirm installation of the contrib module try to create an object for SIFT:

>>> sift = cv2.xfeatures2d.SIFT_create()
>>>

If you do not see any errors then the contrib module is installed properly.

A commonly occurring error message is:

cv2.error: OpenCV(3.4.3) /io/opencv_contrib/modules/xfeatures2d/src/sift.cpp:1207: error: (-213:The function/feature is not implemented) This algorithm is pa
tented and is excluded in this configuration; Set OPENCV_ENABLE_NONFREE CMake option and rebuild the library in function 'create'

This is because in the latest version of opencv-contrib-python 3.4.3.18 the environment variable OPENCV_ENABLE_NONFREE_CMake is not set properly. You can escape this by installing OpenCV 3.4.2 instead of the latest 3.4.3 because otherwise you will have to build OpenCV 3.4.3 from scratch which is a long and tedious process. To install some other version than the latest use following command:

$ sudo pip install opencv-contrib-python==3.4.2.17

This should resole the issue with the contrib module.