Thursday 26 June 2014

How to Install OpenCV in Linux(Ubuntu 12.04 or above)

The steps that we follow will be similar to official pages of Ubuntu community website with certain changes to it. The problems that may occur during installation is discussed assuming that you don't have much experience with linux systems.


Things make sure you have before installing:

  • internet connection

Things you don't need during installation:

Lets start installing OpenCV in Ubuntu:

  1. Now you need to run this .sh file through terminal application.(press Ctrl+Alt+T to open Terminal)
  2. First copy the downloaded file opencv.sh and place it where you want to keep the copy of OpenCV.
  3. In my case it is Desktop.So I placed opencv.sh file in Desktop.
  4. Change your current directory to Desktop using cd command
    • cd Desktop
  5. To run this file, first we need to change the file permission of the file
    • chmod +x opencv.sh
  6. Now run it.
    • ./opencv.sh
  7. This command takes enough time to download and install all the dependency packages like video codecs, audio codecs, python environment, Qt and GTK+2 frameworks for GUI and many more.
  8. It also download compilers gcc, g++.
  9. The Latest version of OpenCV available in sourceforge.net is downloaded and installed.
  10. If installation is successful, you may get message like this "OpenCV2.4.x ready to be used".

Now lets explore what is written in opencv.sh shell file

  • First two lines gets the latest version of OpenCV and stores version number in "version" variable.
  • 3-4 lines creates OpenCV folder where it download's latest OpenCV library.
  • line 6 removes any earlier version of libraries(ffmpeg x264 libx264-dev)
  • line 8-13 download's lots of dependencies
  • line 10 download's latest zipped OpenCV library
  • line 12 unzippes library file
  • line 13-15 creates "build" directory inside unzipped library file and now on current working directory will be "build" folder where we get build of opencv for linux which can be installed.
  • line 16-17 will create build by using cmake tool.
  • line 18 is to install the build. If checkinstall is not available in your system you may end up with error. And you can see an error message as not installed(something like that) above "OpenCV2.4.x ready to be used". If this is your case then replace line 18 by   "sudo make install" and redo the samething starting from step no. 6 .when propmt appeares then press A to install all packages in library.
  • Now you have running opencv in your system.
  • line 19-20 copying the configuration file of OpenCV to lib folder.(Very crucial process)
  • This is it in opencv.sh

Now let us run a small program to check the opencv installation

 #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;
}
  • save this code in a file with name as "abc.cpp"
  • change the path of the image in imread command with your own image available in hard disk(do not use this \, use only /, to separate directories) 
  • to compile it 
    • g++ 'pkg-config --cflags opencv' abc.cpp 'pkg-config --libs opencv' -o abc 

  • which generates executable named as "abc", now run the abc
    • ./abc

By now you must see your image being loaded up in a window

Thus, OpenCV is properly configured in your Linux system

Note: Never ever forget to add header files as shown above in the code(dont miss "opencv2/")

Other header file available in OpenCV are as follows
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/flann/miniflann.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/photo/photo.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui_c.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

or simply you can #include "opencv2/opencv.hpp"


 

No comments:

Post a Comment