Skip to content
Amar Prakash Pandey

Finger Detection and Tracking using OpenCV and Python

opencv, image-processing, finger-detection, open-source-software3 min read

TL;DR. Code is here.

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.

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.

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.

 Place hand over the rectangles

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:

draw_rectangles_in_frame.py
1def draw_rect(frame):
2 rows, cols, _ = frame.shape
3 global total_rectangle, hand_rect_one_x, hand_rect_one_y, hand_rect_two_x, hand_rect_two_y
4
5 hand_rect_one_x = np.array(
6 [6 * rows / 20, 6 * rows / 20, 6 * rows / 20, 9 * rows / 20, 9 * rows / 20, 9 * rows / 20, 12 * rows / 20,
7 12 * rows / 20, 12 * rows / 20], dtype=np.uint32)
8
9 hand_rect_one_y = np.array(
10 [9 * cols / 20, 10 * cols / 20, 11 * cols / 20, 9 * cols / 20, 10 * cols / 20, 11 * cols / 20, 9 * cols / 20,
11 10 * cols / 20, 11 * cols / 20], dtype=np.uint32)
12
13 hand_rect_two_x = hand_rect_one_x + 10
14 hand_rect_two_y = hand_rect_one_y + 10
15
16 for i in range(total_rectangle):
17 cv2.rectangle(frame, (hand_rect_one_y[i], hand_rect_one_x[i]),
18 (hand_rect_two_y[i], hand_rect_two_x[i]),
19 (0, 255, 0), 1)
20
21 return frame

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.

generate_hsv_histogram.py
1def hand_histogram(frame):
2 global hand_rect_one_x, hand_rect_one_y
3
4 hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
5 roi = np.zeros([90, 10, 3], dtype=hsv_frame.dtype)
6
7 for i in range(total_rectangle):
8 roi[i * 10: i * 10 + 10, 0: 10] = hsv_frame[hand_rect_one_x[i]:hand_rect_one_x[i] + 10,
9 hand_rect_one_y[i]:hand_rect_one_y[i] + 10]
10
11 hand_hist = cv2.calcHist([roi], [0, 1], None, [180, 256], [0, 180, 0, 256])
12 return cv2.normalize(hand_hist, hand_hist, 0, 255, cv2.NORM_MINMAX)

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 and here.

histogram_masking.py
1def hist_masking(frame, hist):
2 hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
3 dst = cv2.calcBackProject([hsv], [0, 1], hist, [0, 180, 0, 256], 1)
4
5 disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (31, 31))
6 cv2.filter2D(dst, -1, disc, dst)
7
8 ret, thresh = cv2.threshold(dst, 150, 255, cv2.THRESH_BINARY)
9
10 thresh = cv2.merge((thresh, thresh, thresh))
11
12 return cv2.bitwise_and(frame, thresh)

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)
Hand seperated from background (2)

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. 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:

fingertip_point.py
1def manage_image_opr(frame, hand_hist):
2 hist_mask_image = hist_masking(frame, hand_hist)
3 contour_list = contours(hist_mask_image)
4 max_cont = max_contour(contour_list)
5
6 cnt_centroid = centroid(max_cont)
7 cv2.circle(frame, cnt_centroid, 5, [255, 0, 255], -1)
8
9 if max_cont is not None:
10 hull = cv2.convexHull(max_cont, returnPoints=False)
11 defects = cv2.convexityDefects(max_cont, hull)
12 far_point = farthest_point(defects, max_cont, cnt_centroid)
13 print("Centroid : " + str(cnt_centroid) + ", farthest Point : " + str(far_point))
14 cv2.circle(frame, far_point, 5, [0, 0, 255], -1)
15 if len(traverse_point) < 20:
16 traverse_point.append(far_point)
17 else:
18 traverse_point.pop(0)
19 traverse_point.append(far_point)
20
21 draw_circles(frame, traverse_point)
Contour in Frame (1)
Contour in Frame (2)

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

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

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

Lastly, thank you for reading this post. For more awesome posts, you can also follow me on Twitter — iamarpandey, Github — amarlearning.

Happy coding! 🤓

© 2022 by Amar Prakash Pandey. All rights reserved.