指尖检测
Dectection of finger tip
我正在尝试计算图像中手的姿势。所以,我从图像中提取了手。为了从手上提取指尖,我使用了 convexHull()。我收到此错误 "points is not a numpy array, neither a scalar"。
代码如下:
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 returns: "detected contours. Each contour is stored as a vector of points." 而 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()
结果:
我正在尝试计算图像中手的姿势。所以,我从图像中提取了手。为了从手上提取指尖,我使用了 convexHull()。我收到此错误 "points is not a numpy array, neither a scalar"。
代码如下:
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 returns: "detected contours. Each contour is stored as a vector of points." 而 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()
结果: