我正在尝试计算图像中手的姿势。所以,我从图像中提取了手。为了从手中提取指尖我使用了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)
答案 0 :(得分:1)
findContours返回:"检测到轮廓。每个轮廓都存储为一个点矢量。"而且convexHull()需要一个2D点集。所以我认为你需要一个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()
结果: