Artificial Intelligence 7 min read

Hand Gesture Recognition with OpenCV: Video Capture, Skin Detection, and Contour Processing in Python

This tutorial demonstrates how to build a simple hand‑gesture detection system in Python using OpenCV, covering video capture, YCrCb‑based skin detection, Laplacian filtering, and contour extraction to isolate the hand region for further processing.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Hand Gesture Recognition with OpenCV: Video Capture, Skin Detection, and Contour Processing in Python

Recently a beginner‑friendly hand‑gesture processing project was developed using OpenCV, basic Python syntax, and fundamental image‑processing concepts.

Getting Video (Camera)

The video source is obtained either from a video file or directly from a webcam.

<code>cap = cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4")  # read file
# cap = cv2.VideoCapture(0)  # read webcam
while True:
    ret, frame = cap.read()
    key = cv2.waitKey(50) & 0xFF
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()</code>

Skin Detection

The algorithm converts the image from BGR to YCrCb color space to reduce the influence of luminance, then applies Gaussian blur and Otsu thresholding to isolate skin pixels.

<code>def A(img):
    YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)  # convert to YCrCb
    (y, cr, cb) = cv2.split(YCrCb)                     # split channels
    cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
    _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)  # Otsu
    res = cv2.bitwise_and(img, img, mask=skin)
    return res</code>

Contour Processing

Contours are extracted with cv2.findContours , sorted by area, and the largest contour (assumed to be the hand) is drawn on a white canvas.

<code>def B(img):
    # binaryimg = cv2.Canny(Laplacian, 50, 200)  # optional binarization
    h = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  # find contours
    contour = h[0]
    contour = sorted(contour, key=cv2.contourArea, reverse=True)  # sort by area
    bg = np.ones(dst.shape, np.uint8) * 255  # white canvas
    ret = cv2.drawContours(bg, contour[0], -1, (0, 0, 0), 3)  # draw largest contour
    return ret</code>

Full Script

<code>"""Read frames from video and save as images"""
import cv2
import numpy as np

cap = cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4")  # read file
# cap = cv2.VideoCapture(0)  # read webcam

def A(img):
    YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
    (y, cr, cb) = cv2.split(YCrCb)
    cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
    _, skin = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    res = cv2.bitwise_and(img, img, mask=skin)
    return res

def B(img):
    # binaryimg = cv2.Canny(Laplacian, 50, 200)
    h = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    contour = h[0]
    contour = sorted(contour, key=cv2.contourArea, reverse=True)
    bg = np.ones(dst.shape, np.uint8) * 255
    ret = cv2.drawContours(bg, contour[0], -1, (0, 0, 0), 3)
    return ret

while True:
    ret, frame = cap.read()
    src = cv2.resize(frame, (400, 350), interpolation=cv2.INTER_CUBIC)
    cv2.rectangle(src, (90, 60), (300, 300), (0, 255, 0))
    roi = src[60:300, 90:300]  # hand region
    res = A(roi)               # skin detection
    cv2.imshow("0", roi)
    gray = cv2.cvtColor(res, cv2.COLOR_BGR2GRAY)
    dst = cv2.Laplacian(gray, cv2.CV_16S, ksize=3)
    Laplacian = cv2.convertScaleAbs(dst)
    contour = B(Laplacian)    # contour processing
    cv2.imshow("2", contour)
    key = cv2.waitKey(50) & 0xFF
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()</code>

The resulting application captures video frames, isolates the hand region using color‑space conversion and thresholding, extracts the dominant contour, and displays both the original ROI and the processed contour image.

computer visionPythonimage processingopencvGesture RecognitionSkin Detection
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.