Artificial Intelligence 9 min read

Skin Color Detection Using OpenCV in Python

This article explains how to implement human skin color detection in Python with OpenCV, covering library installation, basic image operations, and three algorithms based on YCrCb and HSV color spaces, including code examples, parameter choices, and visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Skin Color Detection Using OpenCV in Python

The article introduces a skin‑color detection feature implemented with the OpenCV library, an open‑source, cross‑platform computer‑vision toolkit that provides C functions, C++ classes, and bindings for Python, Ruby, MATLAB, and other languages.

Overview : The detection pipeline uses color‑space conversion, channel extraction, Gaussian smoothing, and Otsu thresholding to isolate skin regions.

References : The author cites OpenCV tutorials on skin detection and related learning resources.

Preparation :

Install the required Python packages using the following commands (mirrored from the University of Science and Technology of China for faster download):

pip install opencv-python -i https://mirrors.ustc.edu.cn/pypi/web/simple
pip install numpy -i https://mirrors.ustc.edu.cn/pypi/web/simple

Basic Image Operations :

import numpy as np
import cv2
imname = "6358772.jpg"
# Read the image
img = cv2.imread(imname, cv2.IMREAD_COLOR)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite(imname + "01.jpg", img)

Algorithm 1 – YCrCb Cr Component + Otsu Thresholding :

img = cv2.imread(imname, cv2.IMREAD_COLOR)
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
(y, cr, cb) = cv2.split(ycrcb)
cr1 = cv2.GaussianBlur(cr, (5, 5), 0)
_, skin1 = cv2.threshold(cr1, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imshow("image CR", cr1)
cv2.imshow("Skin Cr+OSTU", skin1)

Algorithm 2 – YCrCb Cr and Cb Range Filtering :

img = cv2.imread(imname, cv2.IMREAD_COLOR)
ycrcb = cv2.cvtColor(img, cv2.COLOR_BGR2YCrCb)
(y, cr, cb) = cv2.split(ycrcb)
skin2 = np.zeros(cr.shape, dtype=np.uint8)
(x, y) = cr.shape
for i in range(0, x):
    for j in range(0, y):
        if (cr[i][j] > 140) and (cr[i][j] < 175) and (cb[i][j] > 100) and (cb[i][j] < 120):
            skin2[i][j] = 255
        else:
            skin2[i][j] = 0
cv2.imshow(imname, img)
cv2.imshow(imname + " Skin2 Cr+Cb", skin2)

Algorithm 3 – HSV H, S, V Range Filtering :

img = cv2.imread(imname, cv2.IMREAD_COLOR)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
(_h, _s, _v) = cv2.split(hsv)
skin3 = np.zeros(_h.shape, dtype=np.uint8)
(x, y) = _h.shape
for i in range(0, x):
    for j in range(0, y):
        if (_h[i][j] > 7) and (_h[i][j] < 20) and (_s[i][j] > 28) and (_s[i][j] < 255) and (_v[i][j] > 50) and (_v[i][j] < 255):
            skin3[i][j] = 255
        else:
            skin3[i][j] = 0
cv2.imshow(imname, img)
cv2.imshow(imname + " Skin3 HSV", skin3)

The article presents screenshots of the detection results for each method and a final comparison image that visualizes the differences among the three algorithms.

Overall, the guide provides a practical, step‑by‑step tutorial for building skin‑color detection pipelines in Python using OpenCV, suitable for beginners and intermediate developers interested in computer‑vision applications.

computer visionPythonimage-processingopencvcolor spaceSkin 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.