Human Skin Detection with OpenCV: YCrCb and HSV Based Methods
This article demonstrates how to use Python's OpenCV library for human skin detection by converting images to YCrCb and HSV color spaces, applying Gaussian blur, Otsu thresholding, and range‑based segmentation, with step‑by‑step installation, code examples, and visual results.
The OpenCV library, released under a BSD license, provides cross‑platform computer‑vision functions for Linux, Windows, macOS, Android, and more, and can be accessed from Python, Ruby, MATLAB, etc. It is used here to implement a human skin‑color detection feature.
First, install the required Python packages from a fast mirror:
pip install opencv-python -i https://mirrors.ustc.edu.cn/pypi/web/simple pip install numpy -i https://mirrors.ustc.edu.cn/pypi/web/simpleBasic image handling with OpenCV includes reading, displaying, and saving images:
import numpy as np
import cv2
imname = "6358772.jpg" # path to 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 : Convert the image to YCrCb, extract the Cr channel, apply Gaussian blur, then use Otsu’s method to obtain a binary skin mask.
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 : After converting to YCrCb, keep pixels whose Cr value lies between 140‑175 and Cb between 100‑120.
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 140 < cr[i][j] < 175 and 100 < 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 range filtering : Convert the image to HSV and keep pixels with H in 7‑20, S in 28‑255, and V in 50‑255.
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 7 < _h[i][j] < 20 and 28 < _s[i][j] < 255 and 50 < _v[i][j] < 255:
skin3[i][j] = 255
else:
skin3[i][j] = 0
cv2.imshow(imname, img)
cv2.imshow(imname + " Skin3 HSV", skin3)Running screenshots and comparative result images illustrate the effectiveness of each method. The first method (Cr+Otsu) produces a clean binary mask, the second (Cr/Cb range) offers a tighter skin region, and the third (HSV range) provides a balance between precision and recall.
The three detection results are further compared in a single figure, showing the trade‑offs among the approaches.
Reference: OpenCV documentation and various online tutorials on skin detection.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.