{"id":2529,"date":"2015-10-18T07:18:23","date_gmt":"2015-10-18T07:18:23","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2529"},"modified":"2015-12-01T06:49:11","modified_gmt":"2015-12-01T06:49:11","slug":"how-to-install-opencv-for-python-in-linux","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2015\/10\/18\/how-to-install-opencv-for-python-in-linux\/","title":{"rendered":"How to install OpenCV for Python in Linux"},"content":{"rendered":"<p><a href=\"http:\/\/opencv.org\/\" target=\"_blank\">OpenCV<\/a> is the best open source solution for computer vision and image recognition applications.\u00a0 It exists for all the major platforms. In this post, we are going to see how to set it up in Ubuntu Linux or Linux Mint.<\/p>\n<p>We are looking at OpenCV 3.0.x which is the latest version as of now. Though you can obtain the source using apt-get , it is better to directly download it from the main site, as that is where the latest version is always available.<\/p>\n<h4>Step 1. Install Dependencies<\/h4>\n<p>We will need cmake to build the OpenCV as its in C++ so we need to update cmake first.<\/p>\n<pre>sudo apt-get install build-essential cmake\r\n<\/pre>\n<p>Next, we install the Qt library<\/p>\n<pre>sudo apt-get install qt5-default libvtk6-dev\r\n<\/pre>\n<p>Install image manipulation libraries<\/p>\n<pre>sudo apt-get install zlib1g-dev libjpeg-dev libwebp-dev libpng-dev libtiff5-dev libjasper-dev libopenexr-dev libgdal-dev\r\n<\/pre>\n<p>Install video manipulation libraries<\/p>\n<pre>sudo apt-get install libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev yasm libopencore-amrnb-dev libopencore-amrwb-dev libv4l-dev libxine2-dev\r\n<\/pre>\n<p>Install supporting math libraries<\/p>\n<pre>sudo apt-get install libtbb-dev libeigen3-dev\r\n<\/pre>\n<h4>Step 2. Update languages<\/h4>\n<p>We need to update python and Java.<\/p>\n<pre>sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy\r\nsudo apt-get install <code class=\"docutils literal\"><span class=\"pre\">python-matplotlib<\/span><\/code><\/pre>\n<pre>sudo apt-get install ant default-jdk\r\n<\/pre>\n<h4>Step 3: Compile OpenCV<\/h4>\n<p>Download the latest source zip from <a href=\"http:\/\/opencv.org\/downloads.html\" target=\"_blank\">here <\/a>. On this page click on OpenCV for Linux\/Mac which will start the download of the zip file.<\/p>\n<p>Unzip the zip into a directory of your choice. eg. <em>\/var\/projects\/opencv<\/em><\/p>\n<p>Make that directory your current directory and type <em>mkdir build<\/em> so you end up with <em>\/var\/projects\/opencv\/build . <\/em>Go into the build directory <em>cd build<\/em><\/p>\n<p>Setup Cmake configuration and run the build.<\/p>\n<pre>cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON ..\r\nmake -j4\r\nsudo make install\r\n<\/pre>\n<p>Run configure with<\/p>\n<pre>sudo ldconfig\r\n<\/pre>\n<p>This completes the setup of OpenCV.<\/p>\n<h4>Step 4: Running the Python Samples<\/h4>\n<p>Got into the samples folder eg. <em>cd \/var\/projects\/opencv\/samples\/python2<\/em><\/p>\n<p>You will find a lot of samples here.<\/p>\n<p>If you have a cam attached then do <em>python video.py<\/em><\/p>\n<p>To run a simple example, try <em>python floodfill.py<\/em><\/p>\n<p>You can get a list and explanation of all the samples by running <em>python demo.py<\/em><\/p>\n<p>&nbsp;<\/p>\n<p>Here is a video of me running the Canny Edge detection sample. <em>python edge.py<\/em><\/p>\n<p><a href=\"https:\/\/www.youtube.com\/watch?v=t2lVJOK12oY\">https:\/\/www.youtube.com\/watch?v=t2lVJOK12oY<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>OpenCV is the best open source solution for computer vision and image recognition applications.\u00a0 It exists for all the major platforms. In this post, we <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2015\/10\/18\/how-to-install-opencv-for-python-in-linux\/\" title=\"How to install OpenCV for Python in Linux\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2532,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[298,314],"tags":[],"class_list":["post-2529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-opencv"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2529"}],"version-history":[{"count":6,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2529\/revisions"}],"predecessor-version":[{"id":2544,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2529\/revisions\/2544"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2532"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}