我正在尝试计算图像中手的姿势。因此,我从图像中提取了手。为了从手中提取指尖,我使用了concveHull()。我收到此错误“点不是numpy数组,也不是标量”。

这是代码:

import numpy as np
import cv2

img = cv2.imread("hand.jpg")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV);
lower_hand = np.array([0,30,60])
upper_hand = np.array([20,150,255])

mask = cv2.inRange(hsv, lower_hand, upper_hand)

res = cv2.bitwise_and(img, img, mask=mask)

derp,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
hull = cv2.convexHull(contours)
print hull
cv2.drawContours(img, contours, -1, (0,255,0), 3)

最佳答案

findContours返回:“检测到的轮廓。每个轮廓都存储为点 vector 。”而凸面壳()需要一个二维点集。所以我认为您将需要一个for循环,如下所示:

    for cnt in contours:
        hull = cv2.convexHull(cnt)
        cv2.drawContours(img,[cnt],0,(0,255,0),2)
        cv2.drawContours(img,[hull],0,(0,0,255),2)

完整的工作代码:
import numpy as np
import cv2

img = cv2.imread("hand.jpg")
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV);
lower_hand = np.array([0,30,60])
upper_hand = np.array([20,150,255])

mask = cv2.inRange(hsv, lower_hand, upper_hand)

res = cv2.bitwise_and(img, img, mask=mask)

#"derp" wasn't needed in my code tho..
derp,contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    hull = cv2.convexHull(cnt)
    cv2.drawContours(img,[cnt],0,(0,255,0),2)
    cv2.drawContours(img,[hull],0,(0,0,255),2)

cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

关于opencv - 指尖检测,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30119189/

10-12 23:19