Skip to content


Installing OpenCV 3.2.0 with contrib modules in Ubuntu 16.04 LTS

UPDATE: You can also install OpenCV 4.5.0 in Ubuntu 20.04LTS.

OpenCV 3.2.0 has been out for a while and contains many improvements and exciting new features, so it’s time to update this guide using the latest Ubuntu 16.04LTS.

A big change in OpenCV 3.2.0 is that now many of the newest algorithms now reside separately in the contrib repository.

Some of these modules include Face Recognition, RGB-Depth processing, Image Registration, Saliency, Structure From Motion, Tracking, and much more.

So let’s install OpenCV 3.2.0 with the contrib modules and other good stuff by executing the following code on the command line:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff5-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev default-jdk ant libvtk5-qt4-dev
cd ~
mkdir opencv
cd opencv
wget https://github.com/opencv/opencv/archive/3.2.0.tar.gz
tar -xvzf 3.2.0.tar.gz
wget https://github.com/opencv/opencv_contrib/archive/3.2.0.zip
unzip 3.2.0.zip
cd opencv-3.2.0

But before we build it, we need to fix one problem currently present in the contrib modules, specifically in the freetype module, which allows you to draw UTF-8 strings. If you are getting an error similar to ImportError: /usr/local/lib/libopencv_freetype.so.3.2: undefined symbol: hb_shape, this will fix it:

sed -i 's/${freetype2_LIBRARIES} ${harfbuzz_LIBRARIES}/${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES}/g' ../opencv_contrib-3.2.0/modules/freetype/CMakeLists.txt

Since now that problem is solved, now we are ready to build OpenCV.

mkdir build
cd build
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D WITH_VTK=ON .. -DCMAKE_BUILD_TYPE=RELEASE -DOPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-3.2.0/modules ..
make
sudo make install
echo '/usr/local/lib' | sudo tee --append /etc/ld.so.conf.d/opencv.conf
sudo ldconfig
echo 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig' | sudo tee --append ~/.bashrc
echo 'export PKG_CONFIG_PATH' | sudo tee --append ~/.bashrc
source ~/.bashrc

Now you should be able to compile with the OpenCV libraries, including the contrib repositories.

For example, let’s compute some Fine-Grained Saliency, which is available in the saliency module of the contrib repository:

cd ~
mkdir saliency
cd saliency
cp ../opencv/opencv_contrib-3.2.0/modules/saliency/samples/computeSaliency.cpp .
cp ../opencv/opencv-3.2.0/samples/data/Megamind.avi .
g++ -o computeSaliency `pkg-config opencv --cflags` computeSaliency.cpp `pkg-config opencv --libs`
./computeSaliency FINE_GRAINED Megamind.avi 23

Original Image:

Computed Saliency:

Posted in Image Processing, Open Source, Programming.


17 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. xs.yan says

    After installing opencv I run the python samples code:

    cd /samples/python/
    python asift.py

    and I get the following error:

    Traceback (most recent call last):
    File “asift.py”, line 26, in
    import cv2
    ImportError: /usr/local/lib/libopencv_freetype.so.3.2: undefined symbol: hb_shape

    Running other python samples will get the same error. I haven’t found the answer in google. How do I fix it?

  2. will says

    have you solve this problem about “undefined symbol: hb_shape”

  3. samontab says

    Yeah, I updated the post with the fix to it. The problem was an incorrect file in the contrib module.

    To fix it, in the file ../opencv_contrib-3.2.0/modules/freetype/CMakeLists.txt in line 22, you need to change the text ${freetype2_LIBRARIES} ${harfbuzz_LIBRARIES} to ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES}

  4. samontab says

    Hey xs.yan, I updated the post with the fix to it. The problem was an incorrect file in the contrib module.

    To fix it, in the file ../opencv_contrib-3.2.0/modules/freetype/CMakeLists.txt in line 22, you need to change the text ${freetype2_LIBRARIES} ${harfbuzz_LIBRARIES} to ${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES}

  5. Vasco says

    Hi @samontab
    I have a problem when i make opencv-3.2.0

    [ 86%] Building CXX object modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o
    c++: internal compiler error: Killed (program cc1plus)
    Please submit a full bug report,
    with preprocessed source if appropriate.
    See for instructions.
    modules/python2/CMakeFiles/opencv_python2.dir/build.make:320: recipe for target ‘modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o’ failed
    make[2]: *** [modules/python2/CMakeFiles/opencv_python2.dir/__/src2/cv2.cpp.o] Error 4
    CMakeFiles/Makefile2:20806: recipe for target ‘modules/python2/CMakeFiles/opencv_python2.dir/all’ failed
    make[1]: *** [modules/python2/CMakeFiles/opencv_python2.dir/all] Error 2
    Makefile:160: recipe for target ‘all’ failed
    make: *** [all] Error 2

    I have tried to install many times but got them same error. Please help me!

  6. Ieuan Uys says

    Thank you so much, a casual 2 hour install time was well worth it.
    I now have the power!

  7. samontab says

    Hey Vasco, that looks like you don’t have enough memory in that system.

  8. Stevux says

    The contrib modules are actually compiled? Because you compile computeSaliency separately it would seem that are not compiled automatically…

  9. samontab says

    Yeah, the contrib modules are actually compiled automatically.

    computeSaliency is just an example program that uses the already compiled library module called saliency.

  10. Jay says

    I got an error when trying to run the example:

    ~/saliency$ ./computeSaliency FINE_GRAINED Megamind.avi 23
    ./computeSaliency: error while loading shared libraries: libopencv_saliency.so.3.2: cannot open shared object file: No such file or directory

  11. Ash says

    It all works but the bin files for sfm is missing – anything I should add ?
    Thanks

  12. Rishabh Arora says

    Doesnt work. Getting this error.
    [ 11%] Linking CXX executable ../../bin/opencv_test_core
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadRGBAStrip@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadDirectory@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFWriteEncodedStrip@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFIsTiled@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFWriteScanline@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFGetField@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFNumberOfStrips@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFScanlineSize@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadEncodedTile@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadRGBATile@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFClose@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFRGBAImageOK@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFOpen@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFReadEncodedStrip@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFSetField@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFSetWarningHandler@LIBTIFF_4.0′
    ../../lib/libopencv_imgcodecs.so.3.2.0: undefined reference to `TIFFSetErrorHandler@LIBTIFF_4.0′
    collect2: error: ld returned 1 exit status
    modules/core/CMakeFiles/opencv_test_core.dir/build.make:857: recipe for target ‘bin/opencv_test_core’ failed

  13. siddharth says

    while compiling the code for generating the depth map
    it gives following errors

    stereo_match.cpp: In function ‘int main(int, char**)’:
    stereo_match.cpp:69:14: error: cannot declare variable ‘bm’ to be of abstract type ‘cv::StereoBM’
    StereoBM bm;
    ^
    In file included from /usr/local/include/opencv2/calib3d/calib3d.hpp:48:0,
    from stereo_match.cpp:10:
    /usr/local/include/opencv2/calib3d.hpp:1740:20: note: because the following virtual functions are pure within ‘cv::StereoBM’:
    class CV_EXPORTS_W StereoBM : public StereoMatcher
    ^
    /usr/local/include/opencv2/calib3d.hpp:1714:26: note: virtual void cv::StereoMatcher::compute(cv::InputArray, cv::InputArray, cv::OutputArray)
    CV_WRAP virtual void compute( InputArray left, InputArray right,
    ^
    /usr/local/include/opencv2/calib3d.hpp:1717:25: note: virtual int cv::StereoMatcher::getMinDisparity() const
    CV_WRAP virtual int getMinDisparity() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1718:26: note: virtual void cv::StereoMatcher::setMinDisparity(int)
    CV_WRAP virtual void setMinDisparity(int minDisparity) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1720:25: note: virtual int cv::StereoMatcher::getNumDisparities() const
    CV_WRAP virtual int getNumDisparities() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1721:26: note: virtual void cv::StereoMatcher::setNumDisparities(int)
    CV_WRAP virtual void setNumDisparities(int numDisparities) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1723:25: note: virtual int cv::StereoMatcher::getBlockSize() const
    CV_WRAP virtual int getBlockSize() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1724:26: note: virtual void cv::StereoMatcher::setBlockSize(int)
    CV_WRAP virtual void setBlockSize(int blockSize) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1726:25: note: virtual int cv::StereoMatcher::getSpeckleWindowSize() const
    CV_WRAP virtual int getSpeckleWindowSize() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1727:26: note: virtual void cv::StereoMatcher::setSpeckleWindowSize(int)
    CV_WRAP virtual void setSpeckleWindowSize(int speckleWindowSize) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1729:25: note: virtual int cv::StereoMatcher::getSpeckleRange() const
    CV_WRAP virtual int getSpeckleRange() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1730:26: note: virtual void cv::StereoMatcher::setSpeckleRange(int)
    CV_WRAP virtual void setSpeckleRange(int speckleRange) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1732:25: note: virtual int cv::StereoMatcher::getDisp12MaxDiff() const
    CV_WRAP virtual int getDisp12MaxDiff() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1733:26: note: virtual void cv::StereoMatcher::setDisp12MaxDiff(int)
    CV_WRAP virtual void setDisp12MaxDiff(int disp12MaxDiff) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1747:25: note: virtual int cv::StereoBM::getPreFilterType() const
    CV_WRAP virtual int getPreFilterType() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1748:26: note: virtual void cv::StereoBM::setPreFilterType(int)
    CV_WRAP virtual void setPreFilterType(int preFilterType) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1750:25: note: virtual int cv::StereoBM::getPreFilterSize() const
    CV_WRAP virtual int getPreFilterSize() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1751:26: note: virtual void cv::StereoBM::setPreFilterSize(int)
    CV_WRAP virtual void setPreFilterSize(int preFilterSize) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1753:25: note: virtual int cv::StereoBM::getPreFilterCap() const
    CV_WRAP virtual int getPreFilterCap() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1754:26: note: virtual void cv::StereoBM::setPreFilterCap(int)
    CV_WRAP virtual void setPreFilterCap(int preFilterCap) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1756:25: note: virtual int cv::StereoBM::getTextureThreshold() const
    CV_WRAP virtual int getTextureThreshold() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1757:26: note: virtual void cv::StereoBM::setTextureThreshold(int)
    CV_WRAP virtual void setTextureThreshold(int textureThreshold) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1759:25: note: virtual int cv::StereoBM::getUniquenessRatio() const
    CV_WRAP virtual int getUniquenessRatio() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1760:26: note: virtual void cv::StereoBM::setUniquenessRatio(int)
    CV_WRAP virtual void setUniquenessRatio(int uniquenessRatio) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1762:25: note: virtual int cv::StereoBM::getSmallerBlockSize() const
    CV_WRAP virtual int getSmallerBlockSize() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1763:26: note: virtual void cv::StereoBM::setSmallerBlockSize(int)
    CV_WRAP virtual void setSmallerBlockSize(int blockSize) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1765:26: note: virtual cv::Rect cv::StereoBM::getROI1() const
    CV_WRAP virtual Rect getROI1() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1766:26: note: virtual void cv::StereoBM::setROI1(cv::Rect)
    CV_WRAP virtual void setROI1(Rect roi1) = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1768:26: note: virtual cv::Rect cv::StereoBM::getROI2() const
    CV_WRAP virtual Rect getROI2() const = 0;
    ^
    /usr/local/include/opencv2/calib3d.hpp:1769:26: note: virtual void cv::StereoBM::setROI2(cv::Rect)
    CV_WRAP virtual void setROI2(Rect roi2) = 0;
    ^
    stereo_match.cpp:70:16: error: cannot declare variable ‘sgbm’ to be of abstract type ‘cv::StereoSGBM’
    StereoSGBM sgbm;
    ^
    In file included from /usr/local/include/opencv2/calib3d/calib3d.hpp:48:0,
    from stereo_match.cpp:10:
    /usr/local/include/opencv2/calib3d.hpp:1805:20: note: because the following virtual functions are pure within ‘cv::StereoSGBM’:
    class CV_EXPORTS_W StereoSGBM : public StereoMatcher

  14. samontab says

    Make sure you’re using the correct API. Check out the stereo tutorials from here:

    https://docs.opencv.org/4.2.0/d3/d14/tutorial_ximgproc_disparity_filtering.html

Continuing the Discussion

  1. Installing OpenCV 2.4.9 in Ubuntu 14.04 LTS – Sebastian Montabone linked to this post on June 4, 2017

    […] UPDATE: You can also install OpenCV 3.2.0 in Ubuntu 16.04LTS. […]

  2. Installing OpenCV 2.1 in Ubuntu – Sebastian Montabone linked to this post on June 4, 2017

    […] UPDATE: You can also install OpenCV 3.2.0 in Ubuntu 16.04LTS. […]

  3. Installing OpenCV 2.2 in Ubuntu 11.04 – Sebastian Montabone linked to this post on June 4, 2017

    […] UPDATE: You can also install OpenCV 3.2.0 in Ubuntu 16.04LTS. […]



Some HTML is OK

or, reply to this post via trackback.