Finger Detection and Tracking using OpenCV and Python
Tags: opencv, imageprocessing, fingerdetection, oss
Published: 2018-07-28 20:27:53

---
title: Finger Detection and Tracking using OpenCV and Python
published: true
description: A histogram based approach is used to separate out the hand from the background frame
tags: Opencv, imageprocessing, fingerDetection, OpenSourceSoftware
cover_image: https://thepracticaldev.s3.amazonaws.com/i/1l1xyvsi3g0h4gpm23nt.jpg
---

**TL;DR. Code is [here](https://github.com/amarlearning/Finger-Detection-and-Tracking)**.

Finger detection is an important feature of many computer vision applications. In this application, A histogram based approach is used to separate out the hand from the background frame. Thresholding and Filtering techniques are used for background cancellation to obtain optimum results.

One of the challenges that I faced in detecting fingers is differentiating a hand from the background and identifying the tip of a finger. I’ll show you my technique for tracking a finger, which I used in this project. To see finger detection and tracking in action check out this video.

{% youtube P3dUePye_-k %}

In an application where you want to track a user’s hand movement, skin color histogram will be very useful. This histogram is then used to subtracts the background from an image, only leaving parts of the image that contain skin tone.

A much simpler method to detect skin would be to find pixels that are in a certain RGB or HSV range. If you want to know more about this approach follow [here](https://docs.opencv.org/3.4.2/df/d9d/tutorial_py_colorspaces.html).

The problem with the above approach is that changing light conditions and skin colors can really mess with the skin detection. While on the other hand, Histogram tends to be more accurate and takes into account the current light conditions.

![Hand over the rectangles](https://thepracticaldev.s3.amazonaws.com/i/aaijxoqwmrkyx8epxq4t.png)

Green rectangles are drawn on the frame and the user places their hand inside these rectangles. Application is taking skin color samples from the user’s hand and then creates a histogram.

_The rectangles are drawn with the following function:_

{% gist https://gist.github.com/amarlearning/839998daf078d459cd238c78f9a814cb %}

There’s nothing too complicated going on here. I have created four arrays __`hand_rect_one_x`__, __`hand_rect_one_y`__, __`hand_rect_two_x`__, __`hand_rect_two_y`__ to hold the coordinates of each rectangle. The code then iterates over these arrays and draws them on the frame using __`cv2.rectangle`__. Here __`total_rectangle`__ is just the length of the array i.e. __`9`__.

Now that the user understands where to place his or her palm, the succeeding step is to extract pixels from these rectangles and use them to generate an HSV histogram.

{% gist https://gist.github.com/amarlearning/238ff29ba026e3ec8000be50bdd534e2 %}

Here function transforms the input frame to HSV. Using Numpy, we create an image of size __`[90 * 10]`__ with __`3`__ color channels and we name it as __ROI__ _(Region of Intrest)_. It then takes the 900-pixel values from the green rectangles and puts them in the ROI matrix.

The __`cv2.calcHist`__ creates a histogram using the ROI matrix for the skin color and __`cv2.normalize`__ normalizes this matrix using the norm Type __`cv2.NORM_MINMAX`__. Now we have a histogram to detect skin regions in the frames.

Now that the user understands where to place his or her palm, the succeeding step is to extract pixels from these rectangles and use them to generate an HSV histogram.

Now that we hold a skin color histogram we can use it to find the components of the frame that contains skin. OpenCV provides us with a convenient method, __`cv2.calcBackProject`__, that uses a histogram to separate features in an image. I used this function to apply the skin color histogram to a frame. If you want to read more about back projection, you can read from [here](https://docs.opencv.org/master/dc/df6/tutorial_py_histogram_backprojection.html) and [here](https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/back_projection/back_projection.html).

{% gist https://gist.github.com/amarlearning/14a1b6cd4a291c3e4d4c8a93e2bc39d9 %}

In the first two lines, I changed the input frame to HSV and then applied __`cv2.calcBackProject`__ with the skin color histogram __`hist`__. Following that, I have used Filtering and Thresholding function to smoothen the image. Lastly, I masked the input frame using the __`cv2.bitwise_and`__ function. This final frame should just contain skin color regions of the frame.

![Hand seperated from background (1)](https://thepracticaldev.s3.amazonaws.com/i/mcgp59fqr03d6bnsln1o.png)

![Hand seperated from background (2)](https://thepracticaldev.s3.amazonaws.com/i/orqapdym6iw05gake3wo.png)

Now we have a frame with skin color regions only, but what we really want is to find the location of a fingertip. Using OpenCV you can find contours in a frame if you don’t know what contour is you can read [here](https://docs.opencv.org/3.4.2/d3/d05/tutorial_py_table_of_contents_contours.html). Using contours you can find convexity defects, which will be potential fingertip location.

In my application, I needed to find the tip of a finger with which a user is aiming. To do this I determined the convexity defect, which is furthest from the centroid of the contour. This is done by the following code:

{% gist https://gist.github.com/amarlearning/9c554ebb244c11653b44c9c4e5c1f2dc %}

![Contour in Frame (1)](https://thepracticaldev.s3.amazonaws.com/i/bj7cm00cs5phg5u3hscg.png)

![Contour in Frame (2)
](https://thepracticaldev.s3.amazonaws.com/i/ovzk2u78ibfg05ajlbp3.png)

Then it determines the largest contour. For the largest contour, it finds the hull, centroid, and defects.

![Defects in red circle and Centroid in purple circle](https://thepracticaldev.s3.amazonaws.com/i/erv2ixq4v28qd51kv7du.png)

Now that you have all these defects you find the one that is farthest from the center of the contour. This point is assumed to be the pointing finger. The center is purple and the farthest point is red. And there you have it, you’ve found a fingertip.

![Centroid in purple color and Farthest point in red color](https://thepracticaldev.s3.amazonaws.com/i/3bbqd3amg0wdp2xevuo4.png)

All hard part is done up until now, now all we have to do is to create a __`list`__ to store the changed location of the __`farthest_point`__ in the frame. It’s up to you that how many changed points you want to store. I am storing only __`20`__ points.

![Finger Detection and Tracking using OpenCV and Python
](https://thepracticaldev.s3.amazonaws.com/i/1l1xyvsi3g0h4gpm23nt.jpg)

Lastly, thank you for reading this post. For more awesome posts, you can also follow me on Twitter — [iamarpandey](https://twitter.com/iamarpandey), Github — [amarlearning](https://github.com/amarlearning).

Happy coding! 🤓