这是皮肤检测的理想解决方案之一。
上面的大多数答案都适用于各种肤色,如深红色、浅黄色、浅橙色……。但这些颜色不应该是肤色,因为它们在图像中几乎看不到。
正如您在这张图片中看到的,红色范围在 350 到 20 之间。

因此,皮肤有两种可能的色调范围。
单纯疱疹病毒范围
#1st range of Hue
lower -> [0, 30, 53]
upper -> [20, 150, 255]
#2nd range of Hue (OpenCV converts 360 to 180)
lower2 -> [172, 30, 53]
upper2 -> [180, 180, 210]
完整代码:
import os, cv2
import numpy as np
image = cv2.imread(os.path.join('skin.png'))
# Covers both range
lower = np.array([0, 30, 53], dtype = "uint8")
upper = np.array([20, 180, 255], dtype = "uint8")
lower2 = np.array([172, 30, 53], dtype = "uint8")
upper2 = np.array([180, 180, 210], dtype = "uint8")
converted = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
skinMask = cv2.inRange(converted, lower, upper)
skinMask2 = cv2.inRange(converted, lower2, upper2)
#Gaussian Blur
skinMask = cv2.GaussianBlur(skinMask, (3, 3), 0)
skinMask2 = cv2.GaussianBlur(skinMask2, (3, 3), 0)
skin1 = cv2.bitwise_and(image, image, mask = skinMask)
skin2 = cv2.bitwise_and(image, image, mask = skinMask2)
skin = cv2.bitwise_or(skin1,skin2) #adding both ranges
# show the skin in the image along with the mask
cv2.imshow("images", np.hstack([frame, skin]))
cv2.waitKey(0)