Install OpenCV in Ubuntu

Image

 

A lot of people have been asking me about this, so I thought it would be easier to explain it through a written tutorial which you can follow easily.

A lot of websites, videos are available showing how to do this, but a majority of them lack some or the other installers!

So, this is a simple guide on how to install and compile one of the most famous computer vision software on your Ubuntu (11.0+) device!

Method 1: Crude method. Install all libraries and files one by one.

 10 Simple steps to have OpenCV running on your Ubuntu system: 

Step 1: Have your updates and upgrades at the newest level. Use:

sudo apt-get update

sudo apt-get upgrade

Step 2: You need something to build your libraries on. So, we use the Cross Platform Make or Cmake

sudo apt-get install cmake

Step 3: GUI’s would be created. Thus, we install the libgtk2.0 package. 

 sudo apt-get install libgtk2.0-dev

This would take some time. So, be patient.

Step 4: Next, we install python’s numpy library. It is used for scientific computations. Type in:

sudo apt-get install python-numpy

Step 5: Download the latest OpenCV version from the official page. Here is the link:

http://opencv.org/downloads.html

Extract it to your HOME  (home as in, not yours, Ubuntu’s home).

Step 6: In your terminal, enter the Extracted OpenCV folder using the cd command: 

 cd OpenCV-2.4.9 (Put in your folder name)

Step 7: Build your OpenCV package using cmake.

 cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_NEW_PYTHON_SUPPORT=ON -D CMAKE_INSTALL_PREFIX=/usr/local ./

There are many types to build like debug, but here, we are using the type Build. Cmake install prefix is used to generate the make file.

Step 8: Use make on the Make File! A make file is a special shell command file. Simply type:

make

make install

This is going to take a lot of time!

Step 9: Editor: some of you might want to use eclipse. I personally prefer using sublime text.

for eclipse:

cd ..

sudo apt-get install eclipse

sudo apt-get install eclipse-cdt.

You can run it by simply typing eclipse and it would open.

Step 10: This is the most important step.

You need to export your libraries to the packages installed or while  running, your terminal would throw up an error saying, OpenCV Package not found.

Export packages and set path to lib.

 export LD_LIBRARY_PATH=/home/opencv/lib

export PKG_CONFIG_PATH=/home/opencv/lib/pkgconfig


And you are done!

 

Try a sample program using the nano editor.

type nano opencvtest.cpp

#include<opencv2/highgui/highgui.hpp>
using namespace cv;

int main()
{

    Mat img = imread("/home/USER/Pictures/python.jpg",CV_LOAD_IMAGE_COLOR);
    imshow("opencvtest",img);
    waitKey(0);

    return 0;
}

Compile it using:

g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`
./opencvtest

For compiling an openCV code, you would have to always type the above lines. I prefer using a bash file instead of going through this boring stuff!

Next tutorial , I will show you how to create this bash file to make life simpler!

 

 

Advertisements

One thought on “Install OpenCV in Ubuntu

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s