0

我正在尝试计算图像中手的姿势。所以,我从图像中提取了手。为了从手上提取指尖,我使用了convexHull()。我收到此错误“点不是 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)
4

1 回答 1

1

findContours 返回:“检测到的轮廓。每个轮廓都存储为点向量。” 而convexHull() 需要一个二维点集。所以我认为你需要一个 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()

结果:

在此处输入图像描述

于 2015-05-08T08:33:58.307 回答