简体   繁体   English

仅查找手部轮廓。 Python。 OpenCV

[英]Find hand contours only. Python. OpenCV

I have foreground mask, like this我有前景蒙版,像这样

If I use findContours method I receive this result如果我使用 findContours 方法,我会收到此结果

I want to find only main contours on hand without trash.我只想找到没有垃圾的主要轮廓。 How can I do it?我该怎么做?

thresh = 100
    # get threshold image
    ret, thresh_img = cv.threshold(fgMask, thresh, 255, cv.THRESH_BINARY)
    # find contours
    contours, hierarchy = cv.findContours(thresh_img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)

    # create an empty image for contours
    img_contours = np.zeros(img.shape)
    # draw the contours on the empty image
    cv.drawContours(img_contours, contours, -1, (0, 255, 0), 3)

    cv.imwrite("img.png", fgMask)
    cv.imwrite("img1.png", img_contours)

Maybe you can try blurring image before threshold to reduce the noise.也许您可以尝试在阈值之前模糊图像以减少噪声。 However , your foreground mask is quite bad actually for this purpose.但是,为此目的,您的前景蒙版实际上非常糟糕。 Since I cannot see what you have done before, it is generally better to use HSV color space if you want to achieve human skin.由于我看不到您之前所做的工作,因此如果您想实现人类皮肤,通常最好使用 HSV 颜色空间。 Because 因为

The color of human skin is created by a combination of blood (red) and melanin (yellow, brown).人类皮肤的颜色是由血液(红色)和黑色素(黄色、棕色)混合而成的。 Skin colors lie between these two extreme hues and are somewhat saturated.皮肤 colors 介于这两种极端色调之间,有些饱和。 HSV: (Hue-Saturation-Value) defined in a way that is similar to how humans perceive color. HSV:(Hue-Saturation-Value)以类似于人类感知颜色的方式定义。

img_path = "hand.jpg"
img = cv.imread(img_path)

# define the upper and lower boundaries of the HSV pixel intensities 
# to be considered 'skin'
hsvim = cv.cvtColor(img, cv.COLOR_BGR2HSV)
lower = np.array([0, 48, 80], dtype="uint8")
upper = np.array([20, 255, 255], dtype="uint8")
skinMask= cv.inRange(hsvim, lower, upper)

# blur the mask to help remove noise
skinMask= cv.blur(skinMask, (2, 2))

# get threshold image
ret, thresh = cv.threshold(skinMask, 100, 255, cv.THRESH_BINARY)
cv.imshow("thresh", thresh)

# draw the contours on the empty image
contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
contours = max(contours, key=lambda x: cv.contourArea(x))
cv.drawContours(img, [contours], -1, (255, 255, 0), 2)
cv.imshow("contours", img)

cv.waitKey()

should resulted better like结果应该更好在此处输入图像描述

Code Reference 代码参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM