If you have been interested in computer vision and machine learning for some time, I’m sure you have heard about OpenCV - but have you learned more about it and practiced with it yourself? If not, this is the perfect place to start, as we will be discussing an overview of OpenCV, it’s optimization for heterogeneous hardware (particularly GPU optimization), and a tutorial on how to install OpenCV so you can begin learning on your own.
OpenCV is an open-source library (under BSD license) which allows you to work with and manipulate image and video files, including a webcam API so that you can use your own direct video feeds, and on top of that has over 2500 different computer vision and machine learning algorithms to be able to gather almost any type of data you may want from images or videos, and also make any transformation you can think of. It can do everything from recognize faces, track moving objects, and even extract 3D models from images (!), and much more, making it an extremely powerful tool for anyone working in computer vision and machine learning. It is also extremely versatile, having interfaces in C++, C, Java, and Python, and also supporting every major development platform - Windows, Linux, MacOS, iOS, and Android, giving it both high versatility and applicability. The applications for this library are nearly endless, for example - security (for example biometrics or city crime prevention), robotics (allows a robot to “see” its environment), medicine (using your phone camera to identify disease), and many many more.
Now let’s go over some more technical information, particularly as it deals with hardware acceleration on heterogeneous systems. Underneath, it is written in C/C++, which is known for being incredibly computationally efficient, making OpenCV run and train models quickly from it’s base. On top of this, the engineers have included a lot of technology for optimizing the library on heterogeneous hardware. Because it is written in optimized C/C++, it can take advantage of multi-core CPUs to the full, by utilizing every single core. The biggest optimization for computer vision however comes from the GPU, and OpenCV has a powerful module to utilize these optimizations fully, using technologies such as OpenCL and CUDA, and the module is supported by Nvidia, which speaks volumes as they are a leading GPU and hardware manufacturer globally. OpenCV is able to automatically tune itself to heterogeneous hardware to make sure it is running as efficiently as possible given the system it is on, an incredibly powerful feature. To demonstrate how significant these performance boosts are, here is a graph from the OpenCV website (opencv.org):
Now that you have an idea about what OpenCV is and how incredibly powerful it is, let’s run through a short tutorial on how you can install it on your own system and begin tinkering, and afterwards I will provide links to some cool tutorials you can follow in order to better learn the library. First, let’s make sure all our software packages are up to date:
sudo apt-get update
sudo apt-get upgrade
Next, we need to install the necessary developer tools, including Python development headers, to compile the library from source:
sudo apt-get install build-essential cmake pkg-config
sudo apt-get install python2.7-dev python3.5-dev
We will obviously be working with many different formats of images and videos, so now we will install the support for working with various formats of images and video:
sudo apt-get install libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get install libxvidcore-dev libx264-dev
The GUI interface for OpenCV is built using the GTK library, which we will be installing to ensure the interface works correctly:
sudo apt-get install libgtk-3-dev
And since machine learning and computer vision work by using many complex matrix calculations, the following libraries will optimize these calculations, and allow us to work with them more easily:
sudo apt-get install libatlas-base-dev gfortran
pip install numpy
Now we will download the latest build of OpenCV from Github, which at the time of writing is 3.3:
wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.3.0.zip
wget -O opencv_contrib.zip
We are almost there, now we just need to setup and configure our build, run cmake, compile OpenCV, and finally install it, after which you are good to go and ready to start programming:
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/ local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.1.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON ..
sudo make install
And you’re done - you should have a clean install of OpenCV for Python ready to run on your system. Don’t hesitate to post a comment with any issues you are having and I’ll be happy to help!
Clearly, OpenCV is an extremely powerful tool for computer vision and machine learning, and allows you to do almost anything you can dream of as far as computer vision goes. Not only that, it is highly scalable and versatile, offering four different interfaces and running natively on every single popular development platform. It also offers incredibly powerful optimization for heterogeneous hardware, offering multi-core support and more importantly a module that allows OpenCV to benefit incredibly from running on GPUs. The applications of this technology are endless, and now that you have the library installed on your own system, you will find links to some tutorials from across the internet for you to follow and learn the library better. Enjoy!
Some useful links to more tutorials you can look at in order to learn and have some fun:
- Face Detection on Raspberry Pi
- Applying a Bilateral Filter
- Canny Edge Detection
- How to distinguish apples and pears with Raspberry Pi