Artificial Intelligence 6 min read

Hand Gesture Detection Using OpenCV and Python: Skin Color and Contour Processing

This article presents a step‑by‑step tutorial for building a hand‑gesture detection system in Python using OpenCV, covering video capture, skin‑color detection via YCrCb conversion, contour extraction, and full source code for processing frames and visualizing results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Hand Gesture Detection Using OpenCV and Python: Skin Color and Contour Processing

The author describes a beginner‑friendly project that implements hand‑gesture detection using Python and OpenCV, leveraging basic image‑processing techniques such as video capture, skin‑color detection in the YCrCb color space, and contour extraction to isolate the hand region.

Video Capture

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

The loop reads each frame, displays it, and exits when the user presses q .

Skin Color Detection

The method converts the frame from BGR to YCrCb, applies Gaussian blur to the Cr channel, and uses Otsu’s thresholding to obtain a binary skin mask, which is then applied to the original image.

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)
    res = cv2.bitwise_and(img, img, mask=skin)
    return res

Contour Processing

After obtaining a binary skin image, contours are found with cv2.findContours . The contours are sorted by area, and the largest one (assumed to be the hand) is drawn on a white canvas.

def B(img):
    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 background
    ret = cv2.drawContours(bg, contour[0], -1, (0, 0, 0), 3)  # draw largest contour
    return ret

Full Processing Loop

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]  # region of interest
    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()

The complete script captures video frames, extracts a hand region, performs skin detection, applies Laplacian filtering, extracts the largest contour, and displays both the original ROI and the processed contour image.

Overall, the tutorial demonstrates how to combine basic OpenCV functions to achieve real‑time hand‑gesture detection, providing all necessary code snippets and explanations for beginners.

computer visionPythonopencvHand GestureSkin 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.