Monday, 27 July 2015

Using OpenCV with Eclipse (plugin CDT) on Ubuntu

OpenCV is written natively in C++ and has a templated interface that works seamlessly with STL containers. OpenCV library therefore, is extensively used with C++. Here, we will see how to use Eclipse with CDT Plugin on Ubuntu 14.04. We will use OpenCV-2.4.11 and Luna Eclipse for C/C++ developers. We start with downloading Eclipse and then configuring it with OpenCV libraries. We will also implement an a OpenCV program.


I will assume that you have the OpenCV-2.4.11 installed on your Ubuntu System. However, if you do not have it installed or you are facing problems getting it done, you can refer to Installing OpenCV-2.4.11 on Ubuntu.

Let's start!

Before we start, we're going to need to download Eclipse. I use OpenCV with C ++, Then I'm going to download the specific release to C / C ++ of Eclipse Luna: Eclipse IDE for C / C ++ Developers. Keep referring to the screenshots as you keep progressing through tutorial. The pictures will exemplify each step and surely simplify your life !


Downloading and Installing Eclipse

  1. Go to Downloads Eclipse.
  2. In Package Solutions, search for  Eclipse IDE for C / C ++ Developers.
  3. Select Linux 32 or 64 bits; (In my case, Linux 64-bit);
  4. Unzip in any folder of your choice;
  5. Done!You also can install Eclipse using sudo apt-get install eclipse-cdt

Making a C++ Project

  1. Start Eclipse. Just run the executable that comes in the folder.
  2. Go to File -> New -> C/C++ Project
  3. Choose a name for your project (i.e. ImageProcessing). An Empty Project should be okay for this example.
  4. Leave everything else by default. Press Finish.
  5. Your project (in this case ImageProcessing) should appear in the Project Navigator (usually at the left side of your window).
  6. Now, let’s add a source file using OpenCV:
    • Right click on ImageProcessing (in the Navigator). New -> Folder .
    • Name your folder src and then hit Finish
    • Right click on your newly created src folder. Choose New source file:
    • Call it ImageProcessing.cpp. Hit Finish
  7. So, now you have a project with a empty .cpp file. Let’s fill it with some sample code (in other words, copy and paste the snippet below):


  8. We are only missing one final step: To tell OpenCV where the OpenCV headers and libraries are. For this, do the following:
    • Go to Project–>Properties
    • In C/C++ Build, click on Settings. At the right, choose the Tool Settings Tab. Here we will enter the headers and libraries info:
      1. In GCC C++ Compiler, go to Includes.In Include paths(-l) you should include the path of folder where opencv was installed. In our example,this is  /usr/local/include/opencv.


      2. Now go to GCC C++ Linker,there you have to fill two spaces: First in Library search path (-L) you have to write the path to where the opencv libraries reside, in my case the path is:

        /usr/local/lib
      3. Then in Libraries(-l) add the OpenCV libraries that you may need.

        opencv_core opencv_imgproc opencv_highgui opencv_ml opencv_video opencv_features2d opencv_calib3d opencv_objdetect opencv_contrib opencv_legacy opencv_flann


        Now you are done. Click OK
    • Your project should be ready to be built. For this, go to Project->Build all

Running the Executable

    • Go to Run->Run Configurations
    • Under C/C++ Application you will see the name of your executable + Debug. Select the name (in this case ImageProcessing Debug).
    • Now, in the right side of the window, choose the Arguments Tab. Write the path of the image file we want to open (path relative to the workspace/DisplayImage folder). Let’s use HappyLittleFish.png:


    • Click on the Apply button and then in Run. An OpenCV window should pop up with the image.


    • Congratulations! You are ready to have fun with OpenCV using Eclipse.

Friday, 24 July 2015

Installing OpenCV-2.4.11 on ubuntu 14.04

Why OpenCV?

Have you at any point of time wondered what OpenCV is all about ? And what makes it so popular among researchers and image processing experts?  Well, OpenCV is the most popular and advanced library with more than 2500 optimised algorithm, which include a comprehensive set of both classic and state-of-art computer vision and machine learning algorithms. These algorithms can be used to detect faces, identify objects, classify human actions in videos, track camera movements and many more. OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 7 million. The library is used extensively in companies, research groups and by governmental bodies. And the best part is: its free for both academic and commercial use with C++, C, Python and Java interfaces and supports Windows, Linux, MacOS, iOS and Android. The official website and support for OpenCV can be found here.

Lets get Started!

So, without waiting lets get started with installing it on our Ubuntu Systems. 

1st Step: Install the Dependencies

To install the dependencies required from OpenCV, just run the following commands:
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip

2nd Step: Download OpenCV 2.4.11

cd~ 
wget https://github.com/Itseez/opencv/archive/2.4.11.zip -O opencv-2.4.11.zip
unzip opencv-2.4.11.zip

3rd step: Install OpenCV

Now, we’ll install OpenCV. Cmake command has a lot of options: choose those that better suit your needs and run the commands below. If you’re planning to use Qt 5.x, don’t use -D WITH_QT=ON. Learn how to use OpenCV with Qt Creator 3.2 (Qt 5.3). According to one of the users that tested it on Ubuntu 14.04, you’ll need to use WITH_FFMPEG=OFF.
cd opencv-2.4.11
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
make -j 4
sudo make install
Note: In command make -j 4 , the number '4' denotes number of core process your system supports. You could use '8', if you own octa-core processor.

4th step: Finishing Installation!! 

sudo gedit /etc/ld.so.conf.d/opencv.conf
 And add the following line to the file and save it.
/usr/local/lib/ 
Now, you have to open another file. 
sudo gedit /etc/bash.bashrc
And add the following 2 lines to it and save it. 
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
exprt PKG_CONFIG_PATH
Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.
Now you have OpenCV 2.4.11 installed in your computer with 3D visualization, Python, Java, TBB, OpenGL, video, and Qt support.

Testing the Installations!

Just in case you are skeptical about the installation, you can simply check it by running few if the inbuilt examples. 
cd opencv/samples/c
chmod +x build_all.sh
./build_all.sh
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" lena.jpg
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg
I hope this tutorial helps you out! Do leave comments if something is not clear or you need an help! Suggestions and feedback are most welcome..!! Have great time with 'Computer Vision'.