{"id":423,"date":"2017-06-03T11:40:53","date_gmt":"2017-06-03T15:40:53","guid":{"rendered":"http:\/\/www.samontab.com\/web\/?p=423"},"modified":"2021-03-03T09:02:38","modified_gmt":"2021-03-02T22:02:38","slug":"installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lts","status":"publish","type":"post","link":"https:\/\/www.samontab.com\/web\/2017\/06\/installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lts\/","title":{"rendered":"Installing OpenCV 3.2.0 with contrib modules in Ubuntu 16.04 LTS"},"content":{"rendered":"<p><strong>UPDATE<\/strong>: You can also install <a href=\"https:\/\/www.samontab.com\/web\/2020\/11\/installing-opencv-4-5-0-in-ubuntu-20-04-lts\/\">OpenCV 4.5.0 in Ubuntu 20.04LTS<\/a>.<\/p>\n<p><strong>OpenCV 3.2.0<\/strong> has been out for a while and contains many improvements and exciting new features, so it&#8217;s time to update this guide using the latest <strong>Ubuntu 16.04LTS<\/strong>.<\/p>\n<p>A big change in OpenCV 3.2.0 is that now many of the newest algorithms now reside separately in the <a href=\"https:\/\/github.com\/opencv\/opencv_contrib\/tree\/3.2.0\" target=\"_blank\" rel=\"noopener noreferrer\">contrib repository<\/a>.<\/p>\n<p>Some of these modules include <a href=\"https:\/\/en.wikipedia.org\/wiki\/Facial_recognition_system\" target=\"_blank\" rel=\"noopener noreferrer\">Face Recognition<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Range_imaging\" target=\"_blank\" rel=\"noopener noreferrer\">RGB-Depth processing<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Image_registration\" target=\"_blank\" rel=\"noopener noreferrer\">Image Registration<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Salience_(neuroscience)#Visual_saliency_modeling\" target=\"_blank\" rel=\"noopener noreferrer\">Saliency<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Structure_from_motion\" target=\"_blank\" rel=\"noopener noreferrer\">Structure From Motion<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Video_tracking\" target=\"_blank\" rel=\"noopener noreferrer\">Tracking<\/a>, and <a href=\"https:\/\/github.com\/opencv\/opencv_contrib\/tree\/3.2.0\/modules\" target=\"_blank\" rel=\"noopener noreferrer\">much more<\/a>.<\/p>\n<p>So let&#8217;s install <strong>OpenCV 3.2.0<\/strong> with the <strong>contrib modules<\/strong> and other good stuff by executing the following code on the command line:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsudo apt-get update\nsudo apt-get upgrade\nsudo 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\ncd ~\nmkdir opencv\ncd opencv\nwget https:\/\/github.com\/opencv\/opencv\/archive\/3.2.0.tar.gz\ntar -xvzf 3.2.0.tar.gz\nwget https:\/\/github.com\/opencv\/opencv_contrib\/archive\/3.2.0.zip\nunzip 3.2.0.zip\ncd opencv-3.2.0\n<\/pre>\n<p>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 <strong>ImportError: \/usr\/local\/lib\/libopencv_freetype.so.3.2: undefined symbol: hb_shape<\/strong>, this will fix it:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nsed -i 's\/${freetype2_LIBRARIES} ${harfbuzz_LIBRARIES}\/${FREETYPE_LIBRARIES} ${HARFBUZZ_LIBRARIES}\/g' ..\/opencv_contrib-3.2.0\/modules\/freetype\/CMakeLists.txt\n<\/pre>\n<p>Since now that problem is solved, now we are ready to build OpenCV.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmkdir build\ncd build\ncmake -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 ..\nmake\nsudo make install\necho '\/usr\/local\/lib' | sudo tee --append \/etc\/ld.so.conf.d\/opencv.conf\nsudo ldconfig\necho 'PKG_CONFIG_PATH=$PKG_CONFIG_PATH:\/usr\/local\/lib\/pkgconfig' | sudo tee --append ~\/.bashrc\necho 'export PKG_CONFIG_PATH' | sudo tee --append ~\/.bashrc\nsource ~\/.bashrc\n<\/pre>\n<p>Now you should be able to compile with the OpenCV libraries, including the contrib repositories.<\/p>\n<p>For example, let&#8217;s compute some <a href=\"https:\/\/scholar.google.com.au\/citations?view_op=view_citation&amp;hl=en&amp;user=dZGA9oQAAAAJ&amp;citation_for_view=dZGA9oQAAAAJ:u5HHmVD_uO8C\" target=\"_blank\" rel=\"noopener noreferrer\">Fine-Grained Saliency<\/a>, which is available in the saliency module of the contrib repository:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncd ~\nmkdir saliency\ncd saliency\ncp ..\/opencv\/opencv_contrib-3.2.0\/modules\/saliency\/samples\/computeSaliency.cpp .\ncp ..\/opencv\/opencv-3.2.0\/samples\/data\/Megamind.avi .\ng++ -o computeSaliency `pkg-config opencv --cflags` computeSaliency.cpp `pkg-config opencv --libs`\n.\/computeSaliency FINE_GRAINED Megamind.avi 23\n<\/pre>\n<p><strong>Original Image:<\/strong><br \/>\n<a href=\"http:\/\/www.samontab.com\/web\/2017\/06\/installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lts\/original\/\" rel=\"attachment wp-att-424\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/original-300x220.png\" alt=\"\" class=\"alignnone size-medium wp-image-424\" width=\"300\" height=\"220\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/original-300x220.png 300w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/original.png 720w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>Computed Saliency:<\/strong><br \/>\n<a href=\"http:\/\/www.samontab.com\/web\/2017\/06\/installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lts\/saliency-2\/\" rel=\"attachment wp-att-425\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/saliency-300x220.png\" alt=\"\" class=\"alignnone size-medium wp-image-425\" width=\"300\" height=\"220\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/saliency-300x220.png 300w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2017\/06\/saliency.png 720w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,30,4],"tags":[],"class_list":["post-423","post","type-post","status-publish","format-standard","hentry","category-open-source","category-opencv","category-programming"],"_links":{"self":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/comments?post=423"}],"version-history":[{"count":0,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/423\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/media?parent=423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/categories?post=423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/tags?post=423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}