簡體   English   中英

僅查找手部輪廓。 Python。 OpenCV

[英]Find hand contours only. Python. OpenCV

我有前景蒙版,像這樣

如果我使用 findContours 方法,我會收到此結果

我只想找到沒有垃圾的主要輪廓。 我該怎么做?

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)

也許您可以嘗試在閾值之前模糊圖像以減少噪聲。 但是,為此目的,您的前景蒙版實際上非常糟糕。 由於我看不到您之前所做的工作,因此如果您想實現人類皮膚,通常最好使用 HSV 顏色空間。 因為

人類皮膚的顏色是由血液(紅色)和黑色素(黃色、棕色)混合而成的。 皮膚 colors 介於這兩種極端色調之間,有些飽和。 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()

結果應該更好在此處輸入圖像描述

代碼參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM