Image Processing using OpenCV

Image Processing using OpenCV

in

OpenCV is the most widely used Computer Vision Library out there. It is very powerful and offers many features suiting the needs of anyone requiring Image Processing. This tutorial is a slightly modified version of this with a simpler method of compiling.

Installation

The simplest possible install is always from the repositories.

For Debian/Ubuntu/Mint:

sudo apt-get install libopencv-dev

For Other distros like Arch, you can find the respective commands on the internet.

If you want python support too, you will need to install the python support by:

sudo apt-get install python-opencv

The repository versions of OpenCV are usually not the latest versions and you might miss some features.

If you need specific features or a newer version of OpenCV (let’s say for python3 support), you could look here for a very good guide on compiling OpenCV from source.

Compiling and running Programs in C++

Now that OpenCV has been installed, you might want to run a program to see if it is working properly. I would suggest the following program:

#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char **argv) {

  // A camera object
  VideoCapture cam(0);

  // An image object
  Mat img;

  while(true) {

    // Storing a frame from camera to image
    cam >> img;

    // Do Your Operations Here

    // Displaying image in a window named "Image"
    imshow("Image",img);

    // Waiting for a keypress for 5ms

    /**
      * WaitKey is very important.
      * Even if you don't want a delay/keypress you should
      * use it with minimum delay otherwise, the gui does not update
      */
    int c = waitKey(5);

    // Quit on pressing q
    if( c == 'q' || c == 'Q')
      break;
  }
  return 0;
}

This simple program just takes input from your webcam and displays it in a window. You can save it in a file named test.cpp.

Now, in order to compile this program, execute this from the terminal:

g++ -o Test /path/to/test.cpp `pkg-config --libs opencv`

The parameter after -o, i.e., Test refers to the name of the executable created after compiling.

After compilation finishes (hopefully without any errors), you can run this test program with:

./Test

For a more involved and complex compilation involving cmake, you can look here and here.

Learning OpenCV

Now that you can compile and execute OpenCV programs, you are ready to begin learning OpenCV. The OpenCV tutorials on their website is a good starting point. My suggested order of reading is:

The above order is because the initial tutorials tend to be more theoretical and focussing on the internals of OpenCV rather than actual usage.

Plus, you might not need to see all the tutorials depending upon your needs.

Best of Luck developing your OpenCV application.