Introduction to Image Processing with OpenCV

In this article we will make use of image processing to detect faces, cool right? ;) Part 1

Abubakr masood
Analytics Vidhya

--

photo by Google

What is Image Processing?

Image Processing is most commonly termed as ‘Digital Image Processing’ and It is considered as a subset of Computer Vision. Both Image Processing algorithms and Computer Vision (CV) algorithms take an image as input; however, in image processing, the output is also an image, whereas in computer vision the output can be some features/information about the image.

Image Processing simply means that an algorithm does some transformations on the image such as smoothing, sharpening, contrasting, stretching on the image.

Prerequisites

Firstly, you should have some basic working knowledge of Python. Secondly, you should know what machine learning is and the basics of how it works, as we will be using some machine learning algorithms for image processing in this article. As a bonus, it would help if you have had any exposure to, or basic knowledge of, OpenCV. Even if you have no idea of the above things don’t worry just continue reading you will get a glimpse of those things :)

  • OpenCV:- It is an open source computer vision library. You can find a detailed description here on its official documentation.
  • Haar Cascade:- It is a machine learning based approach where a cascade function is trained from a lot of positive and negative images. It is then used to detect objects in other images. You can create a Haar cascade file for any real world object. For detailed study click here.

Installation

Since we are going to use OpenCV via Python, it is an implicit requirement that you already have Python (version 3) already installed. The installation can be processed as follows

To check that your installation is successful, run the command ( import cv2). If you get the return without any errors, then you are now ready to go!

  • Download the code:- Please clone this Github Link in your work space to get started (Image_det.py).
  • Import the image:- Now the first step we’re going to do with OpenCV is loading some pre trained data (haar cascade algorithms) for frontal face detection by using the Haar cascade xml file , importing an image and converting it to grayscale. It can be done as follows ;
face.py
RGB To Grayscale Image

Note: The image has been scaled for the sake of displaying it in this article, but the original size we are using is about 600x900.

  • Convert the image to grayscale: — RGB image contains lots of data which may not be required for your processing. When you convert a RGB image into Grayscale you discard lots of information which are not required for processing and also you will save a lot of computational power as well. It has varying shades of black to gray to white.
  • Detecting faces and draw rectangle around the faces:-
  1. Using the Haar cascade model initialized in the last step, we run and detect the image and the number of faces detected.
  2. Finally we shall create a rectangular box around the face detected in previous steps

I have imported randrange from random (2nd line of the first code box) this creates the box of multiple colors. If you want it as a single color just omit the randrange and use simple color code.

detected image

As can be seen that our script detected all the 6 faces in the input image. Which is great here but for the image of the crowd may be it will not detect all faces in that case. So, by training our own custom classifiers, we can make predictions much better.

That’s it for now, Stay tuned next article on this series would be on real time face detection.

--

--