Skip to content

Hand posture detection with OpenCV #8

@rainyear

Description

@rainyear
Owner

简单的手势识别,基本思路是基于皮肤检测,皮肤的颜色在HSV颜色空间下与周围环境的区分度更高,从RGB转换到HSV颜色空间下针对皮肤颜色进行二值化,得到mask:

def HSVBin(img):
    hsv = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)

    lower_skin = np.array([100, 50, 0])
    upper_skin = np.array([125, 255, 255])

    mask = cv2.inRange(hsv, lower_skin, upper_skin)
    # res = cv2.bitwise_and(img, img, mask=mask)
    return mask

然后通过腐蚀与膨胀等形态学变化去除一些噪点,得到更完整的白色(皮肤)色块,最后找出色块的轮廓,并通过色块大小排除一些面积较小的噪点:

def getContours(img):
    kernel = np.ones((5,5),np.uint8)
    closed = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)
    closed = cv2.morphologyEx(closed, cv2.MORPH_CLOSE, kernel)
    contours, h  = cv2.findContours(closed, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    validContours = [];
    for cont in contours:
        if cv2.contourArea(cont) > 9000:
            # x,y,w,h = cv2.boundingRect(cont)
            # if h/w > 0.75: # filter face failed
            validContours.append(cv2.convexHull(cont))
            # rect = cv2.minAreaRect(cont)
            # box = cv2.cv.BoxPoints(rect)
            # validContours.append(np.int0(box))
    return validContours

如果需要排除脸部皮肤干扰,可以同时加入人脸识别,在转换颜色空间之前将识别到的脸部位置颜色去除掉,最终运行结果如下:

handy

完整代码


🍺BitCoin donate button🍺Tenpay donate button🍺Alipay donate button🍺

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @rainyear

      Issue actions

        Hand posture detection with OpenCV · Issue #8 · rainyear/lolita