{"id":593,"date":"2023-02-08T22:50:59","date_gmt":"2023-02-08T11:50:59","guid":{"rendered":"https:\/\/www.samontab.com\/web\/?p=593"},"modified":"2023-02-09T01:13:43","modified_gmt":"2023-02-08T14:13:43","slug":"installing-opencv-4-7-0-in-ubuntu-22-04-lts","status":"publish","type":"post","link":"https:\/\/www.samontab.com\/web\/2023\/02\/installing-opencv-4-7-0-in-ubuntu-22-04-lts\/","title":{"rendered":"Installing OpenCV 4.7.0 in Ubuntu 22.04 LTS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">OpenCV is the most popular computer vision library in the world, widely used in research and industry for more than twenty years. The latest version available at the time of writing is 4.7.0, and it comes with many new features and bug fixes across all modules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;re also going to include the extra modules so that we have access to the latest research in other areas such as the RGBD module for depth cameras. If you&#8217;re planning to buy a new depth camera, make sure to check <a rel=\"noreferrer noopener\" href=\"https:\/\/www.samontab.com\/web\/2023\/02\/overview-of-current-luxonis-oak-cameras-or-which-one-you-should-get-for-your-next-computer-vision-application\/\" target=\"_blank\">this post<\/a> to help you decide which one to get.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;re going to be using Ubuntu 22.04 LTS for this guide, but it should be similar for other versions of Ubuntu. At the end of this tutorial, you will be able to make computer vision applications with OpenCV using either C++ or Python, and the extra modules will be available to you.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin. The first step is to make sure you have everything up to date:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo apt-get update\nsudo apt-get upgrade\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s grab some dependencies:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo apt-get install build-essential cmake python3-numpy python3-dev python3-tk libavcodec-dev libavformat-dev libavutil-dev libswscale-dev libdc1394-dev libeigen3-dev libgtk-3-dev libvtk7-qt-dev\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Time to grab the source code of OpenCV and the extra modules, configure it and compile it:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmkdir ~\/opencv\ncd ~\/opencv\nwget https:\/\/github.com\/opencv\/opencv\/archive\/refs\/tags\/4.7.0.tar.gz\ntar -xvzf 4.7.0.tar.gz\nrm 4.7.0.tar.gz\nwget https:\/\/github.com\/opencv\/opencv_contrib\/archive\/refs\/tags\/4.7.0.tar.gz\ntar -xvzf 4.7.0.tar.gz\nrm 4.7.0.tar.gz\ncd opencv-4.7.0\nmkdir build\ncd build\ncmake -D WITH_TBB=ON -D BUILD_opencv_apps=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-4.7.0\/modules\nmake -j`nproc`\nsudo make install\necho &#039;\/usr\/local\/lib&#039; | sudo tee --append \/etc\/ld.so.conf.d\/opencv.conf\nsudo ldconfig\necho &#039;PKG_CONFIG_PATH=$PKG_CONFIG_PATH:\/usr\/local\/lib\/pkgconfig&#039; | sudo tee --append ~\/.bashrc\necho &#039;export PKG_CONFIG_PATH&#039; | sudo tee --append ~\/.bashrc\nsource ~\/.bashrc\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Now you should have OpenCV correctly installed, and you should be able to start building your computer vision applications in either C++ or Python. Let&#8217;s get you started with some examples.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;re going to create a C++ application based on one of the examples included in the extra modules of OpenCV. It&#8217;s an application that generates Fine-Grained saliency based on <a rel=\"noreferrer noopener\" href=\"https:\/\/scholar.google.com\/citations?view_op=view_citation&amp;hl=en&amp;user=dZGA9oQAAAAJ&amp;citation_for_view=dZGA9oQAAAAJ:u5HHmVD_uO8C\" target=\"_blank\">this paper<\/a>. And we&#8217;re going to use CMake to configure it so that you can use the same project structure later for your own applications. The first step is to grab the sample files:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\nmkdir saliency\ncd saliency\ncp ..\/opencv\/opencv_contrib-4.7.0\/modules\/saliency\/samples\/computeSaliency.cpp .\ncp ..\/opencv\/opencv-4.7.0\/samples\/data\/Megamind.avi .\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We&#8217;re now going to create a new text file in that same directory called <strong>CMakeLists.txt<\/strong> so that cmake can understand how to build it.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nnano CMakeLists.txt\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Paste this content into the file, save it(Ctrl+O) and then exit(Ctrl+X).<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncmake_minimum_required (VERSION 3.0.2)\nproject (saliency)\n\nfind_package(OpenCV REQUIRED)\ninclude_directories( ${OpenCV_INCLUDE_DIRS} )\ninclude_directories( . )\n\nadd_executable (${PROJECT_NAME} computeSaliency.cpp )\ntarget_link_libraries (${PROJECT_NAME} ${OpenCV_LIBS})\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And now we&#8217;re ready to build the application:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmkdir build\ncd build\ncmake ..\nmake\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">We can now run the application. It expects as arguments the type of saliency (FINE_GRAINED), a video file (..\/Megamind.avi), and a frame number to use (23):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n.\/saliency FINE_GRAINED ..\/Megamind.avi 23\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"473a36\" data-has-transparency=\"false\" style=\"--dominant-color: #473a36;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"450\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/saliency-1024x450.webp\" alt=\"\" class=\"wp-image-604 not-transparent\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/saliency-1024x450.webp 1024w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/saliency-300x132.webp 300w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/saliency-768x338.webp 768w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/saliency-jpg.webp 1455w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Original frame on the left, Fine-Grained Saliency calculated on the right.<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You are now ready to start making your own computer vision application in C++. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now let&#8217;s see how we can run a Python example as well:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmkdir ~\/python_app\ncd ~\/python_app\ncp ..\/opencv\/opencv-4.7.0\/samples\/python\/gaussian_mix.py .\npython3 gaussian_mix.py\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image size-full\"><img data-dominant-color=\"282625\" data-has-transparency=\"true\" style=\"--dominant-color: #282625;\" loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"616\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/gaussian.png\" alt=\"\" class=\"wp-image-605 has-transparency\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/gaussian.png 522w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2023\/02\/gaussian-254x300.png 254w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><figcaption class=\"wp-element-caption\">An example of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/EM_algorithm_and_GMM_model\" target=\"_blank\" rel=\"noreferrer noopener\">Expectation\u2013Maximisation algorithm with a Gaussian Mixture Model<\/a> in Python<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Now you should be able to build your own computer vision applications, with either C++ or Python.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enjoyed the tutorial?<\/p>\n\n\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenCV is the most popular computer vision library in the world, widely used in research and industry for more than twenty years. The latest version available at the time of writing is 4.7.0, and it comes with many new features and bug fixes across all modules. We&#8217;re also going to include the extra modules so [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,21,30],"tags":[74,69,71,73,31,70],"class_list":["post-593","post","type-post","status-publish","format-standard","hentry","category-computer-vision","category-open-source","category-opencv","tag-build","tag-computer-vision","tag-depth-camera","tag-install","tag-opencv","tag-rgbd"],"_links":{"self":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/593","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=593"}],"version-history":[{"count":0,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/593\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/media?parent=593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/categories?post=593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/tags?post=593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}