{"id":41,"date":"2010-04-09T14:57:47","date_gmt":"2010-04-09T04:57:47","guid":{"rendered":"http:\/\/www.samontab.com\/web\/?p=41"},"modified":"2021-03-03T09:57:16","modified_gmt":"2021-03-02T22:57:16","slug":"installing-opencv-2-1-in-ubuntu","status":"publish","type":"post","link":"https:\/\/www.samontab.com\/web\/2010\/04\/installing-opencv-2-1-in-ubuntu\/","title":{"rendered":"Installing OpenCV 2.1 in Ubuntu"},"content":{"rendered":"<p><strong>UPDATE<\/strong>: You can also install <a href=\"https:\/\/www.samontab.com\/web\/2017\/06\/installing-opencv-3-2-0-with-contrib-modules-in-ubuntu-16-04-lts\/\">OpenCV 3.2.0 in Ubuntu 16.04LTS<\/a>.<\/p>\n<p>OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis.<\/p>\n<p>OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of my previous posts <a href=\"https:\/\/www.samontab.com\/web\/2010\/03\/installing-opencv-2-0-in-ubuntu\/\">https:\/\/www.samontab.com\/web\/2010\/03\/installing-opencv-2-0-in-ubuntu\/<\/a><\/p>\n<p>Unfortunately, the newer version of OpenCV, 2.1, which was released on April has a slightly different installation procedure. Since it contains many bug fixes and some nice new additions, I will show you how to install it.<\/p>\n<p>UPDATE: Install OpenCV 2.2 in Ubuntu 11.04 with Python and TBB support <a href=\"https:\/\/www.samontab.com\/web\/2011\/06\/installing-opencv-2-2-in-ubuntu-11-04\/\">here<\/a>.<\/p>\n<p>Here are the steps that I used to successfully install OpenCV 2.1 in Ubuntu 9.10. I have used this procedure for previous versions of Ubuntu as well with minor modifications (if any).<\/p>\n<p>First, you need to install many dependencies, such as support for reading and writing jpg files, movies, etc&#8230; This step is very easy, you only need to write the following command in the Terminal<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\nsudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg62-dev libtiff4-dev cmake libswscale-dev libjasper-dev\n\n<\/pre>\n<p>The next step is to get the OpenCV 2.1 code:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\nwget https:\/\/sourceforge.net\/projects\/opencvlibrary\/files\/opencv-unix\/2.1\/OpenCV-2.1.0.tar.bz2\/download\ntar -xvf OpenCV-2.1.0.tar.bz2\ncd OpenCV-2.1.0\/\n<\/pre>\n<p>In this version of OpenCV, the configure utility has been removed. Therefore you need to use Cmake to generate the makefile. Just execute the following line at the console. Note that there is a dot at the end of the line, it is an argument for the cmake program and it means current directory.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncmake .\n<\/pre>\n<p>Check that the above command produces no error and that in particular it reports FFMPEG as 1. If this is not the case you will not be able to read or write videos.<\/p>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-samontab@grima17-OpenCV-2.1.0.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-42 alignnone\" title=\"Screenshot-samontab@grima17: ~-OpenCV-2.1.0\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-samontab@grima17-OpenCV-2.1.0-300x207.png\" alt=\"configuring opencv2.1\" width=\"300\" height=\"207\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-samontab@grima17-OpenCV-2.1.0-300x207.png 300w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-samontab@grima17-OpenCV-2.1.0.png 666w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now, you are ready to compile and install OpenCV 2.1:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nmake\nsudo make install\n<\/pre>\n<p>Now you have to configure the library. First, open the opencv.conf file with the following code:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo gedit \/etc\/ld.so.conf.d\/opencv.conf\n<\/pre>\n<p>Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n\/usr\/local\/lib\n<\/pre>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-opencv.conf-etc-ld.so_.conf_.d-gedit.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Screenshot-opencv.conf (-etc-ld.so.conf.d) - gedit\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-opencv.conf-etc-ld.so_.conf_.d-gedit-300x240.png\" alt=\"\" width=\"300\" height=\"240\"><\/a><\/p>\n<p>Run the following code to configure the library:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo ldconfig\n<\/pre>\n<p>Now you have to open another file:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nsudo gedit \/etc\/bash.bashrc\n<\/pre>\n<p>Add these two lines at the end of the file and save it:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:\/usr\/local\/lib\/pkgconfig\nexport PKG_CONFIG_PATH\n<\/pre>\n<div><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-bash.bashrc-etc-gedit.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Screenshot-bash.bashrc (-etc) - gedit\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-bash.bashrc-etc-gedit-300x240.png\" alt=\"\" width=\"300\" height=\"240\"><\/a><\/div>\n<p>Finally, open a new console, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.<\/p>\n<p>Now you have OpenCV 2.1 installed in your computer.<\/p>\n<p>Let&#8217;s check some demos included in OpenCV:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncd ~\nmkdir openCV_samples\ncp OpenCV-2.1.0\/samples\/c\/* openCV_samples\ncd openCV_samples\/\nchmod +x build_all.sh\n.\/build_all.sh\n<\/pre>\n<p>Some of the training data for object detection is stored in \/usr\/local\/share\/opencv\/haarcascades. You need to tell OpenCV which training data to use. I will use one of the frontal face detectors available. Let&#8217;s find a face:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n.\/facedetect --cascade=\"\/usr\/local\/share\/opencv\/haarcascades\/haarcascade_frontalface_alt.xml\" --scale=1.5 lena.jpg\n<\/pre>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-1.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Screenshot-1\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-1-288x300.png\" alt=\"\" width=\"288\" height=\"300\"><\/a><\/p>\n<p>Note the scale parameter. It allows you to increase or decrease the size of the smallest object found in the image (faces in this case). Smaller numbers allows OpenCV to find smaller faces, which may lead to increasing the number of false detections. Also, the computation time needed gets larger when searching for smaller objects.<\/p>\n<p>In OpenCV 2.1, the grabcut algorithm is provided in the samples. This is a very nice segmentation algorithm that needs very little user input to segment&nbsp; the objects in the image. For using the demo, you need to select a rectangle of the area you want to segment. Then, hold the Control key and left click to select the background (in Blue). After that, hold the Shift key and left click to select the foreground (in Red). Then press the n key to generate the segmentation. You can press n again to continue to the next iteration of the algorithm.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n.\/grabcut lena.jpg\n<\/pre>\n<p>This image shows the initial rectangle for defining the object that I want to segment.<\/p>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-43\" title=\"Screenshot-image-2\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-2-288x300.png\" alt=\"\" width=\"288\" height=\"300\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-2-288x300.png 288w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-2.png 518w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p>Now I roughly set the foreground (red) and background (blue).<\/p>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-44\" title=\"Screenshot-image-3\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-3-288x300.png\" alt=\"\" width=\"288\" height=\"300\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-3-288x300.png 288w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-3.png 518w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p>When you are ready, press the n key to run the grabcut algorithm. This image shows the result of the first iteration of the algorithm.<\/p>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-45\" title=\"Screenshot-image-4\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-4-288x300.png\" alt=\"\" width=\"288\" height=\"300\" srcset=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-4-288x300.png 288w, https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/04\/Screenshot-image-4.png 518w\" sizes=\"auto, (max-width: 288px) 100vw, 288px\" \/><\/a><\/p>\n<p>Now let&#8217;s see some background subtraction from a video. The original video shows a hand moving in front of some trees. OpenCV allows you to separate the foreground (hand) from the background (trees).<\/p>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-tree.avi-VLC-media-player.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Screenshot-tree.avi - VLC media player\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-tree.avi-VLC-media-player-281x300.png\" alt=\"\" width=\"281\" height=\"300\"><\/a><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n.\/bgfg_segm tree.avi\n<\/pre>\n<p><a href=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-2.png\"><img loading=\"lazy\" decoding=\"async\" title=\"Screenshot-2\" src=\"https:\/\/www.samontab.com\/web\/wp-content\/uploads\/2010\/03\/Screenshot-2-300x122.png\" alt=\"\" width=\"300\" height=\"122\"><\/a><\/p>\n<p>There are many other samples that you can try.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: You can also install OpenCV 3.2.0 in Ubuntu 16.04LTS. OpenCV is an excellent library for Computer Vision. I have been using it for years and it helped me a lot during my master thesis. OpenCV 1.0 can be easily installed in Ubuntu via the repositories. You can install OpenCV 2.0 by following one of [&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":[],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-computer-vision","category-open-source","category-opencv"],"_links":{"self":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/41","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=41"}],"version-history":[{"count":0,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.samontab.com\/web\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}