Artificial Intelligence 6 min read

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

This article demonstrates how to build a hand‑gesture detection system in Python using OpenCV, covering video capture, YCrCb‑based skin detection, contour extraction, and provides the complete source code for reproducing the results.

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

The tutorial walks through creating a simple hand‑gesture recognition project that can be followed by beginners, using only OpenCV, basic Python syntax, and fundamental image‑processing concepts.

Video Capture : The script starts by opening a video file (or webcam) with cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4") , reads frames in a loop, resizes them, draws a rectangle to define the region of interest, and extracts that ROI for further processing.

cap = cv2.VideoCapture("C:/Users/lenovo/Videos/1.mp4")  # read file
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]
    ...
    if cv2.waitKey(50) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Skin Detection : The ROI is converted to the YCrCb color space to reduce the influence of illumination, then the Cr channel is blurred and thresholded with Otsu's method to obtain a binary skin mask. The mask is applied to the original ROI using cv2.bitwise_and .

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

Contour Processing : After converting the skin‑detected image to grayscale and applying a Laplacian filter, 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)
    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

The main loop applies the skin detection function A to the ROI, converts the result to grayscale, computes the Laplacian, and then feeds it to the contour function B . The intermediate results are displayed in separate windows titled "0" (ROI), "2" (contour), and the original video stream.

All code snippets are provided in a single block at the end of the article, allowing readers to copy and run the program directly.

computer visionPythonimage-processingopencvContour DetectionHand 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.