如何获得识别肤色的阈值?

How to get threshold to recognize skin-ish color?

我正在修改 OpenCV 3.0 Python 代码,以便通过使用凸包在手指周围绘制轮廓来识别指尖上的点来识别手势。

无论如何,该代码有效,但它只适用于深色(黑色)背景。该代码使用阈值对图像进行二值化,因此最亮的形状将是手(因此背景为黑色,手形为白色)。

如何更改阈值以便将图像二值化以仅选择肤色而不是最亮的颜色?

例如,当完成二值化时,视频流图像的黑色部分将是所有非肤色的颜色,而二值化图像的白色部分将是肤色。

代码如下:

def readCamera(self):
    _, self.original = self.cap.read()
    self.original = cv2.flip(self.original, 1)

def threshold(self):

    hsv = cv2.cvtColor(self.original, cv2.COLOR_BGR2HSV)

    value = (31, 31)
    blurred = cv2.GaussianBlur(hsv, value, 0)
    _, self.thresholded = cv2.threshold(hsv[:,:,0], 0, 255,
                                        cv2.THRESH_BINARY+cv2.THRESH_OTSU)

def extractContours(self):
    _, self.contours, _ = cv2.findContours(self.thresholded.copy(),
                                        cv2.RETR_TREE,
                                        cv2.CHAIN_APPROX_SIMPLE)

将框架转换为 HSV 后,肤色会变得更偏绿。在此图像上应用 RGB 滤镜以滤除该皮肤部分。必要时使用轨迹栏,如下所示:

import cv2

def nothing(x): #needed for createTrackbar to work in python.
    pass    

cap = cv2.VideoCapture(0)
cv2.namedWindow('temp')
cv2.createTrackbar('bl', 'temp', 0, 255, nothing)
cv2.createTrackbar('gl', 'temp', 0, 255, nothing)
cv2.createTrackbar('rl', 'temp', 0, 255, nothing)
cv2.createTrackbar('bh', 'temp', 255, 255, nothing)
cv2.createTrackbar('gh', 'temp', 255, 255, nothing)
cv2.createTrackbar('rh', 'temp', 255, 255, nothing)
while true
        ret,img=cap.read()#Read from source
        hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
        bl_temp=cv2.getTrackbarPos('bl', 'temp')
        gl_temp=cv2.getTrackbarPos('gl', 'temp')
        rl_temp=cv2.getTrackbarPos('rl', 'temp')
        bh_temp=cv2.getTrackbarPos('bh', 'temp')
        gh_temp=cv2.getTrackbarPos('gh', 'temp')
        rh_temp=cv2.getTrackbarPos('rh', 'temp')
        thresh=cv2.inRange(hsv,(bl_temp,gl_temp,rl_temp),(bh_temp,gh_temp,rh_temp))
        if(cv2.waitKey(10) & 0xFF == ord('b')):
        break #break when b is pressed 
        cv2.imshow('Video', img)
        cv2.imshow('thresh', thresh)